Question

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 or equal to the average and fill out the second array with these numbers. You need to design the function.

I tried this code but the errors returned were: expression must have a constant value

#include<iostream>
using namespace std;

void fillArray(int arr1[],int arr2[],int arr_size)
{
int cnt=0;
double avg,sum;
for(int i=0;i<arr_size;i++)
{
sum+=arr1[i];
}

avg=sum/arr_size;
//assigning the second array
for(int i=0;i<arr_size;i++)
{
if(arr1[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<cnt;i++)
{
cout<<arr2[i]<<" ";
}

}

int main()
{
int arr_size;
cout<<"Enter the size of array: ";
cin>>arr_size;
int arr1[arr_size],arr2[arr_size];
cout<<"Enter the element:\n";
for(int i=0;i<arr_size;i++)
{
cin>>arr1[i];
}
fillArray(arr1,arr2,arr_size);


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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;
}

Add a comment
Know the answer?
Add Answer to:
In C++ Write a function that accepts two int arrays of the same size. The first...
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
  • Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration...

    Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration       int   sum = 0;       float avg;       //input test scores       cout << " Enter " << 6 << " test scores: " << endl;       for (int i = 0; i < 6; i++)       {             cout << "Enter Test " << i + 1 << ": ";             cin >> tests[i];       }       return 0; }    Type...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. 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 pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the...

    In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the same size. The first array will have 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 and fill out the second array with these numbers. You need to design the function.Comments as well please because i am super confused. Thank you!

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • Given below is a partially completed C program, which you will use as a start to...

    Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint:...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1.   Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; } return count;...

  • Write a program that works with two arrays of the same size that are related to...

    Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations and a double array...

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