Question

The purpose of this lab is to manipulate an array of integers. The assignment is to...

The purpose of this lab is to manipulate an array of integers. The assignment is to write a program that:

1. Declares an array of integers of some maximum size 20.

2. Implements the following functions:

ü A function that prompts the user to initialize the array with up to 20 non-negative whole numbers and mark the end of the input with a negative number. The entire array does not need to be initialized. The user may enter only as many (or as few) values as they would like in the array. Hint: This implies that the number of elements in the array does not necessarily have to be the same as the maximum size of the array.

ü A function that displays the contents of the partially filled array. Important: Each of the remaining functions described below should be independent of all cin and cout statements. In other words, any and all information required by the function should be passed in through parameters. Likewise, information from the function that the caller needs should be returned back either through the return statement or through reference parameters. All information required should be input by the user prior to the function being invoked and all returned information should be output after the function returns control to the caller.

ü A function that calculates the minimum, maximum, sum and average of all the elements currently contained in the partially filled array. Note that this information should all be calculated in one function. ü A function that accepts a number and determines whether that number occurs in the partially filled array. ü A function that accepts a number and determines how many times (if any) that number appears as an element in the partially filled array.

Sample Run 1 of the program:

Please enter up to 20 non-negative whole numbers separated by space. Mark the end of the input list with a negative number:

7 6 82 8 9 6 -1

7 6 82 8 9 6

The minimum value in the array is 6

The maximum value in the array is 82

The sum of all the elements in the array is 118

The average of all the elements in the array is 19.67

Please enter the value you want to search in the array: 7

Find your target 7

Please enter the value you want to know the frequency of: 6

The number 6 has occurred 2 time(s) in the array

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

#include <iostream>
#include<iomanip>
using namespace std;

int initialize_array(int arr[], int SIZE)
{
// total_numbers represent the numbers entered by user
int total_numbers = 0;
int value =0;
cout<<"Please enter up to 20 non-negative whole numbers separated by space. Mark the end of the input list with a negative number:"<<endl;

// Iterate loop till total_numbers < maximum size of array i.e. SIZE
while (total_numbers<SIZE)
{
// Enter value, if value is -1 exit loop
cin>>value;
if(value==-1)
{
break;
}
// Else, store number to array
// Increment total_numbers by 1
arr[total_numbers] = value;
total_numbers+=1;
}
// Return total_numbers
return total_numbers;
}

void display_array(int arr[], int total_numbers)
{
// Iterate over array and print every element
for(int i =0; i<total_numbers; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}

void array_func(int arr[], int total_numbers, int &min, int &max, int &sum, double & average)
{
// Initially set min and max to first element and sum to 0
min = arr[0];
max = arr[0];
sum = 0;
// Iterate over array
for(int i =0; i<total_numbers; i++)
{
// If min > current element, set min = current element
if(min>arr[i])
{
min = arr[i];
}
// If max < current element, set max = current element
if(max<arr[i])
{
max = arr[i];
}

// Add arr[i] to sum
sum+=arr[i];
}
// Calculate average
average = (double)sum/total_numbers;
}

bool search_number(int arr[], int total_numbers, int number)
{
// Iterate over array
// If number found return true
for(int i =0; i<total_numbers; i++)
{
if(arr[i]==number)
{
return true;
}
}
// Return false
return false;
}

int occurrence(int arr[], int total_numbers, int number)
{
int count = 0;
// Iterate over array
// If number found, increment count by 1
for(int i =0; i<total_numbers; i++)
{
if(arr[i]==number)
{
count+=1;
}
}
// return count
return count;
}
int main()
{
int SIZE = 20;
int arr[SIZE];
int sum,min,max;
double average;
int target;
int num;

int total_numbers = initialize_array(arr,SIZE);

display_array(arr,total_numbers);

array_func(arr,total_numbers,min,max,sum,average);
cout<<"The minimum value in the array is "<<min<<endl;
cout<<"The maximum value in the array is "<<max<<endl;
cout<<"The sum of all the elements in the array is "<<sum<<endl;
cout<<"The average of all the elements in the array is "<<fixed<<setprecision(2)<<average<<endl;
cout<<"Please enter the value you want to search in the array: ";
cin>>target;

if(search_number(arr,total_numbers,target))
{
cout<<"Find your target "<<target<<endl;
}
else
{
cout<<"Couldn't find your target "<<target<<endl;
}

cout<<"Please enter the value you want to know the frequency of: ";
cin>>num;
int count = occurrence(arr,total_numbers,num);
cout<<"The number "<<num<<" has occurred "<<count<<" time(s) in the array"<<endl;
return 0;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
The purpose of this lab is to manipulate an array of integers. The assignment is to...
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
  • 1. Create a file that contains 20 integers or download the attached file twenty-integers.txt twenty-integers.txt reads:...

    1. Create a file that contains 20 integers or download the attached file twenty-integers.txt twenty-integers.txt reads: 12 20 13 45 67 90 100 34 56 89 33 44 66 77 88 99 20 69 45 20 Create a program that: 2. Declares a c-style string array that will store a filename. 3. Prompts the user to enter a filename. Store the file name declared in the c-string. 4. Opens the file. Write code to check the file state. If the...

  • Write a program that reads a sequence of integers into an array and that computes the...

    Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data: 2 1 4 9 16 9 7 4 9 11 Then it computes 2 - 1 + 4 - 9 + 16 - 9 + 7 - 4 + 9 – 11 = 4 Use a scanner object to gather the inputs from the user....

  • Write a function to have a user enter some number of integers into an array. The...

    Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • please use c++ language 1. Request three different integers from the console. a) Write a swap...

    please use c++ language 1. Request three different integers from the console. a) Write a swap function that swaps two integer values. b) Write a sort function which accepts three integers as input then sorts them from largest to smallest using the swap function. c) Output the integers before and after sorting. Example 1 Output (input in bold italics) Enter three different integers: 3 2 4 3 2 4 4 3 2 Example 2 Output (input in bold italics) Enter...

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

  • 2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum...

    2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum of both of the integers and all the numbers in between Write a Marie program that determines the largest of a series of positive integers provided by a user. The user will enter a -1 when she is finished. Up to 10 numbers will be provided by the user. Write a Marie program that accepts two positive integers, multiples them by repeated addition, and...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

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