Help with Homework... Time is almost up.

This is a discussion on Help with Homework... Time is almost up. within the C++ Programming forums, part of the General Programming Boards category; Originally posted by M_Ghani sorry Kasun but i think you should make the following Code: int input[3]; because input[2] means ...

  1. #16
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Re: Re: Just an another common way to do (a)

    Originally posted by M_Ghani
    sorry Kasun but i think you should make the following
    Code:
    int input[3];
    because input[2] means only two elements not three in the array but u needed here three elements of the array input

    "The first element in the array is the 0th element, and the last element is the (n-1th) element, where n is the size of the array."
    - MSDN, Arrays.

    So, input[2] means actually 3 elements - 0, 1 and 2.

    Thanks.

  2. #17
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    actually, he was right... the 'n' they were referring to was here:
    Code:
    int input[n]; //there's the n
    
    input[0]; //there's the 0 (first element)
    ...
    input[n-1]; //there's the n-1 (last element)
    So, input[2] means actually 2 elements - 0 and 1 (n-1).

    Your Welcome.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #18
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Originally posted by major_small
    you should never tell anybody to do what their assignment explicity tells them not to do...
    I understand. But when I post that code, he was already figured out a code appropriate for his class. So I was only thinking of pointing out another way to do it, not telling to use that method, I have no power to do that.

    Anyway, thanks for the advice.

  4. #19
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by major_small
    I dont' do flowcharts or trace tables or anything like that before programming... only for really big projects... and don't mix programming and building a house... that is one of the worst analogies i've ever heard...
    It's a perfect analogy. You need to have a plan when you build a house or a program. It's just that many programs are more on the order of building a doghouse instead of a mansion -- the blueprints are simple enough that experience makes them unnessesary.

    But inexperiance make them very useful.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #20
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I don't know about that... I never used them for small stuff, even when I was inexperienced...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #21
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>because input[2] means only two elements not three in the array but u needed here three elements of the array input

    by my counting input[2] has three elements: [0].[1].[2] !!

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #22
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by axon
    >>because input[2] means only two elements not three in the array but u needed here three elements of the array input

    by my counting input[2] has three elements: [0].[1].[2] !!
    Not sure if you were joking axon, but the discussion of the array seems a little too confusing. It is simple:

    When declaring an array, the number inside the brackets is the size of the array, so

    int input[2];

    declares an array with 2 elements. The elements can be accessed with the operator[] like this:

    input[0] = i1;
    input[1] = i2;

    Attempting to access an element of the array outside of its bounds is undefined.

    input[2] = i3; // BAD!!

    Most of the time, it will overwrite some other memory in your program. Often, it will cause a crash. Sometimes, if you are unlucky, you won't notice.

  8. #23
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Thanks for the help... here is the current result

    Ok, here is the current work (sorry about the formatting... don't know html tags to display it properly on this forum). I have few issues still, but overall I am pleased ( this is my first Computer Science class). My main problems still are the following:

    1. Getting the program to immediately give an error if the input
    given is not a-f instead of it asking for the integers first.

    2. For case 'c', I need to find the average of the 3 integers
    converted to the floating point average. The compiler
    warns me that it is coverting it with a possible loss of
    data. Am I doing this right or is there another way?

    3. The inability to handle a zero input for case 'a' is still
    present as I focused all my energy I finishing the rest
    of the program first. I will be working on that in the
    meantime.

    4. Anything else that you feel like mentioning.

    Code:
    
    #include <iostream.h>
    #include <math.h>
    
    int main()
    {
       int val1;
       int val2;
       int val3;
       int first;
       int second;
       int third;
       char option;
       float sum;
       int same;
       int even = 0;
       int odd = 0;
       float num1 = 0;
       float num2 = 0;
       float num3 = 0;
    
    // Title, Input
    
       cout << "Integer Calculation Program\n" << endl;
       cout << "Enter a single character a-f\n" << endl;
       cin >> option;
       cout << "Enter three integer values." << endl;
       cin >> val1 >> val2 >> val3;
      
      
       switch (option)
    
    /* 
       Case 'a' compares the 3 integers, assigns the values in order from smallest
       to largest to the variables first, second, and third, and then displays
       the output to the user.
    */
    
       {
       case 'a'  : 
         
         if (val1 <= val2 && val1 <= val3 && val2 <= val3)
    	 
                     {   
                       first = val1;
                       second = val2;
                       third = val3;
    	 }
       
         if (val2 <= val1 && val2 <= val3 && val1 <= val3)
    
    	 {
                       first = val2;    
                       second = val1;
                       third = val3;
    	 }
       
       
         if (val3 <= val1 && val3 <= val2 && val2 <= val1)
    
    	 {
                       first = val3;
                       second = val2;
                       third = val1;
    	 }   
       
         if ( val3 <= val1 && val3 <= val2 && val1 <= val2)
    
    	 {
                       first = val3;
                       second = val1;
                       third = val2;
    	 }
       
        cout << first << " " << second << " " << third << " " << endl;
                                   
    	                                                       break;
    /* 
       Case 'b' calculates the absolute values of the 3 integers and then
       displays the output to the user.
    */
    														   
       case 'b'  :
    	
           val1 = abs (val1);
           val2 = abs (val2);
           val3 = abs (val3);
    
           cout << val1 << ", " << val2 << ", and " << val3 
                   << " are the absolute values of your input." << endl;
    
    	                                                       break;
    /*
       Case 'c' adds the 3 integers and then finds the average floating point
       value of their sum by dividing by 3.
    */
    
       case 'c'  :
         
    
         sum = (val1 + val2 + val3) / 3.0; 
         cout << sum << " is the average of the three integers given."
                 << endl;
    
    	                                                       break;
    
    /*
       Case 'd' compares the 3 integers and determines and displays the number of 
       the values that are the same.
    */
    
    
       case 'd'  :
    
         if (val1 == val2 && val2 == val3)
    		 same = 3;
        
         if (val1 == val2 && val3 != val1)
    		 same = 2;
        
         if (val2 == val3 && val1 != val2)
    		 same = 2;
        
         if (val3 == val1 && val2 != val3)
    		 same = 2;
        
         if (val1 != val2 && val2 != val3 && val1 != val3)
    		 same = 0;
    
         cout << same << " of the integer values are the same." 
                 << endl;
    
    	                                                       break;
    
    /* 
       Case 'e' determines how many of the 3 integer values are even and how many
       are odd and then displays the information to the user.
    */
    
       case 'e'  :
    
           if ((val1 %2) == 0)
    	      even++;
              else
                            odd++;
    	  
           if ((val2 %2) == 0)
    	      even++;
              else
                            odd++;
    
           if ((val3 %2) == 0)
                          even++;
              else 
    	        odd++;
    	
           cout << even << " of the values are even." << endl;
           cout << odd << " of the values are odd." << endl;
    
    	                                                       break;
    	     
    /*
       Case 'f' determines if the 3 integers are positive, and then adds the 
       positive integers together and divides them by 3.0 to determine the 
       float point average of the positive integers.
    */
    
       case 'f'  :
    
    	  if (val1 > 0)
    		   num1 = val1;
    	  if (val2 > 0)
    		   num2 = val2;
    	  if (val3 > 0)
    		   num3 = val3;
    	  sum = (num1 + num2 + num3) /3.0;
    	  
    	  cout << sum << " is the floating point average "
                              << "of the positive integers."  << endl;
    
    	                                                       break;
    
    /*
       If any character besides a-f was given, then after entering the 3 integer
       values, the program gives an error message informing the user of the
       mistake.
    */
    
       default : cout << "Not a valid option. You did not choose "
                             << "a character a-f." << endl;
       }
    
    
    
    
    return 0;
    
    }
    Last edited by Uncertain; 03-01-2004 at 08:26 PM.

  9. #24
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>Not sure if you were joking axon,

    lets just say I was joking

    hey uncertain please use [code][/code] tags whenever posting any code! please edit your most recent post with the [code][/code] tags surrounding you code. Also, reread the rules, and other sticky notes on top of this forum.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #25
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Talking That was simple...

    Sometimes the most obvious just slips right past my eyes. Thanks for the tip Axon.

  11. #26
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>Sometimes the most obvious just slips right past my eyes. Thanks for the tip Axon.

    np. looks much better, doesn't it?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  12. #27
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Yep!

    MUCH better!

  13. #28
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    for part 1 and 2, just have an if statement checking if the input is out of bounds...if it is display message and loop back. For 2) also an if statement if c, read in floats...pseudo code below:

    Code:
    float a, b, c;
    bool notDone = true;
    
    while( notDone )
    {
    cout << "Integer Calculation Program\n" << endl;
       cout << "Enter a single character a-f\n" << endl;
       cin >> option;
    
       if( option not a-f )
            cout << "error" << endl;
       else
             notDone = false;
    }//while
       if( option is c)
        { cout << "Enter three floats." << endl;
       cin >> a >> b >> c; //then use these in case c
          }
         else
          {
           cout << "Enter three integer values." << endl;
           cin >> val1 >> val2 >> val3;
           }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  14. #29
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    the way you have your code before, the compiler is giving you warning because you are dividing an int by a float...

    if someone inputs 3.3333 and you read this in into an int...it will only keep the 3. Some compilers will give you an error.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  15. #30
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    integers to floats

    Well, the only problem is the assignment requires the input be integers so I have to convert the average of integers to floating point which the program is doing automatically from what I understand.

    I can't use loops in this assignment. But I was experimenting with
    using an if statement. I am not sure if this can work though as I am uncertain as to what statement to use for the if.

    Code:
    char option;
    
    cout << "Input a character a-f";
    
    cin << option;
    
    if (option ==  'a' || 'b' || 'c' || 'd' || 'e' || 'f')
       
       else 
               cout << "Invalid input.";

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. homework time
    By dP munky in forum C Programming
    Replies: 3
    Last Post: 11-23-2002, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21