I am looking up an online tutorial for C++ programming and it asks to use a push_back method to get 5 values and save them in an array. I have done the program without the push_back method because I don't understand it. How can I add something like that to this program. I know that the line 2 needs to be there for the push_back, but not sure how to add it to a loop that gets the values. Thanks for your help.
#include <iostream>
#include <vector>
usingnamespace std;
//Declaring constants
constint MAX = 5;
int main()
{
//Initialize some components
double sum = 0;
int myData[MAX];
// first loop - get the data
for (int count = 0; count< MAX; count++)
{
cout << "Enter Number: ";
cin >> myData[count];
}
// second loop - accumulate the total
for (int count = 0; count < MAX; count++)
{
sum += myData[count];
}
cout << "The sum is " << sum << endl;
//Creating end point for the program
system("PAUSE");
// Returning 0
return 0;
}
Line 13 should be: std::vector<int> myData;
Now myData is an object that has the push_back method.
Next, the input loop should preferably be a while, where the condition tests both size of the array and success of input (into a variable). Then the variable is pushed into the myData in the body of the loop.
The summation loop should then loop over the actual vector and not presume it to have MAX elements. There is std::accumulate() to use instead of explicit loop.
#include <iostream>
#include <vector>
usingnamespace std;
//Declaring constants
constint MAX = 5;
int main()
{
//Initialize some components
double sum = 0;
vector<int> myData;
// now read data from cin until a zero is typed
cout << "\nType in exam scores ... enter zero to quit";
//Declaring variables
int i;
double data;
// first loop - get the data
do
{
cout << "\nscore " << i++ << ": ";
cin >> data;
if (data != 0) // this is a valid score, save it
{
myData.push_back(data);
}
} while (data != 0);
// second loop - accumulate the total
for (int i = 0; i < MAX; i++)
{
sum += myData[i];
}
cout << "The sum is " << sum << endl;
//Creating end point for the program
system("PAUSE");
// Returning 0
return 0;
}
Line 19,25: i is uninitialized. This will print garbage.
Line 34: As keskiverto pointed out, the loop limit should be myData.size(), not MAX. This will allow any number of grades to be input. The vector will adjust it's size to accommodate.
#include <iostream>
#include <vector>
usingnamespace std;
//Declaring constants
int main()
{
//Initialize some components
double sum = 0;
vector<double> myData;
// now read data from cin until a zero is typed
cout << "\nType in up to five decimals for a sum ... enter zero to quit";
//Declaring variables
int i = 1;
double data;
// first loop - get the data
do
{
cout << "\nNumber " << i++ << ": ";
cin >> data;
if (data != 0) // this is a valid score, save it
{
myData.push_back( data );
}
} while (data != 0);
// second loop - accumulate the total
for ( i; i <= myData.size(); i++)
{
sum += myData[i];
}
cout << "The sum is " << sum << endl;
//Creating end point for the program
system("PAUSE");
// Returning 0
return 0;
}
The problem is your for loop at line 33. You're not initializing i and instead using the last value of i which was used to count the number of values (line 24). Your termination condition is also faulty. Assuming you initialize i to 0, <= will cause you to subscript past the end of the vector. If you enter 5 values, those values are [0]-[4]. [5] is not valid.
Hi all.
DEnumber50 try to use vector functions to access the vars... . like .at()
read the functions of vector. I'm sure you will correct your mistake after you read this functions and used them.
be careful about line 35. and try to write clear code! and do not write useless comments like Returning 0.
Thanks
Here is your code, But please read the vectors functions, they will help you in future.
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
double sum = 0;
vector<double> myData;
cout << endl<<"Type in up to five decimals for a sum ... enter zero to quit";
int j = 0;
double data;
do
{
cout << endl <<"Number " << ++j << ": ";
cin >> data;
if (data != 0)
{
myData.push_back( data );
}
} while (data != 0);
for (int i = 0; i <= myData.size()-1; i++)
sum += myData.at(i);
cout << "The sum is " << sum / myData.size() << endl;
cin.get(); // try to do not use system function. cin.get() will wait to cin an elemnt from keyboard as the system("PAUSE") do.
cin.get(); // try to do not use system function.
return 0;
}