Exam 2 Answers

 

  1.      int sameSum(float one[], float two[], int size) {
            float sumOne = 0;
            float sumTwo = 0;
            int i;
            for (i = 0 ; i < size ; i++) {
               sumOne = sumOne + one[i];
               sumTwo = sumTwo + two[i];
            }
            if (sumOne == sumTwo)
               return 1;
            else
               return 0;
         }
    
  2.      int sameSum(float[], float[], int);
         int main() {
            float first[50];
            float second[50];
            int flag;
            int i;
            for (i = 0 ; i < 40 ; i++) {
               printf("enter a number: ");
               scanf("%f", &first[i]);
            }
            for (i = 0 ; i < 40 ; i++) {
               printf("enter a number: ");
               scanf("%f", &second[i]);
            }
            flag = sameSum(first, second, 40);
            if (flag)
               printf("the sums are the same\n");
            else
               printf("the sums are different\n");
         }
    
    1. 74458
    2. 100
    3. s = &nn;
    4. 74462
    5. 101
    6. 75
    7. 74466
    8. *s = *s + 1;
    9. r = r + 3;
  3.    third fourth (12)
       r i
       less!
       firstPsecondthird fourth
    
  4.   int change(char* str) {
         int i = 0;
         int numMod = 0;
         while (*(str+i) != '\0') {
            if (*(str+i) < 'a' || *(str+i) > 'z') {
               *(str+i) = '#';
               numMod++;
            }
            i++;
         }
         return numMod;
      }
    
  5.    char str[81];
       puts("enter a string");
       gets(str);
       int num = change(str);
       printf("your string is: %s (%d chars replaced)\n", str, num);
    
  6.    int main() {
          char strings[20][51];
          FILE *pfile = fopen("poem.dat", "r");
          int i;
          int numstr = 0;
          int numrep;
          while (fgets(strings[i], 51, pfile) != NULL)
             numstr++;
          for (i = 0 ; i < numstr ; i++) {
             numrep = change(strings[i]);
             printf("your string is: %s (%d chars replaced)\n", str, numrep);
          }
       }
    


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2023