C/C++ Possible title: How to Print All Elements of a Vector Using a For Loop in C++

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Vectors
Click For Summary
The discussion focuses on writing a for loop to print elements of a vector, specifically `hourlyTemp`, with elements separated by a comma and space, without trailing punctuation after the last element. Several coding solutions are presented, emphasizing different approaches to handle the conditional logic for printing commas. One method checks if the index is greater than zero before printing a comma, while another checks if the index is less than `NUM_VALS - 1`. Participants express preferences for the latter method, appreciating its clarity in maintaining the desired output format. The importance of understanding the indexing in relation to the last element is also highlighted.
ineedhelpnow
Messages
649
Reaction score
0
Write a for loop to print all NUM_VALS elements of vector hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:

90, 92, 94, 95

Note that the last element is not followed by a comma, space, or newline.Sample program:
Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_VALS = 4;             
   vector<int> hourlyTemp(NUM_VALS); 
   int i = 0;                         

   hourlyTemp.at(0) = 90;
   hourlyTemp.at(1) = 92;
   hourlyTemp.at(2) = 94;
   hourlyTemp.at(3) = 95;

   <STUDENT CODE>
   cout << endl;

   return 0;
}

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.)

answer is
Code:
   for(int i=0; i<NUM_VALS; i++)
   {
   if(i==0)
   cout << hourlyTemp[i] ;
   else cout <<", " << hourlyTemp[i];
   }

but is there any other way to write the if else statement?
 
Technology news on Phys.org
Hi Pippy! (Smile)

There's lots of ways.
For instance:
Code:
for(int i=0; i<NUM_VALS; i++)
{
   if(i>0) {
      cout << ", ";
   }
   cout << hourlyTemp[i] ;
}

Or:
Code:
for(int i=0; i<NUM_VALS; i++)
{
   cout << hourlyTemp[i] ;
   if (i < NUM_VALS - 1) {
      cout << ", ";
   }
}
(Wasntme)
 
I like Serena said:
Hi Pippy! (Smile)

There's lots of ways.
For instance:
Code:
for(int i=0; i<NUM_VALS; i++)
{
   if(i>0) {
      cout << ", ";
   }
   cout << hourlyTemp[i] ;
}

Or:
Code:
for(int i=0; i<NUM_VALS; i++)
{
   cout << hourlyTemp[i] ;
   if (i < NUM_VALS - 1) {
      cout << ", ";
   }
}
(Wasntme)

Hey ILS. :o
yeah i like the last way the best i think. its saying print all the numbers from the list and as long as its less than the last one also print the comma and space (Thinking) i always forget that NUM_VALS-1 means the item before the last.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
1
Views
4K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K