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;
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/
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.
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.