Click to See Complete Forum and Search --> : C++ question


uNr34l_w0m4N
07-12-2001, 09:59 AM
Yes, I know the topic isn't descriptive. sorry.

What I need to be able to do is take a bunch of numbers that the user inputs, and then find the average of them, as well as the max and the min. What i was wondering is how could i read these numbers from the input, and make them all separate? Is this possible? The user should be able to enter in a string of numbers such as
48 576 21 3 57 8
and the program should recognise these all as different numbers...

And help would be greatly appreciated. Thank you http://www.sharkyforums.com/ubb/smile.gif

------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.

Guess I'll think twice about putting toes in my mouth,eh?

namgor
07-12-2001, 11:20 AM
I will try to write it:

int max = MAXINT, min =MININT, avg = 0, num = 0, temp;
while ( !cin.eof() ) {
cin << temp;
max = math.max (temp, max);
min = math.min (temp, min);
avg += temp;
num += 1;
}
avg = avg / num;


So it read until you type ^D and if you want to check duplicate number as you go, you need to store them in an array (or linked list)

Again, it is just an attempt fro me, the functon might not exist above.

------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3

uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)

reklis
07-12-2001, 04:32 PM
The question really is "Do you know how many variables you'll be reading". Because that's what you need answered. You should be able to do:

cin >> x >> y >> z;

But if you don't know how many you'll be reading, then you'll need to do some kind of loop like namgor states. If you just want to compute the average on the fly, keep a total, and the loop count, and just add the numbers and compute the average every iteration of the loop.

int i, total, curr;
i = total = curr = 0;

while(1) {
cout << "new number: ";
cin >> curr;

++i;
total += curr;

cout << "average: " << total/i << endl;
}

------------------
Advocate of the Sharky (Ultra) High-Resolution Club [SHRC (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)]
main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

[This message has been edited by reklis (edited July 12, 2001).]

pnut33
07-12-2001, 08:54 PM
Im looking for a free editor.

I have visual c++ available but this book my friend lended me kinda has DOS like programs so im not sure if this will work with visual c++.

So anybody know where i could get a free c++ editor n compiler?

------------------
THUNDERBIRD 800 @ 900 MHZ
ABIT KT7a RAID
256 MB PC133 cl2
IBM HD 45 GB 7200 RPM
RADEON 32MB DDR
SB LIVE PLATINUM
CREATIVE DVD 12X W/DXR3
CREATIVE 12X-10X-32X RW
ANTEC SX830

biosx
07-12-2001, 09:26 PM
You can get free c++ editors from a lot of places.
www.gnu.org (http://www.gnu.org) has the famous GCC/GPP compiler that is included with unix systems.
www.borland.com (http://www.borland.com) lets you download their C++ Builder Compiler if you answer a few questions and register with their site.

Also, there are many books that come with other compilers, but most of them are either Borland or GNU compilers.

Head over to www.programmersheaven.com (http://www.programmersheaven.com) and check under the C/C++ section for compilers b/c they usually list alot of choices and rate them.

Hope that helps


------------------
## root is the greed of all evil ##

/* Navi Specs */
Abit KT7-RAID
Duron 800 @ 1GHz
Geforce 2 GTS
512MB Micron/Crucial SDRAM
SB Live! 5.1 w/ Live! Drive
Maxtor ATA100 40GB
Pioneer 16x DVD
HP 9300 series CD-RW
Netgear FA311 NIC
Win2k Server / Linux Mandrake 8.0
Sony Multiscan 17SF (shut up, I know it's old)

Zoma
07-12-2001, 10:20 PM
What I need to be able to do is take a bunch of numbers that the user inputs, and then find the average of them, as well as the max and the min. What i was wondering is how could i read these numbers from the input, and make them all separate? Is this possible? The user should be able to enter in a string of numbers such as
48 576 21 3 57 8
and the program should recognise these all as different numbers...

One of the standard C input functions can do this, but I hate using that kind of thing http://www.sharkyforums.com/ubb/smile.gif

Personally, I'd just read in the whole string myself, and then parse it. Just go through the string, and convert each group of characters into a number. A "group" can be determined by searching for the first space. If you search for messages I've written on here, one or two of them have pretty complete string-to-number functions in them.

biosx
07-13-2001, 12:05 AM
Instead of searching through a line of numbers, why not just enter them in one at a time? It'll be easier to code and all you have to do is set up an array that will hold each number.

Kind of like this:


#include <iostream.h>
#define MAX 100

main()
{
int ns[MAX];
int i, c, last;
int sentinel = 'x';

for (i = 0; ;i++)
{
cin >> c;

if (c = sentinel)
break;
ns[i] = c;

}

last = i;

int sum;
for (i = 0, sum = 0; i = last; i++)
{
sum += ns[i];
}

double avg;
avg = sum/last;

etc. etc. you get the picture



I don't know why I wasted my time doing that. I know she got it already. Well, it was worth a shot. Its spahgetti code anyway







------------------
## root is the greed of all evil ##

/* Navi Specs */
Abit KT7-RAID
Duron 800 @ 1GHz
Geforce 2 GTS
512MB Micron/Crucial SDRAM
SB Live! 5.1 w/ Live! Drive
Maxtor ATA100 40GB
Pioneer 16x DVD
HP 9300 series CD-RW
Netgear FA311 NIC
Win2k Server / Linux Mandrake 8.0
Sony Multiscan 17SF (shut up, I know it's old)

jkresh
07-13-2001, 12:44 AM
Use visual C++, it is a good compiler, for Dos like programs just create a "command console program" all basic C++ work is done using command console, as MFC work is difficult.

uNr34l_w0m4N
07-13-2001, 09:02 AM
Thank you so much guys http://www.sharkyforums.com/ubb/smile.gif I haven't had a chance to try it yet, but it looks pretty good http://www.sharkyforums.com/ubb/smile.gif thank you!

------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.

Guess I'll think twice about putting toes in my mouth,eh?

jrwoods
07-13-2001, 12:09 PM
Originally posted by namgor:
I will try to write it:

int max = MAXINT, min =MININT, avg = 0, num = 0, temp;
while ( !cin.eof() ) {
cin << temp;
max = math.max (temp, max);
min = math.min (temp, min);
avg += temp;
num += 1;
}
avg = avg / num;


So it read until you type ^D and if you want to check duplicate number as you go, you need to store them in an array (or linked list)

Again, it is just an attempt fro me, the functon might not exist above.


I think you just did someones computer science homework for them.

------------------
Get married to the game, but don't have a kid with it

namgor
07-13-2001, 01:39 PM
Well... it must be kindargarden 's assignment then... http://www.sharkyforums.com/ubb/smile.gif

------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3

uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)

richardginn
07-13-2001, 07:12 PM
man this problem was easy.

------------------
www.geocities.com/richardginn/templatehtml (http://www.geocities.com/richardginn/templatehtml) -Come visit the Template HTML homepage

uNr34l_w0m4N
07-14-2001, 02:08 AM
It was just one of those things that didn't quite click over for me, y'know? Sometimes the simple stuff doesn't click. and that wasn't my whole assignment. ::shakes head:: only a fraction of it, and i will be rewriting the code anyway, now that i have seen how to do the stuff. ::nod::

btw. it may be easy to you, but i'm just starting out with C++ in a summer class... so it would be appreciated if you could keep the condescending comments to a minumum.

thanks.

------------------
I lost my toe the other day...
then I looked down at my foot and found he'd come back to me.

Guess I'll think twice about putting toes in my mouth,eh?

OmegalordX
07-16-2001, 12:24 AM
You could also read all the numbers into an array - and just bubble sort it (simple sort algorithm availiable all over the web)- not the most efficent way to do it - but for that assignment it will be ok- and take the lowest position of the array (as the max) and the highest position of the array (as the min). Whew long sentence. That's the way to do it if you don't want to use the hokey library functions.

As far as averaging that should be pretty easy to do.

namgor
07-16-2001, 10:59 AM
Originally posted by OmegalordX:
You could also read all the numbers into an array - and just bubble sort it (simple sort algorithm availiable all over the web)- not the most efficent way to do it -

that is one of the worst way to do it http://www.sharkyforums.com/ubb/tongue.gif

------------------
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3

uwcdc.com (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)