Question

Write a program that will do the following. The main function should ask the user how...

Write a program that will do the following.

  • The main function should ask the user how many numbers it will read in.
  • It will then create an array of that size.
  • Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This will be a void function, since it changes the contents of the array but does not return a separate result.
  • Next, the main function will call another function that will find the average of the array values (averageArray). Pass the array as a parameter to this function. It will use a loop to add the numbers in the array to calculate the total. After the loop is finished, divide the total by the number of array elements to get the average. Return this average to themain function. The main function will print the average.
  • Next, the main function will call a function named printArray that displays the contents of the array before they are sorted.
  • Next, the main function will call another void function to sort the entire array (sortArray).
  • Finally, from the main function, call printArray to print the contents of the sorted array

language: C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

#include <iostream>
#include <array>

using namespace std;

//function to fill the array , pass array name as parameter and size of the array
void fillArray(int a[],int n){
int size = n;
//iterate throught array to fill it
for(int i = 0; i < size ;i++){
//ask value from user and stor in array
cout<<"\nEnter value of "<<(i+1)<<" element ";
cin>>a[i];
}
  
}
//find the average of the array
int averageArray(int a[],int n){
//variable declare
int sum = 0;
//iterate throught array and calculate the sum
for(int i = 0; i < n ;i++){
sum+=a[i];
}
//return the sum/n ie the avergae value
return sum/n;
}
//print the array value
void printArray (int a[],int n){
//iterate through the array and print the iterated values
for(int i = 0; i < n ;i++){
cout<<"\nValue of "<<(i+1)<<" element is "<<a[i];
}
}
//function to swap number used in sorting array
void swapNumber(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//function to sort array , used bubble sort here to sort it   
void sortArray(int a[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)   

for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1])
swapNumber(&a[j], &a[j+1]);
}

int main()
{
int n;
//ask user to numbers of element in array
cout<<"How many numbers it will read in ?";
cin>>n;
//initialize the array
int a[n];
//fill the array
fillArray(a,n);
//find the average
int avg = averageArray(a,n);
cout<<"\n Average is "<<avg;
//function to print the array
printArray(a,n);
//function to sort the arrya
sortArray(a,n);
cout<<"\n";
printArray(a,n);
return 0;
}

Screenshots -

Add a comment
Know the answer?
Add Answer to:
Write a program that will do the following. The main function should ask the user how...
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++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that...

    Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that searches for a number in an array. T a. Write a bool function named search that receives 3 parameters: an int array, an int containing the length of the array, and an int number to search for. Return true if the number is in the array and false otherwise. b. Write a void function called fillArray with the array as a parameter that uses...

  • 1.You are to write a program name search.java that will do the following: 2.You are to...

    1.You are to write a program name search.java that will do the following: 2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C. 3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99. 4.Write 1 sequential search...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • dont use switch E) 120 pts.] Write another function int main ) in C++(and comment the...

    dont use switch E) 120 pts.] Write another function int main ) in C++(and comment the main in Q1.D) that displays the following menu using a do while loop: please choose from the following Menu: I: Populate array with 5 elements 2: Print Array 3: Reverse Array 0: Exit Based, on the user choice, you should call the appropriate function. For example, if the user: - chooses 1, then in function main you should call function populateArray (A, 5); -...

  • l ask Write a C++ program that accomplishes the following tasks 1. Write a function that...

    l ask Write a C++ program that accomplishes the following tasks 1. Write a function that finds the average of current element and the next element in given array named as "elements" and stores the new found averages into another array called "averageArray". Note: For the last element you can just divide the value of last element by 2 Write a function that takes both the given array and average array you just created along with a variable "productofArrays" using...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • In C++ Write a function called fillArray that will fill an array of any constantly declared...

    In C++ Write a function called fillArray that will fill an array of any constantly declared array size variable with random numbers in the range of 1 - 100. Write a function called printArray that will print an array of any size. Write a third function called countEvens which will count and return the number of even numbers in the array. In main create an array that holds 25 ints. Use your functions to fill, print, and count the number...

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