Question

C++ question. Please help, thank you! Write a function called averageOdd that returns the average of...

C++ question. Please help, thank you!

Write a function called averageOdd that returns the average of all of the odd numbers in a 2-Dimensional array with 3 columns. If no odd numbers are present, it should return a result of 0. Excessively long solutions that use more than 15 lines of code may lose points. For example, a program that uses the function averageOdd follows:

int main() {

int data[2][3] = {{3, 1, 4} , {2, 7, 1}};

cout << averageOdd(data, 2, 3) << endl;  

// prints 3.0

// because the odd entries 3, 1, 7, 1 average to 3.0

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

double averageOdd(int arr[][3], int r, int c){
   double sum = 0;
   int count = 0;
   for(int i=0;i<r;i++){
      for(int j = 0;j<c;j++){
         if(arr[i][j]%2==1){
            sum += arr[i][j];
            count += 1;
         }
      }
   }
   if(count == 0){
      return sum;
   }
   else{
      return sum / count;
   }
}

int main() {

int data[2][3] = {{3, 1, 4} , {2, 7, 1}};

cout << averageOdd(data, 2, 3) << endl;  

// prints 3.0

// because the odd entries 3, 1, 7, 1 average to 3.0

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ question. Please help, thank you! Write a function called averageOdd that returns the average of...
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
  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • c++ questions Write a function that returns a pointer to the maximum value of an array...

    c++ questions Write a function that returns a pointer to the maximum value of an array of ints: int* ptrToMaximum(int* array, int* end); You don’t have to worry about what happens if end points beyond end(array). Maybe it is nice to allow end to point to before end(array). In the case that (array == end), you should return nullptr. Running the following main function should result in 8 7 being printed to the console. int main() { int a[] =...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • In C++ please Q4: There are 10 items in the shopping cart. 1. Assign a random...

    In C++ please Q4: There are 10 items in the shopping cart. 1. Assign a random price between 20-60 to each item. 2. Calculate the price maximum in the cart. If the price maximum is less than 55, re-run step 1. 3. If the class maximum is greater than 55, then print out price for every item to the screen. Answer: Q5: Write title lines for the functions that are called by the following main program. Do not supply the...

  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • In C++: You are to write two functions, printString() and testString(), which are called from the...

    In C++: You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

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