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

Discussion in 'C++' started by Kebin, Dec 13, 2011.

  1. Kebin

    Kebin Guest

    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
     
    Kebin, Dec 13, 2011
    #1
    1. Advertising

  2. Kebin

    MikeWhy Guest

    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.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;
    ...
     
    MikeWhy, Dec 14, 2011
    #2
    1. Advertising

  3. Kebin

    Christopher Guest

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

    On Dec 13, 5:14 pm, Kebin <> wrote:
    > 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.
     
    Christopher, Dec 14, 2011
    #3
  4. Re: Issues with combo of structures and arrays - Practice for a final exam

    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.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
     
    Tobias Müller, Dec 14, 2011
    #4
  5. Re: Issues with combo of structures and arrays - Practice for afinal exam

    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.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.
     
    88888 Dihedral, Dec 15, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,769
  2. Sujath
    Replies:
    3
    Views:
    475
    Muhammad Asif
    Nov 23, 2006
  3. Alfonso Morra
    Replies:
    11
    Views:
    741
    Emmanuel Delahaye
    Sep 24, 2005
  4. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    611
    Bob Barrows [MVP]
    Nov 16, 2005
  5. SM
    Replies:
    2
    Views:
    394
Loading...

Share This Page