How can you pause or break a C plus plus program running in DOS-BOX?
There is no pause function as such, but you can easily roll your own:
#include <iostream>
#include <limits>
void Pause()
{
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
int main()
{
Pause();
return( 0 );
}
What is c program of roman numbers?
There being only 7 symbols to consider (IVXLCDM), conversion is easily achieved in any number of ways. In general, numerals are formed from left to right, largest value to smallest. However, if a smaller value precedes a larger value, the smaller value is subtracted from the larger value (or is negated). This can lead to problems such as IVX. Reading left to right this would become 10 - ( 5 - 1 ) = 10 - 4 = 6. There's nothing wrong with this, but most people would accept 6 = VI, not IVX.
The problem is there has never been an official standard relating to how Roman numerals are formed. Decimal 1999 could be represented as MCMXCIX or MIM or MDCCCCLXXXXVIIII or even a mixed format like MCMXCVIIII. All are intrinsically correct. However, only the first example conforms to what many would consider to be the "unofficial" standard, whereby certain combinations are no longer permitted (such as IIII, IM and VX).
This standard has been incorporated into the following code.
#include <iostream>
using namespace std;
int main()
{
char roman[11];
int decimal[10];
memset( roman, 0, 11 );
memset( decimal, 0, 10 * sizeof( int ));
cout << endl;
cout << "Enter a Roman number (max. 10 chars from I, V, X, L, C, D or M): ";
cin.getline( roman, 11, '\n' );
strupr( roman ); // convert to uppercase for consistency
// check validity, including all invalid combinations
if( !strlen( roman )
( strspn( roman, "IVXLCDM") != strlen( roman ))
( strstr( roman, "IIII" ))
( strstr( roman, "XXXX" ))
( strstr( roman, "CCCC" ))
( strstr( roman, "MMMM"))
( strstr( roman, "IL" ))
( strstr( roman, "IC" ))
( strstr( roman, "ID" ))
( strstr( roman, "IM" ))
( strstr( roman, "XD" ))
( strstr( roman, "XM" ))
( strstr( roman, "VX" ))
( strstr( roman, "VL" ))
( strstr( roman, "VC" ))
( strstr( roman, "VD" ))
( strstr( roman, "VM" ))
( strstr( roman, "LC" ))
( strstr( roman, "LD" ))
( strstr( roman, "LM" ))
( strstr( roman, "DM" ))
( strstr( roman, "IIV" ))
( strstr( roman, "IIX" ))
( strstr( roman, "XXL" ))
( strstr( roman, "XXC" ))
( strstr( roman, "CCD" ))
( strstr( roman, "CCM" )))
{
cout << roman << " is not a valid roman number." << endl;
return( -1 );
}
// convert to decimal, in reverse order.
int c = 9, total = 0;
while( c >= 0 )
{
switch( roman[c] )
{
case('I'): decimal[c] = 1; break;
case('V'): decimal[c] = 5; break;
case('X'): decimal[c] = 10; break;
case('L'): decimal[c] = 50; break;
case('C'): decimal[c] = 100; break;
case('D'): decimal[c] = 500; break;
case('M'): decimal[c] = 1000; break;
}
if( c < 9 ) // subtraction required?
if( decimal[c] < decimal[c+1] )
decimal[c] *= (-1); // negate
// update total.
total += decimal[c--];
}
cout << "Roman " << roman << " is decimal " << total << endl;
return( 0 );
}
What do you mean by operator precedence and associativity?
Calculate y in the following equation: y = 2 + 3 x 4 What answer did you get? Did you get 20? If you did, you messed up. You should have gotten 14. If you got 20, you did the operations in the order in which they appear, from left to right, which is a mistake. You added 2 and 3, to get 5, and then you multiplied by 4, to get 20. But you should have multiplied 3 by 4, first, to get 12, and added that product to 2, to get 14. That's because multiplication (and division) take precedent over addition (and subtraction). Now, try this: y = (2 + 3) x 4 Did you get 20? If so, you got the right answer. Do you know why?
Prime number program in C using recursion?
//Program to check number is prime or not using recursive function
#include<stdio.h>
#include<stdlib.h>
void prime(int num,int count)
{
if(count<num)
{
if(num%count==0)
{
printf("%d is not Prime Number\n",num);
goto exit;
}
count += 1;
if(count<num)
{
prime(num,count);
}
}
if(num==count)
{
printf("%d is a Prime Number\n",num);
}
exit:
return 0;
}
int main()
{
system("cls");
int gvar;
printf("Enter the number = ");
scanf("%d",&gvar);
prime(gvar,2);
printf("\nashokakhil@gmail.com\n");
system("PAUSE");
return 0;
}
I think this can be another solution
#include<stdio.h>
#include<conio.h>
int prime(int);
main()
{
int i=1,r;
clrscr();
r=prime(i);
if(r==1)
printf("\n\n\tNo is prime ");
getch();
}
int prime(int i)
{
int n=1,ans,flag=1;
i++;
ans=n%i;
if(ans==0)
{
printf("\t\t\n\nNo is not prime");
flag=0;
return flag;
}
if((i!=n-1)&&(n!=1))
flag=prime(i);
return flag;
}
Write a program that input a positive integer and prints a triangle using for loop?
write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?
What is the difference between for if-then and for loop?
a for loop is defined with an boolean expression to indicate when it should terminate. A for each loop iterates once for each item in a collection.
for example, "for each (book in bookshelf)" will iterate once for each book on the bookshelf, providing access to the current book.
a for loop is defined like "for (int i = 0; i<10;i++)" meaning the loop will iterate as long as the condition is true (i < 10), and will increment on each loop.
Note: there is no 'for each' loop in C language, but there is a 'foreach' in PHP.
How is a structure initialized?
In C, structures are uninitialized by default. To initialize a structure you will typically zero the memory allocated to the structure and then set specific members to specific values. If all members are non-zero, you can simply set those members rather than zero the memory first.
In C++, structures are initialized via inline initializes and/or through the class constructor.
Wap for swapping values of two variables using pointers as arguments to functions?
# include<stdio.h>
# include<conio.h>
void main()
{
clrscr();
int a,b;
printf ("Enter the first value:");
scanf ("%d",& a );
printf ("Enter the second value:");
scanf ("%d",& b );
printf ("\n\nBefor swaping the values ");
printf ("\nThe first value is %d",a);
printf ("\nThe second value is %d",b);
printf ("\n\nAfter swaping the values ");
printf ("\nThe first value is %d",b);
printf ("\nThe second value is %d",a);
}
Writ a program in c to display day of the week using in switch case?
/* write a program to print Days of Week using switch-case structure */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter Day of weak as Number 1 to 7 ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n MONDAY ");
case 2:
printf("\n TUESDAY");
case 3:
printf("\n WEDNESDAY");
case 4:
printf("\n THURSDAY");
case 5:
printf("\n FRIDAY");
case 6:
printf("\n SATURDAY");
case 7:
printf("\n SUNDAY");
default :
printf("\n no operation is required");
}
getch();
}
Definition of loop and its types and programs in c plus plus?
Executing a segment of a program repeatedly by introducing a counter and later testing it using the if statement.
A sequence of statements are executed until some conditions for termination of the loop are satisfied.
A Program loop consists of two segments:
1.Body of the loop
2. Control Statement
Depending on the position of the control statement in the loop, a control strcture may be classifies either as the 2:
How do you install c language in win 7?
How to download and install Turbo C++ in Windows 7....
1.Download dosbox 0.74 from dosbox.com and save in E: drive(say).
2.Download Turbo C++ 3.0.
3.Create a folder Turbo in E: drive & save all contents of Turbo C++ 3.0.
4.Open dosbox.
5.Type as follows:-
mount e e:\ <Press enter>
e: <Press enter>
cd turbo <Press enter>
install install <Press enter>
6.Proceed as per requirements.
7.A folder TC appears in E: drive.
8.To repeat the point 5. commands whenever you open the dosbox, go to E:\Dosbox-0.74\Dosbox-0.74 Options
9.Scroll down to the end of the page and write as follows:-
mount E E:\
E:
CD TC
CD BIN
TC.EXE
10.Now open dosbox by clicking its icon on desktop and find to open Turbo C++ directly, but in a small dialob box.
Why 3rd generation language is a level language?
•Much more portable than low level languages (can be transferred over different computers) •Many tutorials and manual for the languages
•Many prewritten and tested algorithms made (no need to "reinvent the wheel")
•Excellent for general purpose programming
Which command is used to skip the rest of a loop and carry on from the top of the loop again?
From inside any loop statement, the continue; statement will skip any remaining statements and re-evaluate the loop's conditional expression. If that expression remains true, a new iteration of the loop begins, otherwise control passes to the statement that follows the loop. Note that in a for or while loop, the conditional expression is defined before the loop body but in a do loop it is defined after the loop body.
What significance is attached to the name main?
main refers to the entry point of an application. All programs must have a main() function that returns an integer to the calling program or script. The return value can be used for any purpose, but generally a non-zero negative value is used to indicate that an error occurred (zero meaning no error).
Why high level language is slower then assembly language?
Programs written in a high level language might be slower than ones written in Assembly language; but it is not always so, it is very easy to write un-effective programs in Assembly.
What is the return value of getch?
getch(); is used for unbuffered input. e.x:
int main()
{
char num=0;
printf("Press a keyboard button: ");
num = getch(); //This brings in 1 character that the user pressed on the keyboard
printf("\nYou pressed: %c", num); //This prints the character you pressed
getchar(); // I am using getchar(); to stop the program from ending after pressing buttons
return 0;
}
My input will be within the ().
output:
Press a keyboard button: (v)
You pressed: v
EOP //End of program
I hope this has helped you!
Can the program counter be eliminated by using the top of the stack as a program count?
No. The program counter must be stored in a dedicated register. The stack is in working memory and you cannot operate on working memory; all values must be moved into a register in order to operate upon them. It makes no sense to move a program counter in and out of memory unless performing a context switch and you can't use a stack for context switching; a priority queue must be used for this. Keep in mind that the address of the top of the stack has to be moved in and out of its register during a context switch. It doesn't make sense to load the stack register from a priority queue before you can determine where the program counter value is. It's easier to keep all state information in the same place in the priority queue where it belongs.
How do you write a c program to find out whether the given input string is an identifier or not?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,flag=0,m;
char s[5][10]={"if","else","goto","continue","return"},st[10];
clrscr();
printf("\n enter the string");
gets(st);
for(i=0;i<5;i++)
{
m=strcmp(st,s[i]);
if(m==0)
flag=1;
}
if(flag==0)
printf("\n it is not keyword");
else
printf("\n it is a keyword");
getch();
}
C language program to print Pascal Triangle?
#include <iostream.h>
double fact(double n)
{
return (n > 1) ? n * fact(n - 1) : 1;
}
double ncr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int main()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j <= i; j++)
cout <<ncr(i, j) << ' '; cout << endl;
}
return 0;
}
How do you write an algorithm to find the number of permutations or combinations?
Permutations and combinations are two separate things. Although we often use them interchangeably in English, we need a more precise definition in mathematics, such that ABC and CBA are regarded as being two different permutations of the same combination. In other words, the order of the elements is important in a permutation but is completely irrelevant in a combination.
First we have to define what it means to create a combination or permutation. Typically we have a set from which we must make a subset. The number of elements in the set is typically defined using the variable n while the number of elements in the subset is r. Thus we can formally define a permutation mathematically using the function P(n,r) and a combination as C(n,r).
We must also consider whether elements may be repeated within a combination or a permutation. For instance, when selecting numbers for a lottery, no number may be repeated in any combination but in a 4-digit combination lock, any digit may be repeated in a permutation.
Note that a combination lock is really a permutation lock in mathematics and is the perverse way of remembering the mathematical difference between a combination and a permutation.
Thus we have 4 possible variations to cater for. In order of difficulty, they are:
Let's deal with them one at a time.
1. Permutations with repetition
To calculate P(n,r) with repetitions, for every selection, r, there are always n possibilities, thus we have n^r permutations.
In a 3-digit combination lock, each digit has ten possibilities, 0 through 9, so there are 10^3=10x10x10=1000 permutations. This stands to reason because the permutations form all of the numeric values from 000 through to 999, which is 1000 different values.
In C, we can write this function as:
unsigned long long permutations (unsigned long long n, unsigned long long r) {
return pow(n,r); /* use standard library function */
}
2. Permutations without repetition
To calculate P(n,r) without repetitions we must reduce the set by one element each time we make a selection. If we go back to our 3-digit combination lock, we have 10 choices for the first digit which leaves 9 choices for the next and 8 for the next. So instead of 10x10x10=1000 permutations we only have 10x9x8=720 permutations.
Although fairly simple to work out in this case, we need a formula that is generalised to cater for all cases, just as n^r works for all permutations with repetitions.
We can see that 10x9x8 is the initial product of 10! (factorial 10) which is 10x9x8x7x6x5x4x3x2x1. So we need a formula that ignores everything after the 8. The portion after the 8 is 7x6x5x4x3x2x1 which is 7! and we can calculate that from (n-r)!=(10-3)!=7!
Having determined the portion we need to ignore, the rules of multiplication and division state that if we multiply by x and subsequently divide by x, then the two x's must cancel each other out. Thus we get:
10!/7!=(10x9x8x7!)/7!=10x9x8=720
Using formal notation, P(n,r) without repetition is therefore n!/(n-r)!
In C, we must first write a function to calculate factorials:
unsigned long long factorial (unsigned long long n) {
return (n>1)?factorial(n-1):1; /* recursive function */
}
With that in place, we can now write a function to handle permutations without repetitions:
unsigned long long permutations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/factorial(n-r);
}
3. Combinations without repetition
C(n,r) without repetition is simply an extension of P(n,r) without repetition. Every combination of r has r! permutations, so if we divide P(n,r) by r! we will get C(n,r). Expressing this formally, C(n,r) without repetition is n!/((n-r)!r!)
Going back to our 3-digits from 10, there are 10!/((10-3)!3!)=10!/(7!3!)=(10x9x8x7!)/(7!3!)=(10x9x8)/3!=720/6=120 combinations without repetition.
Using the factorial function shown above, we can write a C function to handle combinations without repetition:
unsigned long long combinations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/(factorial(n-r)*factorial(r));
}
4. Combinations with repetition
Combinations with repetition is the hardest concept to wrap your head around.
Going back to our 3-digits from 10, let's begin enumerating all the combinations so we can verify the answer at the end. We start by enumerating all those that combinations that begin with a 0:
000, 001, 002, 003, 004, 005, 006, 007, 008, 009
011, 012, 013, 014, 015, 016, 017, 018, 019
022, 023, 024, 025, 026, 027, 028, 029
033, 034, 035, 036, 037, 038, 039
044, 045, 046, 047, 048, 049
055, 056, 057, 058, 059
066, 067, 068, 069
077, 078, 079
088, 089
099
Note that there is no 010 because it is a permutation of 001. Similarly with 021 which is a permutation of 012. As a result of this, each row has one less combination than the one above. Thus there are 10+9+8+7+6+5+4+3+2+1=55 combinations.
If we now enumerate all those that begin with a 1, we see a similar pattern emerges:
111, 112, 113, 114, 115, 116, 117, 118, 119
122, 123, 124, 125, 126, 127, 128, 129
133, 134, 135, 136, 137, 138, 139
144, 145, 146, 147, 148, 149
155, 156, 157, 158, 159
166, 167, 168, 169
177, 178, 179
188, 189
199
This time we have 9+8+7+6+5+4+3+2+1=45 combinations.
Following the same logic, the next section must have 8+7+6+5+4+3+2+1=36 combinations, followed by 28, 21, 15, 10, 6, 3 and finally 1. Thus there are 220 combinations in total.
The formula to work this out is quite complex, however it becomes simpler when we look at the problem in a different way. Suppose we have 10 boxes and each box holds at least 3 of the same digit. We can number these boxes 0 through 9 according to those digits. Let us also suppose that we can only move in one direction, from box 0 to box 9, and we must stop at every box along the way. This means we must make 9 transitions from one box to the next.
While we along the row, we carry a tray with 3 slots. Whenever we stop at a box (including box 0 where we start from) we can either pick a number from the box or we can move onto the next box. if we pick a number, we place it in the first slot. We can then pick another or we can move on. When we have filled all the slots, we simply move on until we reach box 9. If we reach box 9 and still have slots available, we must pick as many 9s as we need to fill the remaining slots.
It probably sounds far more complex than it really is. By imagining a selection being done this way we can create a convenient binary notation. For instance, if we say that 1 means pick a number and 0 means move onto the next box, the sequence 101010000000 would tell us we selected the combination 123 while the sequence 000000000111 tells us we selected 999. Every combination is therefore reduced to 12-bit value containing exactly three 1s and nine 0s, and it is these specific combinations we are actually looking for.
C(n,r) with repetition is formally expressed as (r+n-1)!/(r!(n-1)!)
If we plug in the actual numbers we find:
=(3+10-1)!/(3!(10-1)!)
=12!/(3!9!)
=(12x11x10x9!)/(3!9!)
=(12x11x10)/3!
=1320/(3x2x1)
=1320/6
=220 combinations with repetition.
This type of problem might be expressed in other ways. For example, how many different ways can we fill a box with 100 sweets from 30 different sweets. C(n,r) is C(30,100) thus we find:
=(100+30-1)!/(100!(30-1)!)
=129!/(100!29!)
=(129x128x127x...x101x100!)/(100!29!)
=(129x128x127x...x101)/29!
=5.3302324527079900778691094496787e+59/8,841,761,993,739,701,954,543,616,000,000
=60,284,731,216,266,553,294,577,246,880 combinations with repetition.
In C we can use the following function in conjunction with the factorial function shown earlier:
unsigned long long combinations (unsigned long long n, unsigned long long r) {
return factorial(n+r-1)/(factorial(r)*factorial(n-1));
}
We might also have similar problems with an additional restriction. For instance, we might be asked to select 100 sweets from 30 different sweets selecting at least 1 of each type. This reduces the number of slots to 100-30=70 but we have the same number of transitions, so we get:
=(70+30-1)!/(100!(30-1)!)
=99!/(70!29!)
=(99x98x97x...x71x70!)/(70!29!)
=(99x98x97x...x71)/29!
=7.7910971370578048745872324992773e+55/8,841,761,993,739,701,954,543,616,000,000
=8,811,701,946,483,283,447,189,128 combinations with repetition.
To accommodate this caveat, we can use the following function instead:
unsigned long long combinations2 (unsigned long long n, unsigned long long r) {
return factorial(n-1)/(factorial(r)*factorial(n-1));
}
Pre increment and post increment?
Both increment the value of the variable by one. The difference is the value of the increments expression itself. With preincrement value is taken after incrementing, and with postincrement value is taken before incrementing.
Example:
Let x have value 5.
y = ++x;
Both y and x are assigned value 6.
Again let x have value 5.
y = x++;
y is assigned value 5. x is assigned value 6.
extern "C"
{
int printf(const char *format,..);
}
int main()
{
printf("Hello world");
}
coz all the include statement does is copy the requested file at the asked location.