Answers: Functions and Arrays

1.
   #include <stdio.h>
   int main() {
      int a[100];
      int b[100];
      int  j, m, suma = 0, sumb = 0, sumdiff = 0;

      printf("enter the number of pairs to read: ");
      scanf("%d", &m);
      printf("enter %d pairs:\n", m);
      for (j = 0 ; j < m ; j++) {
         scanf("%d", &a[j]);
         scanf("%d", &b[j]);
         suma = suma + a[j];
         sumb += b[j];
         sumdiff = sumdiff + (a[j] - b[j]);
      }
      for (j = m - 1; j >= 0; j--)
         printf("%d   %d    %d\n", a[j], b[j], (a[j]-b[j]));
      printf("%d   %d    %d\n", suma, sumb, sumdiff);
   }


2.
   int match(int one[], int two[], int size)
   {
      int i;
      int count = 0;
      for (i = 0 ; i < size ; i++)
         if (one[i] == two[i])
            count++;
      return count;
   }


3.
   int readArray(int array[], int size)
   {
      int i;
      printf("enter values for array:\n");
	  for (i = 0 ; i < size ; i++)
	     scanf("%d", &array[i]);
   }


4.
   #include <stdio.h>
   int main()
   {
      int aa[25];
      int bb[25];
      readArray(aa, 25);
      readArray(bb, 25);
      int matches = match(aa,bb,25);
      printf("number of matches: %d\n", matches);
   }


5.
   void reverse(int one[], int two[], int size)
   {
       int i;
       for (i = 0 ; i < size ; i++)
          one[i] = two[size-i-1];
   }


6.
   int first[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
   int second[12];
   reverse(second, first);
   for (i = 0 ; i < 12 ; i++)
       printf("%d\n", second[i]);


7.
   int lenstr(char str[])
   {
      int i = 0;
      while (str[i] != '\0')
         i++;
      return i;
   }

8.
   char mystr[26];
   int length;
   printf("enter a string:\n");
   fgets(mystr, 26, stdin);
   length = lenstr(mystr);
   printf("your string has %d characters\n", length);


9.
   int count(char str[], char ch)
   {
       int i;
       int thecount = 0;
       for (i = 0 ; str[i] != '\0' ; i++)
          if (str[i] == ch)
             thecount++;
       return thecount;
   }


10.
   char mystr[26];
   int pcount;
   printf("enter a string:\n");
   fgets(mystr, 26, stdin);
   pcount = count(mystr, 'p');
   printf("your string contains %d 'p's\n", pcount);


11.
   void catstr(char str1[], char str2[])
   {
       int i, j;
       // the following loop has no body - that's the meaning of the semicolon
       // after the for statement
       // this loop finds the null byte in str1. after it is done, the second
       // loop copies the characters from str2 into str1, starting by writing
       // over the null byte
       // after the second loop, the last statement adds a null byte to the end
       // of str1
       for (i = 0 ; str1[i] != '\0' ; i++);
       for (j = 0 ; str2[j] != '\0' ; i++, j++)
          str1[i] = str2[j];
       str1[i] = '\0';
   }


12.
   char first[30] = "Hi Mom!";
   char second[12] = "Nice hat!";
   catstr(first, second);
   printf("your string is %s \n", first);


13.
   void ncatstr(char str1[], char str2[], int maxcat)
   {
       int i, j;
       for (i = 0 ; str1[i] != '\0' ; i++);
       for (j = 0 ; str2[j] != '\0' && j < maxcat-1 ; i++, j++)
          str1[i] = str2[j];
       str1[i] = '\0';
   }


14.
   char first[20] = "cloudy";
   char second[15] = "blueblue";
   char third[25] = "sunny";
   char fourth[15] = "bright bright";
   ncatstr(first, second, 10);
   ncatstr(third, fourth, 10);
   printf("first string is %s \n", first);
   printf("third string is %s \n", third);

   first: cloudyblueblue
   third: sunnybright br


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

© Copyright Emmi Schatz 2013