+1

HELP WITH EASY HOMEWORK CODE - [Dynamic Memory Allocation]

Hello, I am taking C++ beginner class and I can't get to do this. The instruction is: 1. cin >> size of array 2. cin >> number (in array) 3. cout << "Number of values grater than the average is " << ( HINT : int* list = new list[size] ) And the answer should be this. Enter array size: 3 Enter number: 1 Enter number: 5 Enter number: 7 The number of values greater than the average is 2 Please, some help :(( I don't know how to get the average or how to make the "enter a number" repeat when using dynamic memory allocation.

11/21/2019 9:39:27 AM

Dosei

4 Answers

New Answer

+1

Dosei prototype of aboveAvg() function should be like void aboveAvg(double Avg, int *list) instead of void aboveAvg(); aboveAvg() function does not return a value hence this statment is wrong cout << "The numbers greater than average are: " << aboveAvg() ; write it like cout << "The numbers greater than average are: " ; aboveAvg(Avg, list); And you are forgetting to release the allocated memory.

+2

Finding an average of the numbers is a school level maths no execuse here. How to repeat "enter a number" ? use loops. Which from the progress of your course i can say you have already covered. Kindly revise it helps. And dynamic memory? Have you covered the dynamic memory section? If not, please cover it first. If yes tell us where you are stuck. If you need help kindly show your effort.

+1

Ok, thank you very much for your help - swim - ! I will keep working on that!

0

I'm sorry, I know what you mean. Here I tried to do this code: #include <iostream> using namespace std; void aboveAvg(); int main() { int size, i; double sum = 0; double Avg; cout << "Enter array number: "; cin >> size; int* list = new int[size]; for (i = 0; i < size; ++i) { cout << i + 1 << "Enter a number: "; cin >> list[i]; sum += list[i]; } Avg = sum / size; cout << "The numbers greater than average are: " << aboveAvg() << endl; return 0; } void aboveAvg(double Avg, int *list){ if (*list > Avg) { cout << *list << " " << endl; } } ------ I only have one error which says " no operator << matches this operands" before aboveAvg << endl; I need some guidance with that. Also, if someone could check if my void code is correct I would really appreciate it, I still find it difficult to understand how to call array and its elements into a void function.