1. Write a C++ program that will prompt the user to input ten integer values.
The program will display the smallest and greatest of those values. It also displays the value that occurs the most.
Solution:
#include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int arr[10];
int mode[10][2];
cout<<"Enter 10 integer values\n";
for(int l=0;l<10;l++) {
cout<<"value "<<l<<":";cin>>a[l];
}
//find the max value and min value
int i,j,temp;
//sort array to find max and min values
for(i=0;i<10;i++)
for(j=9;j>i;j--)
if(arr[j]<arr[j-1]) {
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
cout<<"Max="<<arr[9]<<"\nMin="<<arr[0];
cout<<"\n";
//initialize 2D array storing numbers of occurences, and values
for(i=0;i<2;i++)
for(j=0;j<10;j++)mode[j][i]=0;
mode[0][0]=1;
//find mode
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(arr[i]==arr[j+1]) {++mode[i][0];mode[i][1]=arr[i];}
//find max occurence
int max;
int k=0;
max=mode[0][0];
for(j=0;j<10;j++)
if(max<mode[j][0]){max=mode[j][0];k=j;}
//print result
cout<<"The most occurring item:"<<mode[k][1]<<"\n";
cout<<"It occurs "<<max<<" times.";
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
//find the max value and min value
int i,j,temp;
//sort array to find max and min values
for(i=0;i<10;i++)
for(j=9;j>i;j--)
if(arr[j]<arr[j-1]) {
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
cout<<"Max="<<arr[9]<<"\nMin="<<arr[0];
cout<<"\n";
//initialize 2D array storing numbers of occurences, and values
for(i=0;i<2;i++)
for(j=0;j<10;j++)mode[j][i]=0;
mode[0][0]=1;
//find mode
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(arr[i]==arr[j+1]) {++mode[i][0];mode[i][1]=arr[i];}
//find max occurence
int max;
int k=0;
max=mode[0][0];
for(j=0;j<10;j++)
if(max<mode[j][0]){max=mode[j][0];k=j;}
//print result
cout<<"The most occurring item:"<<mode[k][1]<<"\n";
cout<<"It occurs "<<max<<" times.";
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
2. Write a C++ program to sort 10 integer values (reading from keyboard) in ascending and descending order.
See solution 1
No comments
Post a Comment