Question

In: Computer Science

In C++ Write a function that accepts two int arrays of the same size. The first...

In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be:

(show contents of 1st array)

The numbers that are greater than the average of the first array are: (the numbers)

so if the array is 1 2 3 4 5 . The numbers outputted should be 3 4 and 5 because 15/5=3 and 3 4 and are greater than and equal to 3.

I tried this code but the errors returned were: expression must have a constant value #include using namespace std; void fillArray(int arr1[],int arr2[],int arr_size) { int cnt=0; double avg,sum; for(int i=0;i=avg)//condition to check average { arr2[cnt]=arr1[i]; //assigning value to array 2 cnt++; } } //printing seond array cout<<"Second array is: "; for(int i=0;i>arr_size; int arr1[arr_size],arr2[arr_size]; cout<<"Enter the element:\n"; for(int i=0;i>arr1[i]; } fillArray(arr1,arr2,arr_size); }

also this code is incomplete:

#include <iostrecam>
using namespace std;

// function declaration
void fun(int arr1[], int arr2[], int size){

   cout<<"Content of 1st array: "<<endl;
   for(int i=0; i<size; i++)
       cout<<arr1[i]<<" ";
   cout<<endl;

   // taking input for 2nd array from user
   cout<<"\nEnter "<<size<<" integers :"<<endl;
   for(int i=0; i<size; i++)
       cin>>arr2[i];

   cout<<"\nContent of 2st array: "<<endl;
   for(int i=0; i<size; i++)
       cout<<arr2[i]<<" ";
   cout<<endl;
}

int main(){

   // declaring two array of size 6 and initializing first array with some value
   int arr1[] = {1,2,3,4,5,6};
   int arr2[6];

   // calling function
   fun(arr1, arr2, 6);

   return 0;
}

Solutions

Expert Solution

Code: I modifies your code to get correct output

#include <iostream>
using namespace std;

// function declaration
void fun(int arr1[], int arr2[], int size){

double sum=0,avrg;
cout<<"Content of 1st array: "<<endl;
for(int i=0; i<size; i++)
{
cout<<arr1[i]<<" ";
sum=sum+arr1[i];//caluclating Sum;
}

cout<<endl;

avrg=sum/size;//caluclating Average

int count=0;
//Storing values to array2 if its greater than average
for(int i=0;i<size;i++){
if(arr1[i]>=avrg){
arr2[count]=arr1[i];
count++;}
}

cout<<"\nContent of 2st array: "<<endl;
for(int i=0; i<count; i++)
cout<<arr2[i]<<" ";
cout<<endl;
}

int main(){

// declaring two array of size 6 and initializing first array with some value
int arr1[] = {1,2,3,4,5};
int arr2[5];

// calling function
fun(arr1, arr2, 5);

return 0;
}

Output:

*Feel free to ask(comment) if you have any doubts or if you face any difficulties while executing the program, I will be happy to help you, please upvote if you like the solution


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT