this is the code i have:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

ifstream infile("grade_data.txt");
string answers, key;
int num_grades;

void highest(double [], int);
void lowest(double [], int);

const int NUM_QUESTIONS = 10;
double scores[NUM_QUESTIONS + 1];
int results;   //how many right answers
int wrong=0;   //how many wrong answers
double total=0.00;    //total of all answers, used for average.
double average=0.00;
double percent[50];    //% right of answers
const int x=10;
int t1[x+1];

int main()
{

	int b;

	if (!infile)
	{
		cout << "Error opening file\n\n";
		system("pause");
	}
	else
		cout << "File Successfully Opened\n\n";


	infile >> key;

	cout<<"  KEY\n";
	for (int i=0; i < key.length(); i++)
		cout<<" | "<<key[i]<<" | \n";


	cout<<endl;

	infile >> answers;
	while (!infile.eof() && answers != " ")
	{
		num_grades++;

		for (int a=0; a < answers.length(); a++)
			cout<<" | "<<answers[a]<<" | \n";


		for(b = 0; b < NUM_QUESTIONS; b++)
		{

			if(key[b] == answers[b])
			{
				results++;
				cout<<"CORRECT ANSWERS: "<<b+1<<"\n";  //which questions they are getting right.
				t1[b]++;


			}
			else
				wrong++;


		}

		percent[num_grades]=results*10;
		cout<<"Student "<<num_grades<<":    Made: "<<fixed<<setprecision(0)<<results<<
        "   Missed: "<<wrong<<"   Grade: "<<percent[num_grades]<<"%\n";

		for (int c=0; c<1;c++)
			total+=results;
		average=(total/num_grades);

		infile >> answers; 

		results=0;
		wrong=0;      


	}


	for (int i=0;i<9;i++)
		cout<<"\nQuestion "<<i+1<<"  -   "<<t1[i];
	cout<<"\nQuestion 10 -   "<<t1[9]; 

	cout<<"\n\nThe average of "<<num_grades<<" tests is: "<<fixed<<setprecision(2)<<average<<endl; 

	highest(percent, 10);
	lowest(percent, 10);

	cout<<"\n\n";
	system("pause");
}


void highest(double amt[], int length)                             //function to find the highest value in array
{

	double highest;

	highest = amt[0];                  // sets the first value in array to variable 'highest'
	for (int i=1; i < length; i++)               // counts i amount of times
	{
		if (amt[i] > highest)           // as the counter goes from 1 to the value of a, each number in the array will
			highest=amt[i];                 // be checked for the highest value. highest will be stored in variable 'highest'
	}
	cout<<"\nHighest  :  "<<highest;            
}

void lowest(double amt[], int length)                                // function to find the lowest value in array 'ptr'
{                          
                                             // the lowest function below is the exact same as the 'highest' function,
     double lowest;         
                      
         lowest= amt[length-1];           
         for (int i=0; i< length; i++) 
         {
             if(amt[i] < lowest)
             lowest=amt[i];
             }
             cout<<"\nLowest   :   "<<lowest;
             cout<<"\n\n\n";
}

it compiles but it always returns 0 for the lowest score. it should be higher than that.


heres the grade_data.txt info

bccbbadbca
cbabddcbaa
accabcbbac
babccabadd


any help is appreciated

Perhaps you should take another look at how you're indexing amt[] in your "lowest" function.

Here's a hint: (length - 1) isn't what you want to use. amt[length - 1] is 0, and you're not going to have a grade lower than that, right?

length-1 should be 9 and i tried amt[10] just to see if anything would change but its still 0.

No no - there's nothing in amt[9] either. Set a break point at the beginning of your "lowest" function and step through the code. You'll easily see why you shouldn't be indexing at anything to do with the variable "length".

Let's assume you have 5 grades and "length" = 10 (which you've passed in). If you compare everything in your amt[] array, you'll eventually hit a zero, which will always be lower.

Another part of your code to look at:

for (int i=0; i< length; i++) 
         {
             if(amt[i] < lowest)
             lowest=amt[i];
             }
             cout<<"\nLowest   :   "<<lowest;
             cout<<"\n\n\n";

You don't need to compare every index, right? Only the indexes that have had grades added. So maybe instead of (i < length), you should do something like (i < num_grades), don't you think?

Edited 6 Years Ago by Duki: n/a

I don't see it mentioned, so I'm going to add.

When you are finding a minimum or maximum value, it is advisable to initialize your "lowest"/"highest") variable (whatever it happens to be named) to the first value of your set as opposed to 0 or some arbitrary value.

This simple change guarantees that your lowest/highest detected value will be part of your data set. It's guaranteed (as long as your comparison algorithm(s) are correct) because you are starting with a member of the set and comparing to other members of the set. You are not starting with some arbitrary value that you hope will be substantially different enough to be bigger/smaller than all possible members of the set.

EDIT:
nvm, it seems you are already doing this. I must have read something wrong.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.