Question

Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 -...

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.

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.”)

Make sure you have a menu operating the program until the 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: Exit

Task 2

Implement a class for Task 1 and Hardcode algorithms for Linear Search, Middle Value, First Value, Last Value, Highest Value, Lowest Value, and Bubble Sort.

Main Function: Declare and fill an array with 1000 random integers between 1 - 1000.

Class will contain the methods below and an array as the data structure.

Please use comments to identify constructors and methods in the program.

Language is in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TASK 1 :

#include<iostream> // for cin and cout
#include<stdlib.h> // for rand function
using namespace std;
// Function to display all the elements of the array
void display(int arr[])
{
for(int i=0;i<1000;i++)
{
cout<<"Element "<<i+1<<" = "<<arr[i]<<"\n";
}
}
// Function to sum all the odd elements of the array
void sumodd(int arr[])
{
// Initialize sum as 0
int s=0;
for(int i=0;i<1000;i++)
{
// If a number is not divisible by 2 then it is odd
if(arr[i]%2!=0)
{
s=s+arr[i];
}
}
cout<<"Sum of all odd elements "<<s<<"\n";
}
// Function to sum all the even elements of the array
void sumeven(int arr[])
{
// Initialize sum as 0
int s=0;
for(int i=0;i<1000;i++)
{
// If a number is divisible by 2 then it is even
if(arr[i]%2==0)
{
s=s+arr[i];
}
}
cout<<"Sum of all even elements "<<s<<"\n";
}
// Function to search for an element in the array
void searchvalue(int arr[],int ele)
{
int found=0,pos;
for(int i=0;i<1000;i++)
{
// If the element is found then break from the array
if(arr[i]==ele)
{
pos=i;
found=1;
break;
}
}
if(found)
{
cout<<"Element found at position "<<pos+1<<"\n";
}
else
{

cout<<"Element is not present in the array\n";
}

}
int main()
{
// Initialize the array
int arr[1000],i;
// Use random function for generating random numbers in the range 1-1000
for(i=0;i<1000;i++)
{
arr[i]=rand() % 1000 + 1;
}
// Run an infinite loop until 5 is pressed for exit
while(1)
{
// display the menu
cout<<"Enter 1. Output all integer values\n";
cout<<"Enter 2. Sum all odd numbers\n";
cout<<"Enter 3. Sum all even numbers\n";
cout<<"Enter 4. Enter a search value\n";
cout<<"Enter 5. Exit\n";
int choice,ele;
cin>>choice;
// if the choice is 5 then break
if (choice==5)
{
cout<<"You choose to exit\n";
break;
}
else
{
switch(choice)
{
case 1:display(arr);
break;
case 2:sumodd(arr);
break;
case 3:sumeven(arr);
break;
case 4:cout<<"Enter an element to search \n";
cin>>ele;
searchvalue(arr,ele);
break;
default:cout<<"Wrong Choice"<<"\n";
break;

}
}

}

return 0;
}

TASK 2:

#include<iostream> // for cin and cout
#include<stdlib.h> // for rand function
using namespace std;
// define a class for task 1
class A
{
public:
// Function to sort the array using bubble sort
void bubble_sort(int arr[])
{
for(int i=0;i<1000;i++)
{
for(int j=i+1;j<1000;j++)
{
if(arr[i]<arr[j])
{
swap(arr[i],arr[j]);
}
}
}
cout<<"The array after bubble sort is\n";
for(int i=0;i<1000;i++)
{
cout<<"Element "<<i+1<<" = "<<arr[i]<<"\n";
}
}
// Function to find the middle value
void mid_value(int arr[])
{
int pos=1000/2;
cout<<"The middle value is "<<arr[pos]<<"\n";
}
// Function to find the first value
void first_value(int arr[])
{
int pos=0;
cout<<"The first value is "<<arr[pos]<<"\n";
}
// Function to find the last value
void last_value(int arr[])
{
int pos=1000-1;
cout<<"The last value is "<<arr[pos]<<"\n";
}
// Function to find the highest value
void highest_value(int arr[])
{
int ans=-1;
for(int i=0;i<1000;i++)
{
if(ans<arr[i])
{
ans=arr[i];
}
}
cout<<"The highest value in the array is "<<ans<<"\n";
}
// Function to find the lowest value
void lowest_value(int arr[])
{
int ans=1001;
for(int i=0;i<1000;i++)
{
if(ans>arr[i])
{
ans=arr[i];
}
}
cout<<"The lowest value in the array is "<<ans<<"\n";
}
// Function to display all the elements of the array
void display(int arr[])
{
for(int i=0;i<1000;i++)
{
cout<<"Element "<<i+1<<" = "<<arr[i]<<"\n";
}
}
// Function to sum all the odd elements of the array
void sumodd(int arr[])
{
// Initialize sum as 0
int s=0;
for(int i=0;i<1000;i++)
{
// If a number is not divisible by 2 then it is odd
if(arr[i]%2!=0)
{
s=s+arr[i];
}
}
cout<<"Sum of all odd elements "<<s<<"\n";
}
// Function to sum all the even elements of the array
void sumeven(int arr[])
{
// Initialize sum as 0
int s=0;
for(int i=0;i<1000;i++)
{
// If a number is divisible by 2 then it is even
if(arr[i]%2==0)
{
s=s+arr[i];
}
}
cout<<"Sum of all even elements "<<s<<"\n";
}
// Function to search for an element in the array
void lsearch(int arr[],int ele)
{
int found=0,pos;
for(int i=0;i<1000;i++)
{
// If the element is found then break from the array
if(arr[i]==ele)
{
pos=i;
found=1;
break;
}
}
if(found)
{
cout<<"Element found at position "<<pos+1<<"\n";
}
else
{

cout<<"Element is not present in the array\n";
}

}
};


int main()
{
// Make an object of the array
A obj;
// Initialize the array
int arr[1000],i;
// Use random function for generating random numbers in the range 1-1000
for(i=0;i<1000;i++)
{
arr[i]=rand() % 1000 + 1;
}
// Run an infinite loop until 5 is pressed for exit
while(1)
{
// display the menu
cout<<"Enter 1. Output all integer values\n";
cout<<"Enter 2. Sum all odd numbers\n";
cout<<"Enter 3. Sum all even numbers\n";
cout<<"Enter 4. Enter a search value\n";
cout<<"Enter 5. Exit\n";
int choice,ele;
cin>>choice;
// if the choice is 5 then break
if (choice==5)
{
cout<<"You choose to exit\n";
break;
}
else
{
switch(choice)
{
case 1:obj.display(arr);
break;
case 2:obj.sumodd(arr);
break;
case 3:obj.sumeven(arr);
break;
case 4:cout<<"Enter an element to search \n";
cin>>ele;
obj.lsearch(arr,ele);
break;
default:cout<<"Wrong Choice"<<"\n";
break;

}
}

}

// hard_coded algorithms for linear_search,middle_value,first_value
// last_value,highest_value,lowest_value and bubble_sort
cout<<"Enter an element to search\n";
int e1;
cin>>e1;
obj.lsearch(arr,e1);
obj.mid_value(arr);
obj.first_value(arr);
obj.last_value(arr);
obj.highest_value(arr);
obj.lowest_value(arr);
obj.bubble_sort(arr);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT