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
  • c++ Gebremed、e Diagram TX ent/d/1cukolouus-Gqbal8MgX-znWzblaCw Xbct4azy5f6Mi/edit Search Divide and Conquer. Y ) (132) Zayn x |...

    c++ Gebremed、e Diagram TX ent/d/1cukolouus-Gqbal8MgX-znWzblaCw Xbct4azy5f6Mi/edit Search Divide and Conquer. Y ) (132) Zayn x | Shared wit x Task 3: Task 2 (Repurpose Task 2 Assignment 3): Write a program that uses an array to find the Average of a set float values entered by the user You will have the main function control the operation of the program, but all values will be stored in the class. You will need to use get and set methods in your...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare...

    c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare an array of 10 integers. 2. Call a method to interactively fill the array with any number of values (up to 10) until a sentinel value is entered. [If an entry is not an integer, continue prompting the user until an integer is entered.] When fewer than 10 integers are placed into the array, your statistics will be off unless you resize the array...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

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