0% found this document useful (0 votes)
85 views8 pages

Ahmad Fakhri Bin Ishak 2015663824 Ec2203a Exercise 7

The document contains C++ code for three programming exercises. The first exercise defines functions to find the smallest, largest, and most occurring values in an array of integers. The second exercise sorts an integer array in ascending and descending order. The third exercise defines functions to display a menu, view student records stored in a 2D array by ID or all records, and find the highest and lowest final scores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views8 pages

Ahmad Fakhri Bin Ishak 2015663824 Ec2203a Exercise 7

The document contains C++ code for three programming exercises. The first exercise defines functions to find the smallest, largest, and most occurring values in an array of integers. The second exercise sorts an integer array in ascending and descending order. The third exercise defines functions to display a menu, view student records stored in a 2D array by ID or all records, and find the highest and lowest final scores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

AHMAD FAKHRI BIN ISHAK 2015663824 EC2203A EXERCISE 7

Question 1
#include <iostream>
using namespace std;
int small(int x[]);
int large(int x[]);
int occur(int x[]);
int main()
{
int value[10], smallest, largest, OccurTheMost;
for(int i=0; i<10; i++)
{
cout << "Enter value[" << i+1 << "]: ";
cin >> value[i];
}
smallest=small(value);
largest=large(value);
OccurTheMost=occur(value);
cout << "The smallest value of 10 numbers is " << smallest << ".\n";
cout << "The largest value of 10 numbers is " << largest << ".\n";
cout << "The number that occurs the most is " << OccurTheMost <<
".\n";
}
int small(int x[])
{
int smallest;
smallest=x[0];
for(int i=0; i<10; i++)
{
if(x[i]<smallest)
smallest=x[i];
}
return smallest;
}
int large(int x[])
{
int largest;
largest=x[0];

for(int i=0; i<10; i++)


{
if(x[i]>largest)
largest=x[i];
}
return largest;
}
int occur(int x[])
{
int popular = x[0];
int temp=0, tempCount, count=1;
for (int i=0;i<10;i++)
{
tempCount = 0;
temp=x[i];
tempCount++;
for(int j=i+1;j<10;j++)
{
if(x[j] == temp)
{
tempCount++;
if(tempCount > count)
{
popular = temp;
count = tempCount;
}
}
}
}
return popular;
}

QUESTION 2

#include <iostream>
using namespace std;
void accending(int a[]);
void descending(int a[]);
int main()
{
int value[10];
for(int i=0; i<10; i++)
{
cout << "Enter value[" << i+1 << "]: ";
cin >> value[i];
}
cout << endl;
accending(value);
descending(value);
}
void accending(int a[])
{
int temp;
cout<<"Data before sorting: \n";
for(int i=0; i<=9; i++)
{
cout << a[i] << "\t";
}
cout << endl;
for(int i=0; i<=9; i++)
{
for(int j=0;j<10-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout << "Data after sorting (ascending): \n";

for(int i=0;i<10;i++)
{
cout << a[i] << "\t";
}
cout << endl;
}
void descending(int a[])
{
int temp;
for(int i=0; i<=9; i++)
{
for(int j=0;j<10-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout << "Data after sorting (descending): \n";
for(int i=0;i<10;i++)
{
cout << a[i] << "\t";
}
cout << endl;
}

QUESTION 3
#include <iostream>
using namespace std;
void showheading();
int hivalue(int stu[4][5]);
int lovalue(int stu[4][5]);
void displaymenu()
{
cout<<"=========================================
==============="<<"\n";
cout<<"
MENU
"<<"\n";
cout<<"=========================================
==============="<<"\n";
cout<<"
cout<<"
cout<<"

1.View all student records"<<"\n";


2.View a student records by ID"<<"\n";
3.Show the highest and the lowest scores"<<"\n";

}
void viewall(int stu[4][5]){
int i,j;
showheading();
for(i=0;i<4;i++)
{
for(j=0;j<5;j++) cout<<stu[i][j]<<"\t\t";
cout<<"\n";
}
}
void viewbyid(int stu[4][5])
{
int id,i,j;
bool l=false;
cout<<"Please enter a student's ID:";
cin>>id;
for(i=0;i<4;i++)
{
if(stu[i][0]==id)
{
showheading();l=true;

for(j=0;j<5;j++)
cout<<stu[i][j]<<"\t\t";
}
cout<<"\n";
}
if(l==false) cout<<"Not found!\n";
}
void showhl(int stu[4][5])
{
cout<<"The higest final score is:"<<hivalue(stu);
cout<<"\n";
cout<<"The lowest final score is:"<<lovalue(stu);
cout<<"\n";
}
void showheading()
{

cout<<"=========================================
===============\n";
cout<<"StudentID
Quiz1
Quiz2
Mid-term
Final\n";
cout<<"=========================================
===============\n";
}
int hivalue(int stu[4][5])
{
int *max,i;
max=&stu[0][4];
for(i=0;i<4;i++)
if(*max<stu[i][4])*max=stu[i][4];
return(*max);
}
int lovalue(int stu[4][5]){
int *min,i;
min=&stu[0][4];
for(i=0;i<4;i++)
if(*min>stu[i][4])*min=stu[i][4];

return(*min);
}
int main()
{
int stu[4][5]={{1232,32,34,43,43},{2345,34,34,54,35},
{3432,45,54,56,34},{3456,56,34,34,56}};
displaymenu();
int yourchoice;
char confirm;
do
{
cout<<"Enter your choice(1-3):";
cin>>yourchoice;
switch(yourchoice)
{
case 1:viewall(stu);break;
case 2:viewbyid(stu);break;
case 3:showhl(stu);break;
default:cout<<"invalid";
}
cout<<"Press y or Y to continue:";
cin>>confirm;
}
while(confirm=='y'||confirm=='Y');
system("PAUSE");
return 0;
}

You might also like