Finding the average Marks (I dont know how to get the total number of students)

closed account (167DSL3A)
The question I have to answer is to find out the average marks between two students. These students are:
studentID: P1001 mark: 78.5
studentID: P1002 mark: 66

I am using arrays to store these records. My declarations are as follows:
int menu;
xxxxx studentID[] = {"P1001", "P1002"};
float studentMark[] = {78.50, 66};
int i=0;
int j;
int totalStudents=0;
float totalMarks=0;
float averageMark;

*xxxxx indicates the data type. HOWEVER I do not know what data type to use as it consist both int and char. I have tried using these data types as well as void, but I continue getting an error.

My other question is, how can I find the total number of students so that I can use it to calculate the average. Obviously I know that the total is 2, but I will lose marks if I just enter 2 by default. I have found a solution to calculate the total marks but I just cant seem to come across the number of total students.

Heres is what my full coding looks like at the moment:
#include <iostream>
using namespace std;

int main()
{
int menu;
studentID[] = {"P1001", "P1002"};
float studentMark[] = {78.50, 66};
int i=0;
int j;
int totalStudents=0;
float totalMarks=0;
float averageMark;


cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "------------------------------\n"
<< "Your choice -> ";
cin >> menu;

while (menu != 0)
{
switch (menu)
{
case 1:
for(int j=0;j<2;j++)
totalMarks+=studentMark[j];
totalStudents++;
averageMark=totalMarks/totalStudents;
cout << "Mean or average: " << averageMark <<endl;
break;
case 2:
cout << "Please enter Student ID: ";
break;
case 3:
cout << "Find mark" << endl;
break;
default:
cout << "Invalid selection. Please make a selection between 0-3.\n"
<< endl;
}
system("Pause");
cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "----------------------------\n"
<< "Your choice -> ";
cin >> menu;
}

return 0;
}


If somebody could give me some hints or suggestions it would be very much appreciated.
//Corrected:
#include <string>
#include <iostream>
using namespace std;

int main()
{
int menu;
string studentID[] = {"P1001", "P1002"};
float studentMark[] = {78.50, 66};
int i=0;
int totalStudents=0;
float totalMarks=0;
float averageMark;


cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "------------------------------\n"
<< "Your choice -> ";
cin >> menu;

while (menu != 0)
{
switch (menu)
{
case 1:
for(int j=0;j<2;j++)
{
totalMarks+=studentMark[j];
totalStudents++;
}
averageMark=totalMarks/totalStudents;
cout << "Mean or average: " << averageMark <<endl;
break;
case 2:
cout << "Please enter Student ID: ";
break;
case 3:
cout << "Find mark" << endl;
break;
default:
cout << "Invalid selection. Please make a selection between 0-3.\n"
<< endl;
}
system("Pause");
cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "----------------------------\n"
<< "Your choice -> ";
cin >> menu;
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.