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

Writing a code portion in the C language... Write a for loop to print all NUM_VA

ID: 3568034 • Letter: W

Question

Writing a code portion in the C language...

Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:

Note that the last element is not followed by a comma, space, or newline.


Sample program:

Below, do not type an entire program. Only type the portion indicated by the above instructions (and if a sample program is shown above, only type the <STUDENT CODE> portion.)

what I came up with is:

for(int i=0; i<NUM_VALS[i]; i++)
{
if(i=0)
printf("%d", hourlyTemp[i]);
else printf(", ", hourlyTemp[i]);
}

but this is not correct..any suggestions to make it correct?

Explanation / Answer

#include <stdio.h>
int main(void) {
   const int NUM_VALS = 4;   
   int hourlyTemp[NUM_VALS];      
   int i = 0;                       
   hourlyTemp[0] = 90;
   hourlyTemp[1] = 92;
   hourlyTemp[2] = 94;
   hourlyTemp[3] = 95;
   for(i=0; i<NUM_VALS; i++){
   if(i==0)
   printf("%d",hourlyTemp[i]);
   else
   printf(", %d",hourlyTemp[i]);
   }
   printf(" ");
   return 0;
}

   for(i=0; i<NUM_VALS; i++){
   if(i==0)
   printf("%d",hourlyTemp[i]);
   else
   printf(", %d",hourlyTemp[i]);
   }