beginner question

This is a discussion on beginner question within the C Programming forums, part of the General Programming Boards category; I'm having problems deciding how to program a certain function, maybe you can help: As part of a class assignment, ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    beginner question

    I'm having problems deciding how to program a certain function, maybe you can help:

    As part of a class assignment, I have to program a function that takes in integers from the user until a zero is entered.

    Then the function calculates and displays the sum of all positive integers entered, but only the ones that have their digits in increasing order from left to right(for example, 123 is okay, but not 321 or 801).

    I don't know how to create a function that decides if an integer's digits are increasing or not.

    Take into account that this is a first course in C and this is the first exercise we received, so there are many things I can't use, such as: arrays, pointers, anything more advanced than the last two which I haven't heard of (and thus not listing explicitly) and anything outside of stdio.h.

    Any help would be greatly appreciated.

    p.s.
    The function is called without (so I decided, it wasn't specified. It also wasn't specified it must be a seperate function) any arguments.
    It then requests arguments from the user, which it uses for it's calculations.
    Last edited by ShaiAdar; 03-23-2009 at 07:12 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    What have you got so far? Can you test whether the user has entered anything at all?

    jonathan soons

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by jonsoons View Post
    What have you got so far? Can you test whether the user has entered anything at all?

    jonathan soons
    The user is assumed to be cooperating with the program to a degree that he will type a value (doesn't have to be correct) if the program requests it.

    If the user doesn't enter a value, however, I can just make a loop that continues until he enters a valid value. So that's pretty trivial.
    Last edited by ShaiAdar; 03-23-2009 at 07:28 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Are you allowed to use the greater than (>) and less than (<) operators? They are also trivial to implement. So you have a trivial loop, a couple of useful operators (you probably just need one) and a function we will call "main" which we can define as:
    void main(void)
    or
    int main(void)
    if your compiler requires it. The part you are missing is just to add the values that are entered.
    That should not be a problem.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by jonsoons View Post
    Are you allowed to use the greater than (>) and less than (<) operators? They are also trivial to implement. So you have a trivial loop, a couple of useful operators (you probably just need one) and a function we will call "main" which we can define as:
    void main(void)
    or
    int main(void)
    if your compiler requires it. The part you are missing is just to add the values that are entered.
    That should not be a problem.
    Using the < and > operators I can check if a certain number is greater or smaller than another number, but how do I check if a certain DIGIT of a number is smaller than the next DIGIT of the same number (assuming I don't know the number to be entered before writing the code)?

    Also, from what I know you can't write "int main(void)", since that means the function returns an integer value and also no value, but that's besides the point.
    Last edited by ShaiAdar; 03-23-2009 at 10:33 PM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Can no one help with this?

    I'm just asking for a general direction, I'm not asking for someone to write the code for me.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,182
    Assuming you can already read in the number, use the modulus operator (%) to get a digit.

    For example
    Code:
    int a = 123;
    int first_digit = a % 10;
    int second_digit = (a / 10) % 10;
    int third_digit = (a / 100) % 10;
    Recall that integer divisions truncate (round down for positive numbers).

    Put it into a loop and an array, and you can separate the digits.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Thanks, cyberfish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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