Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12475 > unrolled thread
| Started by | Kebin <k.dulce.21@gmail.com> |
|---|---|
| First post | 2011-12-13 15:14 -0800 |
| Last post | 2011-12-15 08:51 -0800 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.c++
Issues with combo of structures and arrays - Practice for a final exam Kebin <k.dulce.21@gmail.com> - 2011-12-13 15:14 -0800
Re: Issues with combo of structures and arrays - Practice for a final exam "MikeWhy" <boat042-nospam@yahoo.com> - 2011-12-13 18:18 -0600
Re: Issues with combo of structures and arrays - Practice for a final exam Christopher <cpisz@austin.rr.com> - 2011-12-14 00:00 -0800
Re: Issues with combo of structures and arrays - Practice for a final exam Tobias Müller <troplin@bluewin.ch> - 2011-12-14 09:53 +0000
Re: Issues with combo of structures and arrays - Practice for a final exam David Harmon <source@netcom.com> - 2011-12-14 07:47 -0800
Re: Issues with combo of structures and arrays - Practice for a final exam 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-15 08:51 -0800
| From | Kebin <k.dulce.21@gmail.com> |
|---|---|
| Date | 2011-12-13 15:14 -0800 |
| Subject | Issues with combo of structures and arrays - Practice for a final exam |
| Message-ID | <18071799.15.1323818090821.JavaMail.geo-discussion-forums@yqnd19> |
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
[toc] | [next] | [standalone]
| From | "MikeWhy" <boat042-nospam@yahoo.com> |
|---|---|
| Date | 2011-12-13 18:18 -0600 |
| Message-ID | <jc8pvt$cm$1@dont-email.me> |
| In reply to | #12475 |
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;
...
[toc] | [prev] | [next] | [standalone]
| From | Christopher <cpisz@austin.rr.com> |
|---|---|
| Date | 2011-12-14 00:00 -0800 |
| Subject | Re: Issues with combo of structures and arrays - Practice for a final exam |
| Message-ID | <68a33a51-c65a-4ee1-b097-1d8a93484576@p16g2000yqd.googlegroups.com> |
| In reply to | #12475 |
On Dec 13, 5:14 pm, Kebin <k.dulce...@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Tobias Müller <troplin@bluewin.ch> |
|---|---|
| Date | 2011-12-14 09:53 +0000 |
| Subject | Re: Issues with combo of structures and arrays - Practice for a final exam |
| Message-ID | <1931304566345548382.093480troplin-bluewin.ch@news.aioe.org> |
| In reply to | #12475 |
Kebin <k.dulce.21@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | David Harmon <source@netcom.com> |
|---|---|
| Date | 2011-12-14 07:47 -0800 |
| Message-ID | <mvidnccEmLaCWHXTnZ2dnUVZ_o-dnZ2d@earthlink.com> |
| In reply to | #12475 |
On Tue, 13 Dec 2011 15:14:50 -0800 (PST) in comp.lang.c++, Kebin
<k.dulce.21@gmail.com> 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?!
Maybe because "void" functions cannot return anything?
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2011-12-15 08:51 -0800 |
| Message-ID | <29476888.375.1323967893222.JavaMail.geo-discussion-forums@prfb7> |
| In reply to | #12475 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web