Should we use do while loops, nested ifs in theis question??

Sep 29, 2013 at 4:21pm
Write a program that uses nested loops to read the marks of 5 students in 3 subjects and calculate
their total marks and average.

If you can...do this question for me please... :)

Sep 29, 2013 at 4:37pm
You want us to do your homework? Please read the topmost post in this beginners forum. There's a rule that you can't ask us to do your homework. You must first try and then if you're stuck somewhere, we'll be glad to help you.
Sep 29, 2013 at 5:02pm
ok im posting the codes -------An sorri i was so impatient to post ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
    float s1, s2, s3; //3 subjects
    double ave, tmrk; //average score + total marks
    char choi; //holding Y or N input for 5 students

    do
    {
        //input 3 scores
        cout<<"Enter the marks of the 3 subjects please:-\n";
        cin>>s1;
        cin>>s2;
        cin>>s3;

        //calculating total marks
        tmrk = (s1 + s2 + s3);
        cout<<"The total marks of the student is "<<tmrk<<endl;

        //calculating average marks
        ave = (s1 + s2 + s3) / 3.0;
        cout<<"The average marks for the student is "<<ave<<endl;

        // enter input from another student
        cout<<"Do you need to calculate total marks and average marks for other students? (Y/N) ";
        cin>>choi;


    }
    while (choi == 'Y' || choi == 'y');

    return 0;
}


Last edited on Sep 29, 2013 at 5:05pm
Sep 30, 2013 at 6:20am
Your code appears correct in case you only want to store the values of marks , average marks and total marks temporarily to display.
Sep 30, 2013 at 8:03am
If you know the number of loops -iterations- (5x3=15)you can use for loop instead
of while or do-while.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//marks.cpp
//Program that uses nested loops to read the marks 
//of 5 students in 3 subjects and calculate their total
//marks and average.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(){

const int STUDENTS=5;
const int SUBJECTS=3;

double subjects[STUDENTS][SUBJECTS]={};
double average[STUDENTS]={0};


for(int i=0;i<STUDENTS;i++){
	cout<<"Student "<<i+1<<'\n';
	for(int j=0;j<SUBJECTS;j++){ //input 3 scores
		
		cout<<"Enter the mark of subject "<<j+1<<" :";
		cin>>subjects[i][j];
		average[i]+=subjects[i][j]; //calculating total marks
	}//end inner for
	cout<<"\nThe total marks of student "<<i+1<<" is: "<<average[i]<<endl;
	average[i]/=SUBJECTS; //calculating average marks
	cout<<"The average marks for student "<<i+1<<" is: "<<average[i]<<endl;
	cout<<'\n'<<endl;
}//end outer for




return 0; //indicates success
}//end main 



Eyenrique-MacBook-Pro:Help Eyenrique$ ./marks 
Student 1
Enter the mark of subject 1 :1
Enter the mark of subject 2 :2
Enter the mark of subject 3 :3

The total marks of student 1 is: 6
The average marks for student 1 is: 2


Student 2
Enter the mark of subject 1 :2
Enter the mark of subject 2 :3
Enter the mark of subject 3 :4

The total marks of student 2 is: 9
The average marks for student 2 is: 3


Student 3
Enter the mark of subject 1 :3
Enter the mark of subject 2 :5
Enter the mark of subject 3 :7

The total marks of student 3 is: 15
The average marks for student 3 is: 5


Student 4
Enter the mark of subject 1 :0
Enter the mark of subject 2 :0
Enter the mark of subject 3 :0

The total marks of student 4 is: 0
The average marks for student 4 is: 0


Student 5
Enter the mark of subject 1 :1
Enter the mark of subject 2 :2
Enter the mark of subject 3 :1

The total marks of student 5 is: 4
The average marks for student 5 is: 1.33333
Oct 2, 2013 at 4:15pm
Thank you...we had to use nested FOR loop...i've used WHILE loop...which is incorrect. Thanks for the guidelines.
Topic archived. No new replies allowed.