Variables, Data Types, Constants and Operators


  1. What is wrong with this program?

    
    main()
    {
    	p=47;
    	printf("%d\n",p);
    }
    

  2. What is wrong with this declaration?

    
    int x = y = 95;
    

  3. Write four different C statements that subtract 1 from the integer variable x.

  4. Which of the following are valid C statements? Evaluate the ones which are valid.
    • x = 65/5*2
    • 65 / 4 = k;
    • p = 65(4*7);
    • x = 34 - 4 * 7;
    • x = 34 - ( 4 * 7);
    • 54 % 5 = h ;
    • x = 54 % 7;


Answers

	




































    1. The #include <stdio.h> is missing and the variable p has not been declared.

    2. The equals sign can only be used in a declaration to initialise a variable. Therefore it should be
      
      int	x=95, y =95;
      

    3. x = x - 1;
      x --;
      --x;
      x -= 1;
      

      • missing ;
      • not valid - should be k = 64/4;
      • not valid
      • valid x = 6
      • valid
      • not valid
      • valid x = 5


    Back to Questions Back to Main Page for Lecture 2

    
    
    
    
    
    
    
    22.5HR1 Control Constructs Quick Check
     
    
    

    Control Constructs


    1. What is wrong with the following?
      
      int x;
      if (x=1)
      	x++;
      

    2. What is wrong with the following?
      
      int x, y, z;
      
      if (x > y && < z)
      	x = y;
      

    3. Write an extract of code to test if the variable x is a positive number?

    4. Write an extract to test if the variable x is a positive and even number?


    Answers

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    1. The test should be x== 1, with x=1 this will assign the value of 1 to x rather than checking if x is a 1.

    2. Assuming that it is intended to compare z to z, the test should be x > y && x < z

    3.  if (x > 0)
      

    4.  if (x > 0 && x%2 == 0)
      


    Back to Questions Back to Main Page for Lecture 3

    
    
    
    
    
    
    
    22.5HR1 Iteration Quick Check
     
    
    

    Iteration


    1. Change to following code to a while loop.
      
      int	x, y;
      for (x=1; x < 57; x+= 2)
      	y = x;
      

    2. What is wrong with the following?
      
      int	n=0;
      while (n < 100)
      	value = n*n;
      

    3. Write a while loop that calculates the sum of all numbers between 0 and 20 inclusive?

    4. Convert the above loop to a do ... while loop.

    5. Convert the above loop to a for loop.

    6. What is wrong with the following?
      
      i = 1;
      while (i <= 10 )	
      	printf("%d\n", i);
      	i++;
      


    Answers

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    1. 
      x = 1;
      while (x < 57)
      {
      	y = x;
      	x += 2;
      }
      
    2. The value of n within the loop is never incremented. It will therefore always have a value of 0, which is less than 100, and hence this will produce an infinite loop which will never terminate. The program will therefore never end.

    3. int	i, sum =0;
      i = 0;
      while (i <= 100)
      {
      	sum += i;
      	i++;
      }
      
    4. int	i, sum =0;
      i = 0;
      do
      {
      	sum += i;
      	i++;
      }while (i <= 100);
      
    5. int	i, sum =0;
      for (i=0; i <= 100, i++)
      {
      	sum += i;
      }
      
    6. There are no braces around the printf() and increment i statements, therefore it will execute only the printf() statement within the loop, and an infinite loop will again occur.


      Back to Questions Back to Main Page for Lecture 4

      
      
      
      
      
      
      
      22.5HR1 Functions Quick Check
       
      
      

      Functions


      1. Explain the difference between a function declaration and a functions definition? Where is the function declaration placed?

      2. Given the following function declarations, explain the meaning of each.
        1. int function1(int a, float b);
        2. void function2(float a, float b, float c);
        3. int function3(void);
        4. float function4(float x);
        5. void function5(void);

      3. Write an appropriate function call for each of the function declarations given above.

      4. What does the following function do?
        float function(float a, float b, float c)
        {
        	float tmp;
        
        	tmp = (a + b + c )/ 3.0 ;
        
        	return tmp;
        }
        

      5. Give an example function call for the above function.

      6. Give the function declaration for the function given in question 4.


      Answers


      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      


      1. The declaration is contained within the program before the function is called. It is usually outside any function, including the main program. It contains details of the function name, the return type and the types of the function arguments.

        The definition contains the actual code of what is to be implemented when the function is called.

        1. The function is sent an integer argument which it will refer to as a within the function and a floating point variable, which it will refer to as b within the function. The function will return an integer quantity.
        2. The function is sent 3 floating point arguments and returns no value to the calling program.
        3. The function is sent no arguments but returns an integer.
        4. The function is sent one floating point argument and returns a floating point argument.
        5. The function is sent no variable and returns nothing.

      2. Example calls are given below,
        1. int x, z;
          float y;
          
          /* x and y are assigned some values in the program */
          
          z = function1(x, y);
          
        2. 
          float	x,y,z;
          
          /* x, y and z assigned values in program */
          
          function2(x, y, z);
          
        3. int a;
          
          a = function3();
          
        4. float a, b;
          
          /* b assigned a value in program */
          a = function4(b);
          
        5. function5();
          

      3. It returns the average value of the three values sent to it.

      4. x = function(a,b,c);

      5. float function(float, float, float); or float function(float a, float b, float c);


      Back to Questions Back to Main Page for Lecture 6