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
  • 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...

  • 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....

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

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • 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...

  • 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...

  • Create a class called Reverse that reverses an array of integers. The class should have two...

    Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...

  • 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...

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