2. Program that uses two arrays to store the roll # and marks of student. It inputs roll # and marks of five students, displays the roll # and marks of the student with highest marks.
Chapter 7:
Arrays
Programming Exercise
Problem # 2:
Write a program that uses two arrays to store the roll number and marks of student. It inputs roll numbers and marks of five students and stores them in corresponding elements of the arrays. (For example, if roll number 1 is stored in rno[0] then his marks must be stored in marks[0] and so on.) The program finally displays the roll number and marks of the student with highest marks.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int rno[5], marks[5], max, n, i;
cout<<"Enter roll no:\t";
cin>>rno[0];
cout<<"Enter marks:\t";
cin>>marks[0];
max=marks[0];
n=rno[0];
for(i=1;i<5;i++)
{
cout<<"Enter roll no:\t";
cin>>rno[i];
cout<<"Enter marks:\t";
cin>>marks[i];
if(marks[i]>marks[i-1])
{
max=marks[i];
n=rno[i];
}
}
cout<<"\nThe student wuth highest marks has roll no."<<n<<" and marks:"<<max;
}
#include<conio.h>
using namespace std;
int main ()
{
int rno[5], marks[5], max, n, i;
cout<<"Enter roll no:\t";
cin>>rno[0];
cout<<"Enter marks:\t";
cin>>marks[0];
max=marks[0];
n=rno[0];
for(i=1;i<5;i++)
{
cout<<"Enter roll no:\t";
cin>>rno[i];
cout<<"Enter marks:\t";
cin>>marks[i];
if(marks[i]>marks[i-1])
{
max=marks[i];
n=rno[i];
}
}
cout<<"\nThe student wuth highest marks has roll no."<<n<<" and marks:"<<max;
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
1. Program that inputs ten integers in an array and counts all prime numbers entered by the user. The program finally displays total number of primes in array.
Comments
Post a Comment