Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help with array functions

Ok i have to make a program that does a good bit of stuff, it reads in a file and finds the highest scoring test, the average and some other stuff. So far i got a function that reads in the file and counts how many test scores there are. I also have a function already that adds them and gives me the total and then i can find the over all class average. Theres 2 functions left i need help with. One is called maxIndex that is supposed to return the index where the max score is found. I read in the C++ book and found a code that should work but it just returns 2 every time. Also i need a print function that prints all the outputs but finds all the students who have the highest score and find the students who fall below the class average. This is my code. For some reason the sum function works on my school computer but produces weird answers at my home computer. If you could help me figure out max index and complete the print function and explain what stuff does that would help me. If not thats ok, any hints will help.

Here is my program:

#include<iostream>
#include<fstream>
using namespace std;
ifstream inFile;
void getData(ifstream& inputFile, string s[], int sList[], int& size, int maxSize);
void printResult(string s[], int size, double avg);
int sumScores(int list[], int size);
int maxIndex(int list[], int size);
const int NO_OF_STUDENTS = 50;
int main()
{
string s[NO_OF_STUDENTS];
int sList[NO_OF_STUDENTS];
int size = 0;
int total = 0;
int max;

inFile.open("studentdata.txt");
getData(inFile, s, sList, size, NO_OF_STUDENTS);
total = sumScores(sList, size);
cout<<"Average is: "<<total/size<<endl;




system("PAUSE");
return 0;
}

void getData(ifstream& inputFile, string s[], int sList[], int& size, int maxSize)
{
int i = 0;
while(inFile&&i<NO_OF_STUDENTS)
{
inFile>>s[i];
inFile>>sList[i];
i++;
size++;
}
size-=1;
cout<<size<<endl;
}
void printResult(string s[], int size, double avg)
{

}
int sumScores(int list[], int size)
{
int total = 0;
for(int i = 0; i<=size; i++)
{
total = total + list[i];


}
return total;
}
int maxIndex(int list[], int size)
{
int index;
int maxIndex = 0;

for(index = 1; index < size; index++)
{
if(list[maxIndex] < list[index])
maxIndex = index;
cout<<maxIndex<<" "<<index<<endl;
}
return maxIndex;

}
[2909 byte] By [C-+-+] at [2007-11-11 11:58:45]
# 1 Re: Help with array functions
maxindex looks correct -- are you using the same list of data each time and is 2 therefore the correct answer each time?

the sum function has a bug -- you do <= size but it should be < size. Thiis could account for the problem you see.. maybe you add zero on one computer and 26987641 on the other computer -- that last value that you address is "random" garbage.

A print function should be easy, maybe you should sort the list first to find the ones who are above or below the average or some points above or below it ?? You need to define the problem/task a bit better before you can write this function.
jonnin at 2007-11-11 20:55:31 >