Task 1: Main Function: Declare and fill an array with 1000 random integers betwe
ID: 3590414 • Letter: T
Question
Task 1: 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. Function1 Create a function that will output all the integers in the array. (Make sure output is clearly formatted 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 output "value is not in array". ("value is not in array should not be printed more than once Function 6Explanation / Answer
#include <iostream>
#include <random>
using namespace std;
void output_array(int *random_array)
{
cout << "Your Randomly generated Array is: ";
for(int i=0; i<1000; i++)
cout << random_array[i] << ", ";
cout <<" ";
return;
}
void odd_numbers(int *random_array)
{
long sum=0;
cout << " Odd numbers in the Array are: ";
for(int i=0;i<1000;i++)
if(random_array[i]%2==1)
{
sum=sum+random_array[i];
cout << random_array[i] << ", ";
}
cout << " Sum of all Odd numbers = " << sum <<" ";
return;
}
void even_numbers(int *random_array)
{
long sum=0;
cout << " Even numbers in the Array are: ";
for(int i=0;i<1000;i++)
if(random_array[i]%2==0)
{
sum=sum+random_array[i];
cout << random_array[i] << ", ";
}
cout << " Sum of all Even numbers = " << sum <<" ";
return;
}
void search_value(int *random_array,int val)
{
for(int i=0;i<1000;i++)
{
if(random_array[i]==val)
{
cout << "Your value " << val <<" is found at location "<<i+1<<" in the array ";
return;
}
}
cout << "value is not in the array ";
return;
}
void highest(int *random_array)
{
int max=random_array[0];
for(int i=0;i<1000;i++)
{
if(max<random_array[i])
max=random_array[i];
}
cout << " Highest element in the array is : " << max<<" ";
return;
}
void lowest(int *random_array)
{
int low=random_array[0];
for(int i=0;i<1000;i++)
{
if(low>random_array[i])
low=random_array[i];
}
cout << " Lowest element in the array is : " << low<<" ";
return;
}
void bubble_sort(int *random_array)
{
int temp;
for (int i = 0; i < 1000-1; i++)
for (int j = 0; j < 1000-i-1; j++)
if (random_array[j] > random_array[j+1])
{
temp=random_array[j+1];
random_array[j+1]=random_array[j];
random_array[j]=temp;
}
cout << "Your Sorted Array in Descending order is: ";
for(int i=1000-1;i>=0;i--)
cout << random_array[i] << ", ";
cout << " ";
return;
}
void out_first(int *random_array)
{
cout<< "First number in the array = "<< random_array[0]<<" ";
return;
}
void out_last(int *random_array)
{
cout<< "Last number in the array = "<< random_array[1000-1]<<" ";
return;
}
int main()
{
int random_array[1000],flag=0,search_val;
random_device rand; // obtain a random number from hardware
mt19937 gt(rand()); // seed the generator
uniform_int_distribution<> range(1, 1000); // define the range
for(int i=0; i<1000; i++)
random_array[i]=range(gt);
while(1){
cout << "Please select your operation: ";
cout << "1. Output all integer values ";
cout << "2. Sum all odd numbers ";
cout << "3. Sum all even numbers ";
cout << "4. Enter a search value ";
cout << "5. Output Highest number ";
cout << "6. Output lowest number ";
cout << "7. Sort the item in the array and output in descending order ";
cout << "8. Output first number in the array ";
cout << "9. output last number in the array ";
cout << "10. Exit the program ";
cin>>flag;
if (cin.fail())
{
cout << "Please enter a valid option";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
continue;
}
switch (flag)
{
case 1:
output_array(random_array);
break;
case 2:
odd_numbers(random_array);
break;
case 3:
even_numbers(random_array);
break;
case 4:
{
cout<<"please enter search value";
cin>>search_val;
search_value(random_array,search_val);
break;
}
case 5:
highest(random_array);
break;
case 6:
lowest(random_array);
break;
case 7:
bubble_sort(random_array);
break;
case 8:
out_first(random_array);
break;
case 9:
out_last(random_array);
break;
case 10:
break;
default:
cout<<"Invalid option: ";
break;
}
if(flag==10)
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.