C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 10th, 2005, 07:10 AM
cofffy cofffy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 cofffy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > vector problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway