Question

Question 1 Write the following two functions. The first function accepts as input a two-dimensional array of integers. It ret

Please answer in C++, and Please consider ALL parts of the question, especially where it asks the user for V. So there should be a "cin >> V" somewhere.

The last guy did not answer it correctly so please make sure you do! Thank You!!

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

The code for your question is as follows:

#include <iostream>
using namespace std;
const int ROWS=4; //Rows and Columns declaration at top
const int COLS=4; //as we are using arrays


struct result       //Created structure result to store
{                   //to store both sum and average of
   int sum;       //array and return both of them
   double average;   //from the function below

};


result sum_avg(int arr[ROWS][COLS]) //Calculates sum and average
{  
   int sum=0; //Initializing sum to zero
   for(int i=0;i<ROWS;i++) //Iterating the 2D array along the row
   {
       for(int j=0;j<COLS;j++) //Iterating the 2D array along the columns
       {
           sum+=arr[i][j]; //Adding each element to sum
       }
   }
   //Calculating average by dividing sum by total number of elements
   double average=(double)sum/(ROWS*COLS);
   //Created result obj to store sum and average
   result obj;
   obj.sum=sum; //Setting value of sum to sum we calculated
   obj.average=average; //Setting average to average we calculated
   return obj; //Returning the struct object
}
bool search(int arr[ROWS][COLS],int v) //Search function
{

  
   for(int i=0;i<ROWS;i++) //Iterating the 2D array
   {
       for(int j=0;j<COLS;j++)
       {
           if(arr[i][j]==v) //If we find the element, return true
               return true;

       }
   }
   return false; //If we reach the end of the function, return false

}
int main() //Main function
{
  
   int v; //Initliazing v and box array
   int box[ROWS][COLS]={{11,8,0,-4},{75,5,13,42},{29,-7,45,4},{100,23,-3,61}};
   //Declaring result a which stores the result from sum_avg function
   result a=sum_avg(box); //Called the sum_avg function and passed box to it
   //And sum and average is stored in a
   cout<<"The sum is:"<<a.sum<<"\n"; //Printing the sum stored in a
   cout<<"The average is:"<<a.average<<"\n";//Printing the average stored in a
   cout<<"Enter a number to be searched:";//Asking user to input v
   cin>>v;
   bool found=search(box,v);//Calling search function for box array and v
   if(found) //If search returns true
       cout<<"Number found in array";
   else    //If search returns false
       cout<<"Number not found";

}
Output:
The sum is:402 he av Enter a number to be searched:100 erage is:25.125 Number found in array

Add a comment
Know the answer?
Add Answer to:
Please answer in C++, and Please consider ALL parts of the question, especially where it asks the...
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++ show code thanks, only use library <iostream> Question 1 Write the following two functions The...

    c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second function accepts as input a two-dimensional array of integers and integer value V. It returns true if the value V is found in the array, false otherwise. Write a main program that declares and initializes the following...

  • please do all in C++ code, thank you. Worth 1 point Checkpoint 7.12 Write a statement...

    please do all in C++ code, thank you. Worth 1 point Checkpoint 7.12 Write a statement that assigns the value 10 to the first elementof an array of integers named minutes. Type your program submission here Worth 1 point Checkpoint 7.14 Write a cout statement that will display contents of the second element of an array named courseNumbers Type your program submission here Submit Worth 1 point Checkpoint 7.15 Write a cin statement that will store the user's input in...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • C++.Write the body of a function that returns the sum of all elements in a two-dimensional...

    C++.Write the body of a function that returns the sum of all elements in a two-dimensional array passed to it as an argument. Declare and initialize all needed variables. int arraySum(const int numbers[][COLS], int rows) { }

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • C++ there is an issue with my if/else statements, I have tried several different ways to...

    C++ there is an issue with my if/else statements, I have tried several different ways to make it match the instructions but each time i get different errors. Need help geting this to match the instructions peoperly. *******************instructions***************************** Function: nextGeneration This function has no parameters. This function returns an int. The purpose of this function is to modify the grid to represent the next generation. Here's how you are supposed to do that for this assignment: Set each element of...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI...

    Need Part 1 and Part 2 please! also has to build and run, thank you! CSCI 40 Computer Programming Methodology I Assignment 16 Part l Write a program that declares a two dimensional array and initialize it with following integers 2 4 3 81 45 2 10 3 6 20 21 12 28-5 7 11 2 4 75041 8 5 -10 3 2 0 Then 1. Write statements that display all the numbers in the array 2. Write statements that...

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