Writing a program using switch statement


Question1)
a) Convert the expression 4-5/6+(2/3-8)}/8*3 to RPN.

b) What is the output of the following program? Explain step by step the procedure.
#include

int
main()
{
Int row=5,column;
while(row>=1)
{
column=1;
while(column<=5)
{
printf("%s",row%2 ? "<" : ">");
++column;
} --row;
printf("\n");}
return0;}

Instead of using nested while loops, if you want to use nested for loops, what changes are required in the above program so that the output remains same ?

c) Write a program using switch statement which takes two integers and an operator from the keyboard. If the operator entered through the keyboard is any one of the operators { + ,-, * ,/ }, then the program performs the operation and print the result. If the operator entered is other than the mentioned above, the program prints the string “Operation cannot be performed”. Your program should also take care of the division by 0.

Question4) Assume the following declarations

Int a[5] ={1,2,3,4,5};
Int *ptr_a,*ptr=NULL;
Int number ,i;
ptr_a=&a;

Find errors in the following statements if any and do the necessary corrections.

i) ++ptr_a;

ii) number=ptr_a;

iii) number=*ptr_a[2];

iv) for(i=0;i<=5;i++)
printf("%d",ptr_a[i]);

v) number=*ptr;

c) Define a function isprime ( ) which takes a positive integer and returns 1 if the number is prime and 0 otherwise. Use this function to define another function called num_of_primes ( ) which takes  two natural numbers p and q(where p < q) and returns the number of primes between them.

d) Define a macro to find the minimum of two numbers. Using this define a function minimum ( ) which takes four numbers as parameters and returns the minimum.

e) Arrange the following operators in ascending order of priority. Tell associativity for the operators who have same priority.

                     <   *   >  ||  []   +=  !=     +      ++

Question5) a) Write a function with the following prototype int delete_elements (int *list[ ], int last_index, int p, int q) which deletes all the elements of a list from index p to q (where p < q ) and returns the last_index of the updated list . Also write the main function to test your function.

b) Write statements to accomplish each of the following. Assume the structure

struct person

{

char lastName[10];

char firstName[10];

int Age;

};

has been defined and that the file is already open for writing.

i) Initialize the file “personaldata.dat” so that there are 100 records with lastName=“ ”, firstName =“ ” and Age = 0.

ii) Input 10 last names, first names and ages and write them to the file.

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Writing a program using switch statement
Reference No:- TGS02586

Now Priced at $40 (50% Discount)

Recommended (94%)

Rated (4.6/5)