Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C language, Arrays Homework. As a practical joke, a friend gives you an int a

ID: 3919047 • Letter: I

Question

In C language, Arrays Homework. As a practical joke, a friend gives you an int array for your birthday. As if that was not bad enough, your friend tells you that the array contains almost all 0's except for a small string of consecutive 1's contained somewhere in the middle. Overwhelmed by the novelty of this you decide to write a function that will print out the location of the first 1 in the array, the location of the last 1 in the array, and the total number of 1's in the list of consecutive 1 values. Given below is the function prototype: void joke (int num[ ], int max);

Where num is the array of int values and max contains the number of elements in the array

Explanation / Answer

void joke (int num[ ], int max) {
   int first = -1, last = -1, i, count = 0;
   for(i = 0; i < max; ++i) {
       if(num[i] == 1) {
           if(first == -1) {
               first = i;
           }
           last = i;
           count++;
       }
   }
   printf("First 1 found at index %d ", first);
   printf("Last 1 found at index %d ", last);
   printf("Number of 1's found is %d ", count);
}