need some C++ help

compguy786

Platinum Member
May 26, 2005
2,141
3
81
Hello Guys,
I have been working on this one assignment for the past 5 hours...and still havent gotten it to work properly.
I have attached the code.

here are the instructions on what i am supposed to do "

CodeExercise - PExercise10_3
Write two overloaded functions that return the average of an array with the following headers:
int average(int * array, int size);
double average(double * array, int size);

Use {1, 2, 3, 4, 5, 6} and {6.0, 4.4, 1.9, 2.9, 3.4, 3.5} to test the functions.

i have no clue why and what is happening to this, at one point i got it to work
with a instant crash !

Locking this one for now. OP needs to get the basic structure right or generate some specific errors that we can discuss. --Markbnj, AT Programming Moderator
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Not going to do your hw for you but here are a couple pointers.

Double check you array size versus the size you pass into the function (this is the crashing problem)

These statements do nothing:
num/6;
num1/6;

I also think you need to think about how would calculate the average on paper and then code that. Average is sum/#elements but there is no addition in your code at all.
 

Neverm1nd

Member
Jul 3, 2006
42
0
0
That actually compiles? Your functions don't match your prototypes, and C++ really shouldn't allow you to assign an int to an int*.

It's not crashing because the size is incorrect (although it is), because you never read from the array.

I'd start by writing just one of the cases (say the int one), once you've got that working, doing the other should be much easier. My guess is that your code doesn't compile. Try reading the error messages, they're usually pretty informative. Think about which variables needs to be pointers and which doesn't. And, as nickbits said, do it on paper and try to write down the algorithm.
 

compguy786

Platinum Member
May 26, 2005
2,141
3
81
Ok.
I went through the program again, and tried to make it work...and have no clue still what to do to make it work !

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Do it out on paper first.

Your rework still does not address the critical areas that nickbits mentioned
I also think you need to think about how would calculate the average on paper and then code that. Average is sum/#elements but there is no addition in your code at all.

 
Sep 29, 2004
18,656
67
91
Can you post your code without the code tags so I can see each line.
Thanks.

for example:
foo()
{
a++;
}
 
Oct 27, 2007
17,009
1
0
//this program tests functions out
#include <iostream>

using namespace std;

int average(int*,int );

int main() {
int array[] = {1, 2, 3, 4, 5, 6};
int size = 6;
int *avg; //pointer

avg = average(array,size) cout << avg;
}
//end main

int average(int *array, int size) { {
int num = *array;
for(int i = 0; i <num; i++)
num/6
};
return array; }//end

There it is line-for-line. I'm not sure what's going on with the curly brackets in the average() function but clearly there's something wrong there. OP, you need to carefully consider the algorithm for finding the average. Put it in words:
-declare a temporary variable, temp = 0
-add all numbers in the array to temp
-divide temp by the number of items in the array
-return the result

Now consider what your code is doing:
-assign an integer variable, num, to the first item in the array
-loop from 0 to num-1
--divide num by the size of the array
-return the array

You're having trouble with the basic algorithm here, aside from your syntactical troubles. Try writing a function that follows the steps in my algorithm above.
 
Sep 29, 2004
18,656
67
91
avg = average(array,size) cout << avg;

-------------- 1)
should be:
avg = average(array,size);
cout << avg;

What you had shouldn't even have compiled.

-------------- 2)
int *avg; //pointer

This should not be a pointer.

-------------
Having looked at the rest of the code, I have to tell the OP that you really need to spend some time with your books and make the effort (days probably) to understand the basics of C/C++. We can give you the answers but you really need to start understanding what is going on. You average(....) method is seriously flawed. It really demonstrates that you gave almost no effort to figure out things on your own.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Having looked at the rest of the code, I have to tell the OP that you really need to spend some time with your books and make the effort (days probably) to understand the basics of C/C++. We can give you the answers but you really need to start understanding what is going on. You average(....) method is seriously flawed. It really demonstrates that you gave almost no effort to figure out things on your own.

Yeah, certainly that code never compiled. OP, make an effort to get the structure right or at least generate some specific errors that you can ask questions about, then repost. I'm locking this for now.