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: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include using namespace std; int main() { const int NUM_VALS = 4; int hourlyTemp[NUM_VALS]; int i; for (i = 0; i < NUM_VALS; ++i) { cin >> hourlyTemp[i]; } /* Your solution goes here */ cout << endl; return 0; } 1 test passed All tests passed Run

Respuesta :

Answer:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.    const int NUM_VALS = 4;
  6.    int hourlyTemp[NUM_VALS];
  7.    int i;
  8.    
  9.    for (i = 0; i < NUM_VALS; ++i)
  10.    {
  11.        cin >> hourlyTemp[i];
  12.        
  13.    }
  14.    
  15.    /* Your solution goes here */
  16.    for(i = 0; i < NUM_VALS; ++i){
  17.        
  18.        if(i < NUM_VALS -1){
  19.            cout<<hourlyTemp[i]<<",";
  20.        }
  21.        else{
  22.            cout<<hourlyTemp[i];
  23.        }
  24.    }
  25.    cout << endl;
  26.    return 0;
  27. }

Explanation:

The solution code is given from Line 18 - 26. To print the element from array one after another, we create a for loop to traverse through every element in the array (Line 18). Create an if condition to check if the current index i is not the last index, print the element followed with a comma (Line 20 -22). Otherwise print only the element (Line 23 - 25).

Answer:

for(i = 0; i < NUM_VALS; ++i){

     

      if(i < NUM_VALS -1){

          System.out.print(hourlyTemp[i] + ", ");

      }

      else{

          System.out.print(hourlyTemp[i]);

      }

  }

Explanation:

RELAXING NOICE
Relax