5. Write a function which will find the average of all elements of an array of integers. Pass an array and a dimension (the number of elements), and return the value of the average as a float ( the return value). Test this function in the main
A function which will find the average of all elements of an array of integers.
float average(int array[], int dimension)
{
//declare local variable avg of float type
float avg;
//declare integer type variable sum
int sum=0;
//for loop run until dimension
for(int i=0; i<dimension; i++)
{
//compute sum
sum=sum+array[i];
}
//compute average
avg=sum/dimension;
//retrun avg
return avg;
}
The following program is written in the C++ programming language.
Program:
//included header file
#include <iostream>
//included namespace
using namespace std;
//function which will find the average
float average(int array[], int dimension)
{
//declare local variable avg of float type
float avg;
//declare integer type variable sum
int sum=0;
//for loop run until dimension
for(int i=0; i<dimension; i++)
{
//compute sum
sum=sum+array[i];
}
//compute average
avg=sum/dimension;
//retrun avg
return avg;
}
//main function
int main ()
{
//declared array with given elements
int array[]={5, 7, 3, 5, 6, 4};
//declared flaot type variable avg
float avg;
//declared variable dimension of integer type
int dimension=6;
//calling function average
avg=average(array, dimension);
//print average of all elements of an array of integers
cout<<"The average of all elements of an array of integers is:"<<avg;
return 0;
}
Step by stepSolved in 3 steps with 1 images
- Carry out the following task with an array: a. Allocate an arraya of ten integers. b. Put the number 17 as the initial element of the array. c. Put the number 29 as the last element of the array. d. Fill the remaining elements with –1. e. Add 1 to each element of the array.f. Print all elements of the array, one per line. g. Print all elements of the array in a single line, separated by commas.h. Count how many elements are less than a given value (entered by user). i. Remove all elements that are less than a given value (entered by user). j. Place all elements that are less than a given value (provided by user) in another array.arrow_forwardwrite a c++ program: Create an array with 11 integers, which will be randomly selected from the range of 0 to 100. Print the items of the array on screen as one line. Develop a function that takes the array as argument and perform these operations: -Find the minimum of array items and replace (swap) with first item of the array. -Find the maximum of array items and replace (swap) with last item of the array. -Find the average of array’s items and assign it to middle location of the array. The average of numbers should be calculated as an integer. (hint: static_cast<int>(float))arrow_forwardHello, can you please help me do this Java program using the two attached pics, here are the instructions: Problem Description:Use a one-dimensional array to solve the following problem:Request five unique numbers, each of which is between 10 and 100, inclusively. As each number is read,display it only if it is not a duplicate of a number already read, providing for the “worst case”, which is allfive numbers are different.Use the smallest possible array to solve this problem. Display the complete set of unique values inputafter the user inputs each new value.Programming Steps1. Download the provided programs Project7_UniqueNumbers.java andProject7_UniqueNumbersTest.java2. Complete the programs to use arrays in your compilera. There are nine (9) TODOsi. Seven (7) in Project7_UniqueNumbers.javaii. Two (2) in Project7_UniqueNumbersTest.javab. You should add necessary statements/structures to the given programs as instructed inthe comments3. Debug and run your applicationarrow_forward
- It's important to note that the name of an array by itself is really a pointer to the first element of the array. Therefore passing an array to function is simple since you can just specify the name of the array. Are these statements true?arrow_forward1- Write a user-defined function that accepts an array of integers. The function should generate the percentage each value in the array is of the total of all array values. Store the % value in another array. That array should also be declared as a formal parameter of the function. 2- In the main function, create a prompt that asks the user for inputs to the array. Ask the user to enter up to 20 values, and type -1 when done. (-1 is the sentinel value). It marks the end of the array. A user can put in any number of variables up to 20 (20 is the size of the array, it could be partially filled). 3- Display a table like the following example, showing each data value and what percentage each value is of the total of all array values. Do this by invoking the function in part 1.arrow_forwardWrite 3 functions: Write int readNumbersIntoArray(int numbers[], int size); // Reads up to size numbers or a non-numeric and puts them in the array. // Returns how many numbers read. Write int minOrMax(int numbers[], int size, bool Max); // Returns the minimum or the maximum value in the array depending on "Max". // If "Max" is true, then it returns the maximum. // If "Max" is false, then it returns the minimum. Write double trimmedAvg(int numbers[], int size); // Returns the trimmed average of the array. // A trimmed average is a normal average but leaves out the first instance of // the smallest and largest value in the array when doing the calculation. // HINT: use calls to minOrMax() to get the largest and smallest number. Write int main(): // Prompt the user for between 3 and 20 numbers and use readNumbersIntoArray() to // read them into an array. Print an error message and exit if less than three numbers. // Print out the min, max, and trimmedAverage of the array using calls…arrow_forward
- Program: Using a multidimensional array, create a triangular-shaped array. You will ask the user how many lines they want to see and then create the array, fill it, and then print it. You will fill the array with one 1 in the first row, two 2’s in the second row, etc. This should work for any integer that the user enters. (Just because I am starting on 1 does not mean row 0 was skipped.) You must: use a loop to create the array shape. You must: use nested for loops to fill the array and to print the values back to the screen. Your program should print as shown below. Example Output: How many lines would you like in your triangle? >>>9 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 If your code looks like the code below, it is not what I’m asking for. The code below is making a square multidimensional array, not a triangular one. The code below is just leaving certain spots empty so that it looks like a triangle. I will ask you…arrow_forwardWrite a function that determines the average of the values in an array. In the main() function declare a floating point array with a length of 8 elements, and have the user enter the value of each element using a loop. The program should then call and pass the array to the function, then display the average.arrow_forwarda. Problem 1. Create an array of 30 random numbers that range between 1 and 100. Then, write a function that will receive a number from the user and determine if that number exists in the array or not. For instance, assume the array is: [2, 93, 14, 89, 12, 3, 81, 15, 14, 89, 52, 96, 71, 82, 5, 2, 41, 23, 52, 59, 44, 44, 88, 39, 49, 50, 97, 45, 48, 36] Now, assume the user enters 89, the program should output true. But, if the user enters 77, the program should output false. Approach: We will be implementing this method in two different ways. Both will be recursive. First, implement a method called findA (x,A), where x is the number we are looking for and A is an array. In the body of the function, compare x with the FIRST item that is in the array. If this first item is equal to X, return true. If not, remove the first item from A and call findA (x,A) on the revised list. If you call find on an empty list, you will want to return false. Writing any explicit loop in your code results a…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education