Write a program that initializes roll numbers and marks of different subjects in two-dimensional array. The roll numbers should be in first column and marks of different subjects should be in other columns such as:
1 55 78 95 75
2 54 85 62 85
3 58 81 85 75
4 60 82 74 75
5 45 72 25 74
The program should read the 2-D array, compute the total marks of each student and store the result in the last column.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an 2D array tab1.
- Step 3: Check if r< =4 then ask the user to enter the rollno.
- Step 4: Check if c < 4 then ask the user to enter the subject mark.
- Step 5: Repeat Step 4 until c<4, if is not c<4 then goto step 3.
- Step 6: Print results.
- Step 7: End Program
Code:
Write a program that initializes roll numbers and marks of different subjects in a two-dimensional array.
#include<iostream>
using namespace std;
main()
{
int tab1[5][6], r, c, s;
r = 0;
while(r<=4)
{
c = 0;
s = 0;
cout<<"Enter record # "<<r+1<<endl;
cout<<"Enter roll number ? ";
cin>>tab1[r][c];
while(c<4)
{
c = c + 1;
cout<<"Enter marks of subject "<<c<<" ? ";
cin>>tab1[r][c];
s = s + tab1[r][c];
}
c = c + 1;
tab1[r][c] = s;
r++;
}
cout<<"\nRecords of students:" <<endl;
cout<<"Roll#\tSub1\tSub2\tSub3\tSub4\tTotal Marks"<<endl;
for(r = 0; r<=4; r++)
{
for(c = 0; c<=5; c++)
cout<<tab1[r][c]<<"\t";
cout<<endl;
}
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well😇 https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: 👉https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: 👉https://myustaadg.com/category/programming-exercises/arrays-exercises/