three questions about hw assignment

This is a discussion on three questions about hw assignment within the C++ Programming forums, part of the General Programming Boards category; I have to do three small programs that use loops. I know how to do the loops but am unsure ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation three questions about hw assignment

    I have to do three small programs that use loops. I know how to do the loops but am unsure about how to do the rest for exercises 1 and 2. I think that I can do exercise 3 fully on my own.

    For the first part, I have to calculate the sum of the integer entered.

    If the user typed in 698, how would I commute the sum? Would I declare sum= integer? (interger being the number entered).

    Sample:
    Please input an integer between 0 and 1000: 1001
    * Number not in 0-1000 range. Please re-enter: -1
    * Number not in 0-1000 range. Please re-enter: 456

    Sum of the digits is 15

    For the second one, the user has to enter 3 intergers and they have to be sorted from smallest to largest. I don't know how to get them sorted from smallest to largest.

    Sample run:

    Input integer 1 : 34
    Input integer 2 : 200
    Input integer 3 : -14

    Sorted : -14 <= 34 <= 200

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Try some code yourself and show us, and then we can help you even more. But you could start by reading the string of intergers into an array. Then you could use c-string operations to get your result for both parts...try it yourself!

    axon

    some entropy with that sink? entropysink.com

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

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    For the second one, the user has to enter 3 intergers and they have to be sorted from smallest to largest. I don't know how to get them sorted from smallest to largest.
    1. Check two numbers first and obtain the smaller one. Let's call the result s.
    2. Now compare s and the last remaining number to see which is smaller.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: three questions about hw assignment

    Originally posted by jlmac2001
    For the first part, I have to calculate the sum of the integer entered.

    If the user typed in 698, how would I commute the sum? Would I declare sum= integer? (interger being the number entered).

    Sample:
    Please input an integer between 0 and 1000: 1001
    * Number not in 0-1000 range. Please re-enter: -1
    * Number not in 0-1000 range. Please re-enter: 456

    Sum of the digits is 15
    For that one, suggest writing a function that will return a digit at a specified spot in an integer.
    Code:
    int stripDigit(int num, int place);
    // if place == 1, return ones place digit
    // if place == 2, return tens place digit, etc.
    
    stripDigit(456, 2) == 5;
    That's how I'd do it, anyway.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For Exercise 1, here's one way:

    456

    Read this in as an int. Now you want to strip out each digit. If you divide by 10, and take the remainder, this will give you the first digit. Use the mod operator (%) to get the remainder:
    456 % 10

    Then divide by 10 to get 45, and repeat:
    456 / 10

    Each time you take the mod, add this to your sum (declared as an int).

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Read the input as string and read it character by character and take the sum at the same time

    while COUNT < size of string
    sum = sum + (int) string[COUNT]
    COUNT++
    Last edited by alphaoide; 09-09-2003 at 03:03 PM.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You could:
    Code:
    int input;
    std::cin >> input;
    if(input > 1000 || input < 0)
        doSomethingAboutIt();
    
    char str[5]; //4 digits possible + 1 character for null 0
    
    //break it into a string of characters representing the digits
    itoa(input, str, 10); //10 = the base that you're using
    int sum = 0;
    for(int i = 0; str[i] != '\0'; ++i)
    {
        sum += (str[i] - '0'); // - '0' to get the actual numerical value
    }
    //now sum is the sum.
    I haven't tested it, but it just crossed my mind and I thought it might be an easy way to do it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    need help

    I started the two programs but am still unsure how to do the sum for the first one and how to sort the second one. I don't think we can use functions or strings because we haven't gotten that far in this class.

    Here are the two programs that I wrote unfinished:

    This one should read in a digit greater than0 but less than 1000. Then if the digit is valid, the sum of it should be calculated. I don't know how to do that part. Example: Enter a digit: 54
    Sum is 9.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int main()
    
    {
    
    int integer;
    
    
    cout << "Please input an integer between 0 and 1000: ";
    cin >> integer;
    
    cout << " Number not in 0-1000 range. Please re-enter:  ";
    
    
    while( integer >= 0 || integer <= 1000)
    
    {
    
     cout << "Please input an integer between 0 and 1000: ";
     cin >> integer;
     
     cout << " Number not in 0-1000 range. Please re-enter: <<  ";
    
    
    
    }
    
    return 0;
    
    }

    For this one, you have to put in three integers. Then, they have to be sorted from smallest to largest. example -9 < 98 <128. I don't know how to sort the numbers from largest to smallest. Can someone help me?
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    
    int main ()
    
    {
    
    int num1, num2, num3;
    
    cout << "Enter integer 1 : ";
    cin >> num1;
    
    cout << "Enter integer 2 : ";
    cin >> num2;
    
    cout << "Enter integer 3 : " ;
    cin >> num3;
    
    
    
    return 0;
    
    }

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, some editing here:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    int integer;
    
    cout << "Please input an integer between 0 and 1000: ";
    cin >> integer;
    
    //cout << " Number not in 0-1000 range. Please re-enter:  ";
    
    while( integer < 0 || integer > 1000)
    {
     cout << " Number not in 0-1000 range. Please re-enter: <<  ";
     cout << "Please input an integer between 0 and 1000: ";
     cin >> integer;
    }
    
    return 0;
    }
    The changes I made are in red. This should get you to input a number in the range of 0 to 1000 inclusive. To get the sum, read the rest of the thread above.

    Program 2:
    If your class is at the stage that I think it is, then use a ..........load of if statements.
    Last edited by Hunter2; 09-11-2003 at 07:02 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Need to host a web server for hw assignment
    By indigo0086 in forum Tech Board
    Replies: 7
    Last Post: 04-15-2007, 11:57 AM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM

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