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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Vectors
Click For Summary
SUMMARY

The discussion focuses on various methods to print all elements of a vector in C++ using a for loop, specifically the vector named hourlyTemp with a constant NUM_VALS set to 4. Participants provide multiple solutions to ensure that elements are separated by a comma and space, without trailing punctuation after the last element. The solutions include conditional statements to manage the formatting effectively, demonstrating different coding styles and logic.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with vectors in C++
  • Knowledge of for loops and conditional statements
  • Basic output operations using cout
NEXT STEPS
  • Explore advanced vector manipulation techniques in C++
  • Learn about C++ STL (Standard Template Library) for efficient coding
  • Investigate formatting output in C++ using iomanip
  • Study best practices for writing clean and maintainable C++ code
USEFUL FOR

C++ developers, programming students, and anyone interested in improving their skills in vector manipulation and output formatting in C++.

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.
 

Similar threads

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