
April 10th, 2005, 07:10 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 4
Time spent in forums: 1 h 35 m 41 sec
Reputation Power: 0
|
|
|
vector problems
Hi there,
can someone help me? I have been working on this for a long time, and it seems like I am messing things up more, as I go.
I put at the top of the program what I am trying to do. The problem seems to be with the avgVector.
Thanks for anyhelp you can send my way! ......going nuts
Code:
//ask the user for test grades.
//The user should enter -99 to indicate no more test grades.
//Then the program should sort the grades in ascending order
//and display the grades in that order.
//Also, the program will calculate and display the average test score.
//then the lowest score will be dropped
#include <iostream>
#include <vector> //include the vector header
#include <algorithm> //Required for STL algorithms
#include <cstring> // strcmp
#include <cstdlib> // atoi
using namespace std;
int main()
{
vector<int> testScores;
const int size = 4;
char input[size];
int total = 0, count = 0;
int tempscores;
vector<int> vect;
float average;
//Ask for the number of tests
cout << "Enter a test score, then enter Q when you are finished.";
cin.getline(input, size);
while (tolower(input[0]) != 'q') // I want this to be -99not 'q' but can't figure out code
{
total += atoi(input); // Keep a running total
count++; // Count the numbers entered
cout << "Enter the next number or Q to quit: ";
cin.getline(input, size);
}
for (tempscores = 0; tempscores < vect.size(); tempscores++)
{
int tempScores; // to hold the number of testscores entered
testScores.push_back(tempScores); // add elemnet to testscores
}
//Sort
if (count !=0)
{
sort(vect.begin(), vect.end());
//Display the vector's element again
cout << "The elements have been sorted: \n";
for (tempscores = 0; tempscores < vect.size(); tempscores++)
cout << vect[tempscores] << " ";
cout << endl;
}
//calculate the avg.
//if (count !=0)
//{
// average = static_cast<float>(total) / count;
// cout << "Average: " << average << endl;
//}
average = avgVector(testScores);
cout << "Average: " << average << endl;
return 0;
float avgVector(vector<int> vect)
{
int total = 0;
float avg;
if (vect.empty()) //determine if the vector is empty
{
cout << "No values to average. \n";
avg = 0.0;
}
else
{
for (int count = 0; count < vect.size(); count++)
total +=[count]
avg = total / vect.size();
}
}
return avg;
}
|