Answer to Question #260512 in C++ for malika

Question #260512

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.


#include <iostream>

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
Expert's answer
2021-11-05T00:34:16-0400
#include <iostream>
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];
	}


	for (i = 0; i < NUM_VALS-1; ++i) {
		cout << hourlyTemp[i]<<",";
	}
	cout << hourlyTemp[NUM_VALS-1];


	cout << endl;






	cin>>i;


	return 0; 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS