next up previous
Next: About this document ...

Ceng 198 Introduction to Programming
Final
May 28, 2008 09.00 - 10.50
Good Luck!
  1. (40 pts) Write a complete C program that makes a call to a function that will take an array of size 250 as an argument.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #define SIZE 250
    void myfunction(int arrayinfunction[], int size);
    int main()
    {
      int arrayinmain[SIZE];
      myfunction(arrayinmain, SIZE);
      return 0;
    }
    void myfunction(int arrayinfunction[], int size)
    {
      int i, min, max, minIndex, maxIndex;
      float average, sum, sigma;
      srand(time(0));
      for (i = 0; i < size; i++)
        arrayinfunction[i] =1+rand()%100;
      for (i = 0; i < size; i++)
        printf("number=%d index=%d \n",arrayinfunction[i],i);
      /*Maximum and corresponding index*/
      max = arrayinfunction[0];
      for (i = 1; i < size; i++)
        if (arrayinfunction[i] > max)
          {
    	max = arrayinfunction[i];
    	maxIndex = i;
          }
      printf("Max. Number: %d, Index of Max. Number: %d\n", max, maxIndex);
      /*Minimum and corresponding index*/
      min = arrayinfunction[0];
      for (i = 1; i < size; i++)
        if (arrayinfunction[i] < min)
          {
    	min = arrayinfunction[i];
    	minIndex = i;
          }
      printf("Min. Number: %d, Index of Min. Number: %d\n", min, minIndex);
      /*Arithmetic Mean*/
      sum=0.0;
      for (i = 0; i < size; i++)
        sum=sum+arrayinfunction[i];
      average=sum/size;
      printf("Arithmetic mean (average) of %d numbers is %f \n", size, average);
      /*Standart Deviation*/
      sum=0.0;
      for (i = 0; i < size; i++)
        sum=sum+(arrayinfunction[i]-average)*(arrayinfunction[i]-average);
      sigma=sqrt(sum/size);
      printf("Standart deviation is %f \n", sigma);
      return;
    }
    
  2. (25 pts) Define a function with the name ```Calculator''
    #include <stdio.h>
    float simpleCalculator(float, char, float);
    int main()
    {
      float firstOp, secondOp,result;
      char oper;
      printf("Enter operator (+,-,*, or /): ");
      scanf("%c", &oper);  
      printf("Enter first operand: ");
      scanf("%f", &firstOp);
      printf("Enter second operand: ");
      scanf("%f", &secondOp);
      result = simpleCalculator(firstOp, oper, secondOp);
      printf("Result of the operation %f %c %f is %f\n", firstOp, oper, secondOp,
              result);
      return 0;
    }
    float simpleCalculator(float fo, char op, float so)
    {
      if(op == '+')
        return fo + so;
      else if(op == '-')
        return fo - so;
      else if(op == '*')
        return fo * so;
      return fo / so;
    }
    
  3. (25 pts) Write a complete C program that makes a call to a function
    #include <stdio.h>
    void printcharacter();
    int main()
    {
      printcharacter();
      return 0;
    }
    void printcharacter()
    {
      int i,j,integer1, integer2;
      char character;
      printf("Enter character : ");
      scanf("%c", &character);
      printf("Enter first integer: ");
      scanf("%d", &integer1);
      printf("Enter second integer: ");
      scanf("%d", &integer2);
      for(i=1;i<=integer2;i++)
        {
          for(j=1;j<=integer1;j++)
    	printf("%c",character);
          printf("\n");
        }  
      return;
    }
    
  4. (20 pts) 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-06-09