Issues with combo of structures and arrays - Practice for a final exam

K

Kebin

Hey how's it hanging? I'm a student at PSU and I don't molest children. Maybe you can help me finish my code - it implements pretty much everything I need to know for my final tomorrow so help is much appreciated!

http://www.mediafire.com/?x5and7k92cs0wa4

There a link to what I've done so far, and if you're sketched about downloading it go ahead and scan a million times and know that its just a cpp filewhich idk if you can even get viruses on those? Anyway check it out - the one problem I have currently is that the last function

void AverageTotal (Book index[],int s,double avg,double sum)
{
int i;

for (i=0; i<s; i++)
{
sum = sum + index.cost;
}
avg = sum / i+1;
}

That is designed to find avg and total of the books is not returning anything but 0. Maybe you can find out why?!

Anyway, thanks for your time and potential help.

Good day to youuzzz
 
M

MikeWhy

Kebin said:
Hey how's it hanging? I'm a student at PSU and I don't molest
children. Maybe you can help me finish my code - it implements pretty
much everything I need to know for my final tomorrow so help is much
appreciated!

http://www.mediafire.com/?x5and7k92cs0wa4

There a link to what I've done so far, and if you're sketched about
downloading it go ahead and scan a million times and know that its
just a cpp file which idk if you can even get viruses on those?
Anyway check it out - the one problem I have currently is that the
last function

void AverageTotal (Book index[],int s,double avg,double sum)
{
int i;

for (i=0; i<s; i++)
{
sum = sum + index.cost;
}
avg = sum / i+1;
}

That is designed to find avg and total of the books is not returning
anything but 0. Maybe you can find out why?!

Anyway, thanks for your time and potential help.

Good day to youuzzz


maybe try:

double AverageTotal (Book index[],int s)
{
double sum = 0.0;
...
return sum / s;
}

or

void AverageTotal (const Book index[],int s, double & avg, double & sum)
{
avg = sum = 0.0;
...
 
C

Christopher

void AverageTotal (Book index[],int s,double avg,double sum)
{
   int i;

   for (i=0; i<s; i++)
           {
                 sum = sum + index.cost;
           }
   avg = sum / i+1;

}

That is designed to find avg and total of the books is not returning anything but 0. Maybe you can find out why?!


It doesn't return anything at all. Your function returns void.
If you wish to change the value of average, pass it by reference. It
is currently passed by value.
If you wish to change the value of sum, pass it by reference. It is
currently passed by value. However, I don't see any reason to even
have it as a parameter at all vs a local unless your intention was to
get the average and the sum in one call.

I also dislike passing array in the form
void Foo(MyType[] arr)

and prefer
void Foo(MyType * arr)

Hopefully, your parameter s is the number of items in the array. I'd
name it as such. Like numBooks.
the name index is misleading. You are not passing an index, but rather
the entire array.

My untested function would look like:

double Average(Book * books, size_t numBooks)
{
double sum = 0.0;

// Always check for valid parameters to prevent pesky unhandled
exceptions!
if( !books || !numBooks )
{
// Error - invalid parameters;
return 0.0;
}

for (size_t index = 0; index < numBooks; ++i)
{
sum += books[index].cost; // Assuming cost is of type double
}

return = sum / static_cast<double>(numBooks);
}

If you also want to get the total cost, then make a seperate function
for it. Seperating responsibilities is good IMO, especially when you
start designing classes later/now.

If you want to be fancy, I'd make a "Books" class that is a container,
and add methods for Average, Count, and Total cost. The former could
be calculated on the fly and the latter would be incremented/
decremented with every insertion/removal.
 
T

Tobias Müller

Kebin said:
Hey how's it hanging? I'm a student at PSU and I don't molest children.
Maybe you can help me finish my code - it implements pretty much
everything I need to know for my final tomorrow so help is much appreciated!

http://www.mediafire.com/?x5and7k92cs0wa4

There a link to what I've done so far, and if you're sketched about
downloading it go ahead and scan a million times and know that its just a
cpp file which idk if you can even get viruses on those? Anyway check it
out - the one problem I have currently is that the last function

void AverageTotal (Book index[],int s,double avg,double sum)
{
int i;

for (i=0; i<s; i++)
{
sum = sum + index.cost;
}
avg = sum / i+1;
}

That is designed to find avg and total of the books is not returning
anything but 0. Maybe you can find out why?!

Anyway, thanks for your time and potential help.

Good day to youuzzz


Problem #1:
You need to pass "sum" and "avg" by pointer or reference, otherwise all
changes to them are only inside this function.

Problem #2:
"sum / i + 1" means "(sum / i) + 1". this is probably not what you want.

Problem #3:
Assuming that you meant "sum / (i + 1)", this is probably still wrong,
because i is also incremented after the last iteration of the loop.
Probably you mean "avg = sum / i" or even more obvious "avg = sum / s".

Tobi
 
8

88888 Dihedral

Hey how's it hanging? I'm a student at PSU and I don't molest children. Maybe you can help me finish my code - it implements pretty much everything I need to know for my final tomorrow so help is much appreciated!

http://www.mediafire.com/?x5and7k92cs0wa4

There a link to what I've done so far, and if you're sketched about downloading it go ahead and scan a million times and know that its just a cpp file which idk if you can even get viruses on those? Anyway check it out - the one problem I have currently is that the last function

void AverageTotal (Book index[],int s,double avg,double sum)
{
int i;

for (i=0; i<s; i++)
{
sum = sum + index.cost;
}
avg = sum / i+1;
}

That is designed to find avg and total of the books is not returning anything but 0. Maybe you can find out why?!

Anyway, thanks for your time and potential help.

Good day to youuzzz


I suggest you change to the following style of most of your functions:

int dosomething(T1 *input, T2 *output)

Please define the input and output structures or classes of objects
clearly first.

Of course call by reference is allowed in C++.
But that's another just syntax sugar for kids again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top