Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Issues with combo of structures and arrays - Practice for a final exam

Reply
Thread Tools

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

 
 
Kebin
Guest
Posts: n/a
 
      12-13-2011
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[i].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
 
Reply With Quote
 
 
 
 
MikeWhy
Guest
Posts: n/a
 
      12-14-2011
Kebin wrote:
> 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[i].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;
...



 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      12-14-2011
On Dec 13, 5:14*pm, Kebin <(E-Mail Removed)> wrote:
> void AverageTotal (Book index[],int s,double avg,double sum)
> {
> * *int i;
>
> * *for (i=0; i<s; i++)
> * * * * * *{
> * * * * * * * * *sum = sum + index[i].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.

 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      12-14-2011
Kebin <(E-Mail Removed)> wrote:
> 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[i].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
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      12-15-2011
On Wednesday, December 14, 2011 7:14:50 AM UTC+8, Kebin wrote:
> 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[i].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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Remember when your piano teacher taught you, "Practice, practice,practice ...?" Wayne Wastier Windows 64bit 3 06-10-2005 08:29 PM



Advertisments