input arrays,output , max , min seond highest ,second lowest function, reverse array,sum
please give feedback on :)
https://www.facebook.com/invigorating.arslaan
#include<iostream>
using namespace std;
int menu( char &);
void takeAction();
int inputArray(int ,int );
int outputArray(int , int );
int maximum(int , int);
int minimum(int , int);
int secondhighest (int , int );
int reverseArray (int , int );
int sumOfArray ( int , int );
int main(){
char a=0;
takeAction();
menu(a);
system("Pause");
return 0;
}
int inputArray(int Arr[],int size)
{
// just using a loop to set inputs according to index.
for(int i=0;i<size;i++)
cin>>Arr[i];
return 0;
}
int outputArray(int Arr[], int size)
{
// just using looop to show the value of every index on console.
for(int i=0;i<size;i++)
cout<<Arr[i]<<" ";
return 0;
}
int maximum(int arr [], int size)
{
int max=0;
max=arr[0];
for(int i=0;i<size;i++)
{
// we are comparing a variable max with this complete array and updating its value to largest number we found in array.
if(arr[i]>max)
max=arr[i];
}
return (max);
}
int minimum(int arr[], int size)
{
int min=0;
min=arr[0];
for(int i=0;i<size;i++)
{
// we are doing same thing as in above function in this we are assigning smallest value to variable min.
if(arr[i]<min)
{
min=arr[i];
}
}
return (min);
}
int secondhighest (int arr[], int size)
{
int max=0,secondmax=0;
// this portion will work if we are only dealing with 0 to 10 numbers.
/*
for (int i=0;i<size;i++)
{
if(arr[i]>max)
{
secondmax=max;
max=arr[i];
}
else if(secondmax>arr[i] && secondmax!=max)
{
secondmax=arr[i];
}
}
return (secondmax);
*/
// we are just comparing 1st and second number in this array and in this case we are assigning smallest value to secondmax.
if(arr[0] > arr[1])
{
secondmax = arr[1];
max = arr[0];
}
else
{
secondmax = arr[0];
max = arr[1];
}
// after checking first two numbers then we will compare for number 3 , 4 ,5 and so on and will update the values of max and second max.
for(int i = 2; i < size; i++)
{
// we use >= not > because if there are two same large values then > sign would update values of both secondmax and max to that value.
if(arr[i] >= max)
{
secondmax=max;
max=arr[i];
}
else if(arr[i] > secondmax)
{
secondmax=arr[i];
}
}
return(secondmax);
}
int reverseArray (int arr[], int size)
{
// just using a loop and this loop would start from end to begin and will cout values accordingly loop.
// we size -1 instead of size beacuse if we take size it would print one extra value than required.
for(int i=size-1;i>=0;i--)
{
cout<<arr[i]<<" ";
}
return 0;
}
int sumOfArray ( int arr[], int size )
{
int sum=0;
// in the loop we are just calculating all elements sum
for(int i=0;i<size;i++)
{
sum=sum+arr[i];
}
return (sum);
}
int menu( char &character)
{
const int size=10;
int myArray[size]={0},max=0,min=0,secondmax=0,sum=0;
do
{
cin>>character;
if(character=='a')
{
system("cls");
cout<<" PLEASE ENTER DIGITS IN THE ARRAY "<<endl;
inputArray(myArray,size);
takeAction();
system ("pause");
system("cls");
}
else if(character=='b')
{
takeAction();
cout<<"THE ARRAY OF NUMBERS ENTERED BY YOU IS : "<<endl;
outputArray(myArray,size);
system("pause");
system("cls");
}
else if (character=='c')
{
takeAction();
max=maximum(myArray,size);
cout<<"THE MAXIMUM NUMBER IN THIS ARRAY IS : "<<max<<endl;
system ("pause");
system("cls");
}
else if(character=='d')
{
takeAction();
min=minimum(myArray,size);
cout<<"THE MINIMUM NUMBER IN THIS ARRAY IS : "<<min<<endl;
system ("pause");
system("cls");
}
else if (character=='e')
{
takeAction();
secondmax=secondhighest(myArray,size);
cout<<"THE SECOND HIGHEST NUMBER IN THIS ARRAY IS : "<<secondmax<<endl;
system ("pause");
system ("cls");
}
else if (character=='f')
{
takeAction();
cout<<"IN THE REVERSE ORDER THIS ARRAY IS : "<<endl;
reverseArray(myArray,size);
system ("pause");
system("cls");
}
else if(character=='g')
{
takeAction();
sum=sumOfArray(myArray,size);
cout<<" SUM OF ALL THE NUMBERS IN THIS ARRAY IS : "<<sum<<endl;
system ("pause");
system("cls");
}
}
while(character!='x');
return (character);
}
void takeAction()
{
cout<<" Please Follow the instructions "<<endl;
cout<<" a. Input the array from the user "<<endl;
cout<<" b. Output the array "<<endl;
cout<<" c. Find the maximum value in the array. "<<endl;
cout<<" d. Find the minimum value in the array. "<<endl;
cout<<" e. Find the second highest value in the array. "<<endl;
cout<<" f. Reverse the elements of the array "<<endl;
cout<<" g. Sum of Elements in Array "<<endl;
cout<<" Please press x to exit from main menu "<<endl;
}
https://www.facebook.com/invigorating.arslaan
#include<iostream>
using namespace std;
int menu( char &);
void takeAction();
int inputArray(int ,int );
int outputArray(int , int );
int maximum(int , int);
int minimum(int , int);
int secondhighest (int , int );
int reverseArray (int , int );
int sumOfArray ( int , int );
int main(){
char a=0;
takeAction();
menu(a);
system("Pause");
return 0;
}
int inputArray(int Arr[],int size)
{
// just using a loop to set inputs according to index.
for(int i=0;i<size;i++)
cin>>Arr[i];
return 0;
}
int outputArray(int Arr[], int size)
{
// just using looop to show the value of every index on console.
for(int i=0;i<size;i++)
cout<<Arr[i]<<" ";
return 0;
}
int maximum(int arr [], int size)
{
int max=0;
max=arr[0];
for(int i=0;i<size;i++)
{
// we are comparing a variable max with this complete array and updating its value to largest number we found in array.
if(arr[i]>max)
max=arr[i];
}
return (max);
}
int minimum(int arr[], int size)
{
int min=0;
min=arr[0];
for(int i=0;i<size;i++)
{
// we are doing same thing as in above function in this we are assigning smallest value to variable min.
if(arr[i]<min)
{
min=arr[i];
}
}
return (min);
}
int secondhighest (int arr[], int size)
{
int max=0,secondmax=0;
// this portion will work if we are only dealing with 0 to 10 numbers.
/*
for (int i=0;i<size;i++)
{
if(arr[i]>max)
{
secondmax=max;
max=arr[i];
}
else if(secondmax>arr[i] && secondmax!=max)
{
secondmax=arr[i];
}
}
return (secondmax);
*/
// we are just comparing 1st and second number in this array and in this case we are assigning smallest value to secondmax.
if(arr[0] > arr[1])
{
secondmax = arr[1];
max = arr[0];
}
else
{
secondmax = arr[0];
max = arr[1];
}
// after checking first two numbers then we will compare for number 3 , 4 ,5 and so on and will update the values of max and second max.
for(int i = 2; i < size; i++)
{
// we use >= not > because if there are two same large values then > sign would update values of both secondmax and max to that value.
if(arr[i] >= max)
{
secondmax=max;
max=arr[i];
}
else if(arr[i] > secondmax)
{
secondmax=arr[i];
}
}
return(secondmax);
}
int reverseArray (int arr[], int size)
{
// just using a loop and this loop would start from end to begin and will cout values accordingly loop.
// we size -1 instead of size beacuse if we take size it would print one extra value than required.
for(int i=size-1;i>=0;i--)
{
cout<<arr[i]<<" ";
}
return 0;
}
int sumOfArray ( int arr[], int size )
{
int sum=0;
// in the loop we are just calculating all elements sum
for(int i=0;i<size;i++)
{
sum=sum+arr[i];
}
return (sum);
}
int menu( char &character)
{
const int size=10;
int myArray[size]={0},max=0,min=0,secondmax=0,sum=0;
do
{
cin>>character;
if(character=='a')
{
system("cls");
cout<<" PLEASE ENTER DIGITS IN THE ARRAY "<<endl;
inputArray(myArray,size);
takeAction();
system ("pause");
system("cls");
}
else if(character=='b')
{
takeAction();
cout<<"THE ARRAY OF NUMBERS ENTERED BY YOU IS : "<<endl;
outputArray(myArray,size);
system("pause");
system("cls");
}
else if (character=='c')
{
takeAction();
max=maximum(myArray,size);
cout<<"THE MAXIMUM NUMBER IN THIS ARRAY IS : "<<max<<endl;
system ("pause");
system("cls");
}
else if(character=='d')
{
takeAction();
min=minimum(myArray,size);
cout<<"THE MINIMUM NUMBER IN THIS ARRAY IS : "<<min<<endl;
system ("pause");
system("cls");
}
else if (character=='e')
{
takeAction();
secondmax=secondhighest(myArray,size);
cout<<"THE SECOND HIGHEST NUMBER IN THIS ARRAY IS : "<<secondmax<<endl;
system ("pause");
system ("cls");
}
else if (character=='f')
{
takeAction();
cout<<"IN THE REVERSE ORDER THIS ARRAY IS : "<<endl;
reverseArray(myArray,size);
system ("pause");
system("cls");
}
else if(character=='g')
{
takeAction();
sum=sumOfArray(myArray,size);
cout<<" SUM OF ALL THE NUMBERS IN THIS ARRAY IS : "<<sum<<endl;
system ("pause");
system("cls");
}
}
while(character!='x');
return (character);
}
void takeAction()
{
cout<<" Please Follow the instructions "<<endl;
cout<<" a. Input the array from the user "<<endl;
cout<<" b. Output the array "<<endl;
cout<<" c. Find the maximum value in the array. "<<endl;
cout<<" d. Find the minimum value in the array. "<<endl;
cout<<" e. Find the second highest value in the array. "<<endl;
cout<<" f. Reverse the elements of the array "<<endl;
cout<<" g. Sum of Elements in Array "<<endl;
cout<<" Please press x to exit from main menu "<<endl;
}
Comments
Post a Comment