There will be two programs for C++ PROGRAM#1: Create the pseudo code and then co
ID: 3875335 • Letter: T
Question
There will be two programs for C++
PROGRAM#1:
Create the pseudo code and then code, debug and create the requisite submission files for Programming Challenge #1 (Largest/Smallest Array Values)
Largest/Smallest Array Values: Write a program that lets the user enter 10 values into an array. The program should then display the largest and smallest values stored in the array.
PROGRAM #2:
Recreate what was made in program#1, and this time use functions. You will need the following functions (These are prototypes):
void getUserInput(int [], int);
int findHigest(int[], int); //this function returns the highest value in the array
int findLowest(int[],int);//this function returns the lowest value in the array
void displayArray(int[], int);
Use these 3 important pieces:
1. prototype the function with parameters that indicate the type of array followed by square brackets and then have another parameter that will hold the array size. (as I've shown above)
2. call the function by naming the function and then passing it the name of the array and the size. ie, getUserInput(myArray, SIZE);
3. define the function with parameters that indicate the type of array, it's name, followed by square brackets and then have the 2nd parameter be the size.
Example:
void getUserInput(int numbers[], int sizeOfArray)
{
etc....
Explanation / Answer
PROGRAM 1
int main()
{
int arr[],n;
cout << :Enter size of array";
cin>>n;
cout << "Enter elements:";
for (int i=0;i<n;i++)
cin>> arr[i];
int max = arr[0];
int min= arr[0];
// Traverse array elements from second and
// compare every element with current max
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
for (int i = 1; i < n; i++)
if (arr[i] <min)
min = arr[i];
printf("Largest in given array is %d", max);
printf("Smallest in given array is %d", min);
return 0;
}
PROGRAM 2
void getUserInput(int [], int);
int findHigest(int[], int);
int findLowest(int[],int);
void displayArray(int[], int);
int main()
{
int arr[],n;
cout << :Enter size of array";
cin>>n;
getUserInput(arr,n);
return 0;
}
void getUserInput(int arr[], int n)
{
cout << "Enter elements:";
for (int i=0;i<n;i++)
cin>> arr[i];
displayArray(arr,n);
int max= findHighest(arr,n);
int min= findLowest(arr,n);
}
int findHigest(int arr[], int n)
{
int max = arr[0];
// Traverse array elements from second and
// compare every element with current max
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
printf("Largest in given array is %d", max);
return max;
}
int findLowest(int[] arr,int n)
{
int min= arr[0];
for (int i = 1; i < n; i++)
if (arr[i] <min)
min = arr[i];
printf("Smallest in given array is %d", min);
return min;
}
void displayArray(int arr[], int n)
{for (int i=0;i<n;i++)
cout<< arr[i];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.