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...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
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.  
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
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 forth. The...
C++ Write a function that accepts an array of doubles and the array's size as arguments....
C++ Write a function that accepts an array of doubles and the array's size as arguments. The function should display the contents of the array to the screen.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT