How to find the total number of records

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.
You need a struct or class that gives details for every student.
In your example you need: "studentID" and "studentMark", you can add more details like name etc.
Your struct looks like this:

struct Students
{
unsinged long studentID;
float studentMark;
} totalStud;

** unsinged means that the number is positive and it expand the range of the variable value.
long is 4 bytes which is 32 bits and it's range is: -(2^(32-1)) to (2^(32-1))-1
When you add unsigned, the range becomes: 0 to (2^32)-1

in main function, you define the details of every student. If you don't know the initial number of students, you should use dynamic memory, look in: http://www.cplusplus.com/doc/tutorial/dynamic/

Good luck!
Last edited on
Noha, I believe you have a typo there, the keyword is unsigned.

Anyway I think he has to use a string for that, since his student IDs contain a letter of the alphabet.

Dwade: You are already hard coding the data (the student IDs and their scores) into your program, so why can't you just hard code the totalStudents=2 as well?

Alternatively you can make your program scan for all inputs (i.e. Enter student ID. Enter score. Process another student? ) to make it less contrived. Have a counter keep track of the number of student IDs entered and assign it to totalStudents.
That's true, I always make this typo. Sorry!

Regarding to the student IDs, I was sure that he uses numbers, but even in the case of numbers I agree with "fauntleroy42" that it's better using a string since it occupy less place in memory (char = 1 byte and long = 4 bytes).

If student IDs is a string of numbers, you can convert it to int, long whatever in order to calculate. You do this by defining a new variable with type int, long etc. (you choose it according to the range of the variable):

1
2
long studID;
stringstream (student IDs) >> studID;


Thus, you define only one variable with type long.
Topic archived. No new replies allowed.