C++ question

Sharky Forums


Results 1 to 15 of 15

Thread: C++ question

  1. #1
    Goldfish
    Join Date
    May 2001
    Location
    Lost in my mind... Or is that Lost my mind?
    Posts
    69

    Post C++ question

    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

    ------------------
    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?
    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?

  2. #2
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    I will try to write it:
    Code:
    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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  3. #3
    Reef Shark
    Join Date
    Apr 2001
    Location
    Austin, TX
    Posts
    371

    Post

    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:

    Code:
    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.

    Code:
    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]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

    [This message has been edited by reklis (edited July 12, 2001).]
    Advocate of the Sharky (Ultra) High-Resolution Club [SHRC]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

  4. #4
    Reef Shark
    Join Date
    Nov 2000
    Location
    Puerto Rico
    Posts
    402

    Question


    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
    Athlon XP 2.0 GHZ
    MSI K7T TURBO2
    512 MB PC133 RAM
    WD HD 20 GB 7200 RPM
    RADEON 32MB DDR
    SB LIVE PLATINUM
    PIONEER DVD 16X
    SONY 48X-16X-48X RW
    ANTEC SX830
    SWIFTECH MC462

  5. #5
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Smile

    You can get free c++ editors from a lot of places.
    www.gnu.org has the famous GCC/GPP compiler that is included with unix systems.
    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 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)
    ## root is the greed of all evil ##

  6. #6
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    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

    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.
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

  7. #7
    Reef Shark biosx's Avatar
    Join Date
    Jun 2001
    Location
    Chicago, IL, USA
    Posts
    448

    Post

    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:

    Code:
    #include &lt;iostream.h&gt;
    #define MAX 100
    
    main()
    {
        int ns[MAX];
        int i, c, last;
        int sentinel = 'x';
        
        for (i = 0; ;i++)
        {
           cin &gt;&gt; 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)
    ## root is the greed of all evil ##

  8. #8
    Reef Shark
    Join Date
    Dec 2000
    Posts
    327

    Post

    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.

  9. #9
    Goldfish
    Join Date
    May 2001
    Location
    Lost in my mind... Or is that Lost my mind?
    Posts
    69

    Post

    Thank you so much guys I haven't had a chance to try it yet, but it looks pretty good 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?
    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?

  10. #10
    Sushi
    Join Date
    Jul 2001
    Location
    dallas, tx, usa
    Posts
    4

    Post

    Originally posted by namgor:
    I will try to write it:
    Code:
    int max = MAXINT, min =MININT, avg = 0, num = 0, temp;
    while ( !cin.eof() ) {
       cin &lt;&lt; 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
    Get married to the game, but don't have a kid with it

  11. #11
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    Well... it must be kindargarden 's assignment then...

    ------------------
    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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  12. #12
    Ultra Great White Shark!! richardginn's Avatar
    Join Date
    Feb 2001
    Posts
    16,118

    Post

    man this problem was easy.

    ------------------
    www.geocities.com/richardginn/templatehtml -Come visit the Template HTML homepage
    www.myeducational plan.com-come see my plan to fix the USA educational system. I hope this is sig legal. Major Site Design Update on July 18, 2006. On June 18, 2009 passed the 10,000 post mark. December 24, 2009: Major Theme change and more....

  13. #13
    Goldfish
    Join Date
    May 2001
    Location
    Lost in my mind... Or is that Lost my mind?
    Posts
    69

    Post

    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?
    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?

  14. #14
    Expensive Sushi
    Join Date
    Apr 2001
    Posts
    33

    Post

    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.

  15. #15
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    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

    ------------------
    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 or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •