Main Function: Declare and fill an array with 1000 random integers between 1 - 1
ID: 3585392 • Letter: M
Question
Main Function: Declare and fill an array with 1000 random integers between 1 - 1000.
You will pass the array into functions that will perform operations on the array.
Function 1:
Create a function that will output all the integers in the array. (Make sure output is clearly formatted.
Function 2:
Create a Function that will sum and output all the odd numbers in the array.
Function 3:
Create a Function that will sum and output all the even numbers in the array.
Function 4:
Create a function that will allow the user to enter an integer value to search. Program will output if integer value is found and the location of the integer in the array. or The program will output “value is not in array”. (“value is not in array should not be printed more than once.”)
Function 5:
Create a function that will output the highest value in the array.
Function 6:
Create a function that will output the lowest value in the array.
Function 7:
Implement a Bubble Sort algorithm. Sort the elements in the array and output in descending order.
Explain how your bubble sort algorithm works in your own words below
Make sure you have a menu operating the program until user decides to exit.
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Please be clear for your comments and post the correct code with execution window strictly following the guidelines given above. Please make it understandable to both User and Editor. It should be in C++ language. Thanks in advance.
Explanation / Answer
Please find the required solution:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void printmenu()
{
cout<<"Enter 1. Output all integer values" << endl;
cout<<"Enter 2. Sum all odd numbers" << endl;
cout<<"Enter 3. Sum all even numbers" << endl;
cout<<"Enter 4. Enter a search value" << endl;
cout<<"Enter 5: Output the Highest Number" << endl;
cout<<"Enter 6: Output the Lowest Number" << endl;
cout<<"Enter 7: Sort the items in the array and Output in Descending Order" << endl;
cout<<"Enter 8: Output First Number in the Array" << endl ;
cout<<"Enter 9: Output Last Number in the Array" << endl;
cout<<"Enter 10: To Exit Program"<< endl;
}
void printarray(int *array, int size)
{
int i;
for(i=0; i< size;i++ )
{
cout << "("<<i<<")"<<array[i]<<" ";
if(i%20 == 0)
cout<<endl;
}
}
void sum_odd(int *array, int size)
{
int result=0, i;
for(i=0;i<size;i++ )
{
if(array[i]%2 != 0)
result+=array[i];
}
cout<<"sum of odd numbers:" << result;
}
void sum_even(int *array, int size)
{
int result=0, i;
for(i=0;i<size;i++ )
{
if(array[i]%2 == 0)
result+=array[i];
}
cout<<"sum of even numbers:" << result;
}
void search(int *array, int size)
{
int inp, i;
cout<<"enter search value:";
cin >>inp;
bool found =false;
for(i=0;i< size;i++ )
{
if(array[i] == inp) {
found =true;
cout << inp << " is present in array at in index "<< i << endl;
}
}
if(!found)
cout << inp << " is not in array.";
}
void large(int *array, int size)
{
int result = array[0];
for(int i=1;i<size;i++)
{
if(array[i] > result)
result = array[i];
}
cout << "The Highest number is: " << result << endl;
}
void small(int *array, int size)
{
int result = array[0];
for(int i=1;i<size;i++)
{
if(array[i] < result)
result = array[i];
}
cout << "The lowest number is: " << result << endl;
}
void bubblesort(int *arr, int n)
{
int i, j, temp;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] < arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] =temp;
}
printarray(arr, n);
}
int main()
{
int inp, i, num = 1000;
int array[num],result;
//generate random numbers
srand((unsigned)time(0));
for(i=0;i<num;i++)
{
array[i]= rand() % 1000;
}
printmenu();
cout << "Enter input from menu:";
cin >> inp;
while(inp !=10 )
{
switch(inp)
{
case 1: printarray(array, num);
break;
case 2: sum_odd(array,num);
break;
case 3: sum_even(array,num);
break;
case 4: search(array,num);
break;
case 5: large(array,num);
break;
case 6: small(array, num);
break;
case 7: bubblesort(array, num);
break;
case 8: cout << "First number in array:" << array[0];
break;
case 9: cout << "Last number in array:" << array[num-1];
break;
case 10:
break;
default:
cout<<"Invalid option selected";
}
cout<< endl;
printmenu();
cout << "Enter input from menu:";
cin >> inp;
}
cout<<"bye... end of program";
return 0;
}
Sample output(with array_size=10):
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Enter input from menu:1
693
34 340 191 340 446 664 907 795 330
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Enter input from menu:8
First number in array:693
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Enter input from menu:9
Last number in array:330
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Enter input from menu:7
907
795 693 664 446 340 340 330 191 34
Enter 1. Output all integer values
Enter 2. Sum all odd numbers
Enter 3. Sum all even numbers
Enter 4. Enter a search value
Enter 5: Output the Highest Number
Enter 6: Output the Lowest Number
Enter 7: Sort the items in the array and Output in Descending Order
Enter 8: Output First Number in the Array
Enter 9: Output Last Number in the Array
Enter 10: To Exit Program
Enter input from menu:10
bye... end of program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.