I am a beginner at C++ am having a lot of trouble trying to make functions to execute each task. I think I could handle doing each task in the main function but I don't know how to break them into separate functions (eg: I don't know how to break reading of the .txt file and displaying it into separate functions.)
Also, for 'scores' I keep getting an error message that says "subscript needs array or pointer type" but I don't know what that means.
Note: I have not completely finished the program so the curve, displaycurve, and averagecurve do not have a function made yet, which I will do later.
//this program reads data from a .txt file, displays the scores, finds the average score, finds the highest score, and displays the curve
#include <iostream>
#include <fstream>
using namespace std;
//function prototypes
void readscores (int); // read exam scores into an array from examscores.txt
void displayscores (int); // display scores in row of four scores
double average (const double scores [], int); //??? calculate average score and display
double maxscore (const double[], int); // find max and display
double curve (const double []); //find the "curve" based on the highest scores
double displaycurve (const double []);// display curves in rows of four
double averagecurve (const double []); //calculate the average curved score and display
int main ()
{
const int array_size = 30; //array size
double scores[array_size];//array of 30 elements
int count = 0;
ifstream inputfile;
//open file
inputfile.open("ExamScores.txt");
//read scores
while (count < array_size && inputfile >> scores[count])
count ++;
//display scores
cout << "The numbers are:";
for (count = 0; count < array_size; count++)
displayscores(scores[count]);
//calculate the average
cout << "The average is:";
average (scores, array_size);
//find the max score and display
cout << "The maximum score is:";
maxscore (scores, array_size);
return 0;
}
void displayscores (int num)
{
cout << num << " ";
}
double average (const double scores, int array_size)
{double total = 0;
double average;
for (int count = 0; count < array_size; count ++)
total += scores[count];
average = total /array_size;
}
double maxscore (const double scores, int array_size)
{double max;
max = scores [0];
for (int count = 1; count < array_size; count++)
{if (scores[count] > max)
max = scores[count];
}
return max;
}
These are the numbers or scores in the .txt file:
67 64 83 81 72 75 85 81 56 88 71 80 90 58 78 74 84 64 72 69 78 87 84 72 83 68 62 88 70 75
I apologize if my coding is completely wrong, I am still trying to wrap my head around the basic concepts as the professor doesn't like to explain what he is teaching and the semester is already halfway done.