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
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top