next up previous
Next: About this document ...

Ceng 198 Introduction to Programming
Midterm
April 03, 2008 18.00 - 19.50
Good Luck!
Each question is 25 pts.
  1. Write a program in order to play the game “Game of Guessing”.
    Sample Run: (Let’s say the computer generates a number: 55)
    I have a number, try to guess it: 43
    Too low, a little bit higher please: 70
    Too high, a little bit lower please: 60
    Too high, a little bit lower please: 50
    Too low, a little bit higher please: 55
    You found the number! Congratulations!
    Number of tries = 5
    
    Hints: To generate a random number include the libraries;
    #include<stdlib.h> 
    #include<time.h>
    
    And add the following code to your main program;
    srand(time(0));
    number = rand()%XXX
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main(void)
    {
      int guess, number, counter;
      srand(time(0));
      number=1+rand()%99;
    //   printf("number=%d\n",number);
    //   number=55;
      printf("I have a number, try to guess it: ");
      scanf(" %d",&guess);
      counter=1;
      while(guess != number)
        {
          if (guess < number)
    	printf("Too low, a little bit higher please: ");
          else if (guess > number)
    	printf("Too high, a little bit lower please: ");
          scanf(" %d",&guess);
          ++counter;
        }
      printf("You found the number! Congratulations!\n");
      printf("Number of tries = %d\n",counter);
      return 0;
    }
    
  2. One large chemical company pays its salespeople on a commission basis. The salespeople receive $200 per week Develop a complete C program that uses any of the repetitive structures to input each salesperson’s gross sales for last week and calculate and display that salesperson’s earnings.
    Sample Run:
    Enter sales in dollars (-1 to end): 5000
    Salary is: $800.00
    
    #include<stdio.h>
    int main(void)
    {
      float sales, earning, base=200.0;
      printf("Enter sales in dollars (-1 to end) : ");
      scanf(" %f",&sales);
      //   printf("sales = %f \n",sales);
      while(sales != -1)
        {
          if (sales <= 2500)
    	earning=base+sales*8/100;
          else if (sales > 2500)
    	earning=base+sales*12/100;
          printf("Salary is $%.2f \n",earning);
          printf("Enter sales in dollars (-1 to end) : ");
          scanf(" %f",&sales);
          //   printf("sales = %f \n",sales);
        }
      return 0;
    }
    
  3. What will be the output of the following program fragment?
        for( sum=0, i=2; i <= 8; i +=2)
        {
          j=i;
          while(j < 4)
          {
              k=j;
              do
              {
                  sum++;
                  k+=2;
              }while(k <= 3);
              j++;
          }
         printf(" %d %d %d %d \n", sum, i, j, k);
        }
    
    #include<stdio.h>
    int main()
    {
      int sum,i,j,k;
      
      for( sum=0, i=2; i <= 8; i +=2)
        {
          j=i;
          while(j < 4)
            {
              k=j;
              do
                {
                  sum++;
                  k+=2;
                }while(k <= 3);
              j++;
            }
          printf(" %d %d %d %d \n", sum, i, j, k);
        }   
      return 0;
    }
     2 2 4 5
     2 4 4 5
     2 6 6 5
     2 8 8 5
    
  4. A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a complete C program that reads in a five-digit integer and determines whether it is a palindrome.
    Hints:
    #include<stdio.h>
    int main(void)
    {
      int i,number, onlar=10,a,b,c,d,e;
      printf("Enter a five-digit integer : ");
      scanf(" %d",&number);
      //   printf("number = %d \n",number);
      a=number%onlar;
      number=number/onlar;
      b=number%onlar;
      number=number/onlar;
      c=number%onlar;
      number=number/onlar;
      d=number%onlar;
      number=number/onlar;
      e=number%onlar;
      printf("a=%d b=%d c=%d d=%d e=%d \n",a,b,c,d,e);
      if(a==e && b==d)  
        printf("Entered number %d%d%d%d%d is a palindrome\n",e,d,c,b,a); 
      else 
        printf("Entered number %d%d%d%d%d is NOT a palindrome\n",e,d,c,b,a);
      return 0;
    }
    



next up previous
Next: About this document ...
Cem Ozdogan 2008-05-22