Question
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 array: int box08,0,-4, 74,5,13,42), (29-7,45,4, 100,23.-3,613 Your program needs to call both functions and display appropriate messages
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<algorithm>
using namespace std;
#define N 4 //defind dymention of array
#define M 4
float sumAndAvg(int ar[][N]){
  
int s=0; //for sum of element
float c1=0; //maintain count of array
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
s+=ar[i][j]; //keep summing array element
c1++; //increase the count
}
}
cout<<"sum of numbers: "<<s<<endl; //one function can only return one value
//so print sum here return avg
float avg=s/c1; //avg= sum/no of elements
return avg; //return avg
}
//return true if element found else false
bool find(int ar[][N],int v){
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
if(ar[i][j]==v) //if element is v return true
return true;
}
}
return false; //else false
}
// Driver program to test above methods
int main()
{
int box[][N] = {{11, 8,0,-4}, //defind given 2D array
{74, 5,13,42},
{29,-7,45,4},
{100,23,-3,6}
};
cout <<"avg of numbers: "<<sumAndAvg(box)<<endl; //call sumAndAvg function
int v=3;
bool isAvailable=find(box,v); //call find function
if(isAvailable)
cout<<v<<" Found ";
else
cout<<v<<" Not Found";
return 0;
}

output

sum of numbers: 346
avg of numbers: 21.625
3 Not Found

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

    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!! 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...

  • Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

    Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write...

  • 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,...

  • 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...

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Use only Include<iostream> Question 01: write a complete C++ program that should print the result of...

    Use only Include<iostream> Question 01: write a complete C++ program that should print the result of each of functions the following a. Function name: FindMinElement The function should take 3 integer values and returns the minimum the number. b. Function name: FindMaxElement The function should takes integer values and returns the maximum the number. C. Function name: FindAverage The function should ask the user to input double value for 10 times and it should returns the average Of those values....

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • 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...

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