Saturday 8 December 2012

(a) Z =     (a + b ) 2 / c - 0.5 + 2 a / (q + r)

                           ( a + b) * ( 1 / m)

          Answer

 Z = (( 8.8 * ( a + b ) * 2 / c ) - ( 0.5 + 2 * a / ( q + r ))) / (( a + 5 ) * ( 1 / m))

(b)  X =    - b + ( b * b ) + 2 4ac
                            2a


        Answer

X  = ( - b + ( b * b ) + 2 * 4 * a * c ) / ( 2 * a )

(c)  R =      2v + 6.22 ( c + d )
                          g + v 

      Answer

R = ( 2 * v + 6.22 * ( c + d )) / ( g + v )

(d)  A =    7.7b ( xy + a ) / c - 0.8 + 2b
                         ( x + a ) ( l / y ) 

        Answer

A = (( 7.7 * b ) * ( x * y + a ) / c - 0.8 + 2 * b ) / (( x + a ) * ( 1 / y ))

[E]  What would be the output of the following programs:

(a)     main ( )
            {
                   int i = 2 , j = 3 , k , l ;
                   float  a , b ;
                   k = i / j *  j ;
                   l = j / i * i ;
                  a = i / j * j ;
                  b = j / i * i ;
                  printf ( " %d %d %f %f " , k , l , a , b ) ;
                             }

                        Output

                           0 2 0. 000000   2.000000  


(b)     main ( )
            {
                    int a , b ;
                    a = -3 - -3 ;
                    b = -3 - - ( - 3 ) ;
                    printf ( * a = %d b = %d* , a , b ) ;
             }
                      
                        Output

                         a = 0  b = -6


(c)      main ( )
             {
                     float a = 5, b = 2 ;
                     int c ;
                     c = a % b ;
                     printf ( "%d" , c ) ;
              }

                        Output

                         Error. Mod ( % ) operator cannot be used on floats


(d)     main ( )
            {
                       printf ( "nn \n\n nn\n" ) ;
                       printf ( "nn /n/n nn/n" ) ;
             }

                        Output

                        nn
                        nn /n/n nn/n


(e)      main ( )
             {
                        int a , b ;
                        printf ( "Enter values of a and b" ) ;
                        scanf ( " %d %d " , &a , &b ) ;
                        printf ( " a = %d b = %d" , a , b ) ;
               }

                         Output

     "Since spaces are given after and before double quotes in scanf ( ) it waits even after enterning two values. We must enter one more value to complete scanf ( ) execution."
     


[F]   Write C programs for the following


(a)  Zain's basic salary is input through the keyboard. His dearness allowance is 40% of basic salary , and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
      
             Program:

/* To calculate gross salary of Zain */

#inculde <stdio.h>
#inculde <conio.h>

       main ( )
        {
                float bp , da , hra , grpay ;

               clrscr ( ) ; /* Clears Screen */
               printf ( *\n Enter the Basic Pay of Zain : * ) ;
               scanf ( * "%f" , &bp ) ;
               da = 0.4 * bp ;
               hra = 0.2 * bp ;
               grapy = bp + da + hra ; /* Gross Pay = sum of basic & all allowances */
                      
               printf ( "\nBasic Pay of Zain = %f" , bp ) ;
               printf ( "\nDearness Allowance = %f" , da ) ;
               printf ( "\nHouse Rent Allowance = %f" , hra ) ;
               printf ( "\nGross Pay of Zain is %f" , grapy ) ;

               printf ( *\n\n\n\n\nPress any key to exit...* ) ;
               getch ( ) ; /* reads a characteer from keyboard */
                 
           }

(b)  The distance between two cities (in km) is input through the keyboard. Write a program to convert and print this distance in meter , feet , inches and centimeters.

                 Program :

/* Conversion  of distance */

#inculde (stdio.h>
#inculde <conio.h>

       main ( )
        {
                  float km , m , cm , ft , inch ;

                  clrscr ( ) ;
                  printf ( "\nEnter the distance in kilometers :" ) ;
                  scanf ( "%f" , &km ) ;
         
                  m = km * 1000 ;
                  cm = m * 100 ;
                  inch = cm / 2.54 ;
                  ft = inch / 12 ;

                  printf ( "\nDistance in meters = %f" , m ) ;
                  printf ( "\nDistance in  centimeters = %f" , cm ) ;
                  printf ( "\nDistance in feet = %f" , ft ) ;
                  printf ( "\nDistance in inches = %f" , inch ) ;

                  printf ( "\n\n\n\n\n Press any key to exit....." ) ;
                  getch ( ) ;
        }


(c)  If the mark obtained by a student in five different subjects are input through the keyboard , find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.

             Program :

/* Calculation of aggregate & percentage marks */ 

#inculde <stdio.h>
#inculde <conio.h>

   main ( )
      {

               int m1 , m2 , m3 , m4 , m5 , aggr ;
               float per ;

              clrscr ( ) ;
              printf ( "\nEnter marks in 5 subjects :" ) ;
              scanf ( "%d %d %d %d %d" , &m1 , &m2 , &m3 , &m4 , &m5 ) ;

              aggr = m1 + m2 + m3 + m4 + m5 ;
              per = aggr /  5 ;

             printf ("\nAggregate Marks = %d " , aggr ) ;
             printf ( "\nPercentage Marks = %f " , per ) ;


              printf ("\n\n\n\n\n Press any key to exit.....") ;
               getch ( ) ;
     }

(d)  Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into centigrade degrees.

                    Program :

/* Conversion of temperature from Fahrenheit to Centigrade */

#inculde <stdio.h>
#inculde <conio.h>

      main  ( )
         {
                 float fr , cent ;

                 clrscr ( ) ;
                 printf ( *\nEnter the temperature (F) : * ) ;
                 scanf ( " %f " , & fr ) ;

                 cent = 5.0 / 9.0 * ( fr - 32 ) ;
                 printf ( " \n Temperature in centigrade = %f " , cent ) ;

                 printf " \n\n\n\n\n Press any key to exit....* ) ;
                 getch ( ) ;
            }

(e)  The length & breadth of rentangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area &circumference of the circle.

        Program: 

/* Calculation of perimeter & area of rectangle and circle */

#inculde<stdio.h>
#inculde<conio.h>

        main ( )
           {
                      int l , b , r , area1 , perimeter ;
                      float area2 , circum ;
                      
                      clrscr ( ) ;
                      printf  ("\nEnter Length & Breadth of Rectangle ") ;
                      scanf ( "%d %d" , &l , &b ) ;
                      area1 = l * b ; /* Area of a rectangle */
                      perimeter = 2 * 1 + 2 * b ; /* Perimeter of a rectangle */

                      printf ( "\nArea of Rectangle = %d" , area1 ) ;
                      printf ( "\nPerimeter of Rectangle = %d " , perimeter ) ;

                      printf ("\n\nEnter Radius of circle " ) ;
                      scanf (" %d " , &r ) ;

                      area2 = 3.14 * r *r ; /* Area of Circle */
                      circum = 2 * 3.14 * r ; /* Circumference of circle */

                      printf ( "Area of Circle = %f " , area2 ) ;
                      printf ( "\nCircumference of Circle = f% , circum ) ;

                      printf ( "\n\n\n\n\nPress any key to exit...") ;
                      getch ( ) ;
           
         }

(f) Two number are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

               Program :

/* Interchanging of contents of two variables c & d */

#include <stdio.h>
#inculde (conio.h>

        main ( )
            {
                       int c , d , e ;

                       clrscr ( ) ;
                       printf ( "\nEnter the number at location C: " ) ;
                       scanf ( "%d" , &c ) ;
                       printf ( "\nEnter the number at location D: " ) ;
                       scanf ( "%d" , &d ) ;

                      /* Interchange the contents of two variables using a third as temporary store */
            
                      e = c ;
                      c = d ;
                      d = e ;

                      printf ( "\nNew Number at location C = %d" , c ) ;
                      printf ( "\nNew Number at location D = %d , d ) ;

                      printf ( "\n\n\n\n\nPress any key to exit....") ;
                      getch ( ) ;
              }

(g) If a five-digit number is input through the keyboard , write a program to calculate the sum of its digits.
(Hint: Use the modulus operator '%')

            Program :

/* sum of digits of a 5 digit number */

#inculde <stdio.h>
#inculde <conio.h>

           main ( )
              {
                      int num , a , n ;
                      int sum = 0 ; /* sum initialised to zero as otherwise it will contain a garbage value */

                      clrscr ( ) ;
                      printf ( "\nEnter a 5 digit number(less than 32767)" ) ;
                      scanf ( "%d" , &num ) ;

                      a = n % 10 ; /* last digit extracted as remainder */
                      n = num/10 ; /* remaining digits */
                      sum = sum + a ; /* sum updated with addition of extracted digit "/

                      a = n % 10 ; /* 4 th digit */
                      n = n /10 ;
                      sum = sum + a ;

                      a = n % 10 ; /*3 rd digit */
                      n = n/10 ;
                      sum = sum + a ;

                      a = n % 10 ; /* 2 nd digit */
                      n = n/10 ;
                      sum = sum + a ;

                      a = n % 10 ; /* 1st digit */
                      sum = sum + a ;

                      printf ( "\n The sum of the 5 digits of %d is %d" , num , sum ) ;

                      printf ( "\n\n\n\n\nPress any key to exit...") ;
                      getch ( ) ;
            }

(h) If a five-digit number is input through the keyboard , write a program to reverse the number.

                Program:

/* To reverse the digits of a 5-digit number */

#inculde <stdio.h>
#inculde <conio.h>

          main ( )
              {
                      int n , a , b ;
                      long int revnum = 0 ;
                      / * Initialised otherwise it will contain garbage value. Declared as long as after reversing it
                      may not be in the int range */

                     clrscr ( ) ;
                     printf ( "\nEnter a five digit number (less than 32767) " ) ;
                     scanf ( "%d" , &n ) ;

                     a = n % 10 ; /* last digit */
                     a = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 10000L ; /* revnum updated with value of extracted digit */

                     a = n % 10 ; /* 4 th digit */
                     a = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 1000 ;

                     a = n % ;  /*3 rd digit */
                     n = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 100 ;
                     a = n % 10 ; /* 2 nd digit /*
                     n = n 10 ; /* remaining digits */
                     revnum = revnum + a * 10 ;
                     a = n % 10 ; /* 1 st digit */
                     revnum revnum + a ;

                     printf ( "\nThe reversed number is %d" , revnum ) ;
                     printf ( "\n\n\n\n\nPress any key to exit...") ;
                     getch ( ) ;
           }



                    

                    

Thursday 6 December 2012

(c) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;
       ( qui = 4, add = 2, god = 2, assume sto be an int )

            Answer

            s = 4 * 2 / 4 - 6 / 2 +2 / 3 * 6 / 2                 operation: *
            s = 8 / 4 - 6 / 2 + 2 / 3 * 6 / 2                      operation: /
            s = 2 - 6 / 2 + 2 / 3 * 6 / 2                           operation: /
            s = 2 - 3 + 2 / 3 * 6 / 2                                operation: /
            s = 2 - 3 + 0 * 6 / 2                                     operation: *
            s = 2 - 3 + 0 / 2                                           operation: /
            s = 2 - 3 + 0                                                operation: -
            s = - 1 + 0                                                   operation: +
            s = - 1

(d) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
       ( s = 4, g = 3 , assume to be an int )

          Answer

           s = 1 / 3 * 4 / 4 - 6 / 2 + 2 / 3 + 6 / 3         operation: /
           s = 0 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3              operation: *
           s = 0 / 4 - 6 / 2 + 2 / 3 * 6 / 3                    operation: /
           s = 0 - 6 / 2 + 2 / 3 * 6 / 3                         operation: /
           s = 0 - 3 + 2 / 3 * 6 / 3                              operation: /
           s = 0 - 3 + 0 * 6 / 3                                   operation: *
           s = 0 - 3 + 0 / 3                                         operation: /
           s = 0 - 3 + 0                                              operation: -
           s = - 3 + 0                                                 operation: +
             s = -3

Tuesday 4 December 2012

   Let Us C++ Solution

 

                                   Getting Started

 

[A]  Which of the following are invalid variable names and why?


   BASICSALARY          (valid)
   _basic                           (valid)
   basic-hra                       (invalid , we cannot use hyphen in variable name)
  #MEAN                        (invalid , variable name must begin with an alphabet or underscore)
  group                             (invalid A  is not allowed in variable name)
  422                                 (invalid A variable name cannot begin with a digit)
  population in 2012         (invalid variable name cannot contain spaces)
  over time                        (invalid variable name cannot contain spaces)
 mindovermatter             (valid)
 FLOAT                           (valid)
 Hello                              (valid)
 queue                            (invalid A is not allowed in variable name)
 team'svictory               (invalid A ',, is not allowed in variable name)
 Plot # 3                        (invalid, The '#' and spaces are not allowed in variable name)
 2012_DDay                (invalid, A variable name must with an alphabet or an underscore)
             

[B]  Point out the errors, if any, in the following C statements:


(a) int = 314.562*150;
          Error. int is a keyword hence should not be used as a variable.
(b) name = 'Ali'
          Error. 'Ali' is an invalid character constant.
(c) varchar = '3'
          No error
(d) 3.14*r*r*h = vol_of_cyl;
          Error. on the left-hand side of equal to (=) there can only be a variable.
(e) k = (a * b) ( c + (2.5a + b) (d + e) ;
          Error. Multiplication operator (*) missing among the three expressions and also between 2.5 and variable a.
(f) m_inst = rate of interest * amount in rs;
          Error. rate of interest and amount in rs are invalid variable names.
(g) si = principal * rateofinterest * numberofyear / 100;
          No error.
(h) area = 3.14 * r ** 2;
          Error. '**' is an invalid operator.
(i) volume = 3.14 * r 2 * h;
         Error. The expression 3.14 * r will result in a float. the ' ' operator cannot be used with floats.
(j) k = ((a * b) + c) ( 2.5 * a + b);
         Error. Multiplication operator ( * ) missing between the two expressions given in parentheses.
(k) a = b = 3 = 4;
         Error. A constant 3 cannot be on the left hand side of the assignment operator.
(l) count = count + 1;
         No error.
(m) date = '2 Mar 04;
         Error. Double quotes ("") should be used instead of ('') to enclose the string 2 Mar 04.

[C] Evaluate the following expressions and show their hierarchy.

 

(a) g= big / 2 + big * 4 / big - big + abc / 3;
     (abc = 2.5, big = 2, assume g to be a float)

        Answer

          g  = 2 / 2 + 2 * 4 / 2 - 2 + 2.5 / 3          operation: /
          g = 1 + 2 * 4 / 2 - 2 + 2.5 / 3                operation: *
          g = 1 + 8 / 2 - 2 + 2.5 / 3                      operation: /
          g = 1 + 4 - 2 + 2.5 / 3                           operation: /
          g = 1 + 4 - 2 + 0.83                              operation: +
          g = 5 - 2 + 0.83                                    operation: -
          g = 3 + 0.83                                         operation: +
          g = 3.83

(b) on = ink * act / 2 + 3 / 2 * act + 2 + tig ;
       (ink = 4, act = 1, tig = 3.2, assume on to be an int)

         Answer

         on =  4 * 1 / 2 + 3 / 2 * 1 + 2 + 3.2        operation: *
         on =  4 / 2 + 3 / 2 * 1 + 2 + 3.2              operation: /
         on = 2 + 3 / 2 * 1 + 2 + 3.2                    operation: /
         on = 2 + 1 * 1 + 2 + 3.2                         operation: *
         on = 2 + 1 + 2 + 3.2                               operation: +
         on = 3 + 2 + 3.2                                     operation: +
         on = 5 + 3.2                                           operation: +
           on =