Thread: [Help] Formatting a Number with Commas

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    [Help] Formatting a Number with Commas

    Hi, im new to the forums, and im new to C Programming. I have a program I have been working on that has many parts. My professor doesnt really go over anything he assigns. So the problem is to have a user enter a long number (ex. 1024), and then output it in two ways, 1. formatted with commas 1,024, and 2. were suppose to take the number in as an amount of bytes and convert and display it back as something like 1KB. Im not worried on the 2nd part, I will just be using if and for loops. I am just stuck on how to write a function to format a number with commas. ***We have only gone over arrays, functions, types, loops, and expressions*** I was trying to think of a way to do it with arrays or strings(next chapter), but i cant not seem to think of how to do it. All it has to be is a function I can use to format the number with commas. Thanks for any help!

    I know you use a printf and scanf to get the number, just no clue on how to format that number with commas using a function.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Have you learned recursion yet, as part of your functions lesson? If so, there is a slick way to do it with recursion. If not, you can do it with loops.

    This is a great problem solving exercise, so I'll try to give you a hint without giving away too much (besides, forum rules, we can't hand out code). Whether you use loops or arrays, you will need to "break off" groups of three digits, starting from the right-most digit. Think about how you can use modulo/remainder (%), and integer division (/), to break off pieces of that number.

    Take a stab at this yourself, and if you get stuck, post the code you have, and tell us in detail what is not working about it, and we'll be able to help you more.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok, and i can use mod 0 and mod 3 to know when to place commas, but i dont understand how im suppose to get this as a function

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    also Recursion is in our functions chapter, but literally all my professor has actually gone over with us in class is printf statements, compiling, executing c programs, and other things that arent lessons helping us understand how to do the programs.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    i have this for the start of the function, int formatInt(int x) {, how would i use a for loop to go through each individual number of a number a user enters. if i can figure that out i believe i can use an if statement using mods, not too sure how exactly though.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by clmoore3 View Post
    how would i use a for loop to go through each individual number of a number a user enters.
    For this kind of thing it helps me to think of a simple example. Say the number is called num and is 123456. How do you get the individual digits? Getting the 6 is easy: just do num % 10. How about the 5? Do num % 100 / 10. How about the 4? See the pattern yet? This allows you to print the numbers backwards.

    6
    5
    4
    3
    2
    1

    With a little work, you can place commas in the correct places. Next, instead of printing them, place them into a string buffer and then reverse that buffer. (If needed, construct an independent function that reverses a string buffer.).

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, don't use mod 0 or mod 3. Think about your approach and try it out before you start coding. No point in writing code if you aren't pretty sure it will work. Did you try it out at all, in a small program or on paper? Think about it. x % 0 just doesn't make sense, it's like saying "the remainder when x is divided by 0 is". What happens when you divide by 0? How do you get a remainder from that? Also, x % 3 says "the remainder when dividing x by 3 is". That would only ever give you values of 0, 1 or 2.

    Your first post said you went over "arrays, functions, types, loops, and expressions", yet now you're claiming your professor has only covered printf and compiling/running programs. So which is it? How did you do the other homework assignments if all you know is printf? I'm assuming the first post is correct, that you do know arrays, functions, types, loops and expressions. Those are tools of the C language (a tool itself). You use those tools to solve a problem. Programming is ultimately about solving problems, not simply about typing up a solution and compiling/running it.

    Thus, the point of the exercise is for you to figure out how to solve the "commas" problem with the tools of the C programming language you have already learned. This is the last help I'll give before I see some code from you.

    Work it out on paper, by hand:

    98,765,432 / _____ = 98
    print a comma
    do something with % to get rid of the 98, now you're left with
    765,432 / _____ = 765
    repeat until you have done the whole number.

    Try some more examples, with different amount of digits. Pay careful attention to every little step in your process, it will be the basis for your algorithm/function.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    I apologize, the arrays,functions,types,loops, and expressions were chapters we were assigned to read for this week. this is our 2nd assignment, our first assignment was displaying limits of integer types. Which all we really did was using <limits.h> and integer types and displayed the ranged with printf statements. My professor himself, has not really taught us much in class. As in, if i had to say what chapter we were in, in the class, would be the chapter on expressions, we have not talked about functions, arrays, strings in class. the other people in my class have way more programming background than me, im a transfer student who has only had 1 other programming type class, it was just basic java. Im working out the mods on paper, then i will see how to put it into a function. Thanks for help, if i get stuck, then ill post the code im getting errors on.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Heres what i have atm:
    void formatInt(int x);


    int main(void) {

    int numBytes;
    printf("Enter a number of Bytes: ");
    scanf("%d", numBytes);


    return 0;

    I know how the % and / to get what numbers i want work. 98765432 / 1000000 = 98, and to get rid of the 98 you take 9876543 % 1000000, which gives you 765432. If a user is entering a number, of unknown length, is there a way to get the length of the number the user enters? or how else would i know what to / by to check the first digits to find out where im putting commas?

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    I hope that actually sounded like a logical question. How would i set up a loop to go through X numbers(unknown length of number user entered) and divide by the right number to get the first set of numbers up to 3. Its hard to word what im trying to ask, so I hope you understand what im trying to say.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok, it might not be part of my function, I just need to know how to determine how many digits the user enters.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by clmoore3 View Post
    Ok, it might not be part of my function
    Get the algorithm working first. THEN worry about making it a function.

    If you want to through the number forwards with your example of 98765432, then your loop has to do this

    98765432 / 10000000 % 10 == 9
    98765432 / 1000000 % 10 == 8
    98765432 / 100000 % 10 == 7
    ...
    98765432 / 1 % 10 == 2

    Notice that the first denominator for division is pow(10,n), where n is the base 10 logarithm of 98765432

    OR you can go through backwards as I suggested if you don't want to take a logarithm. It doesn't really matter. But if you go through backwards its easier to place the commas. Then you can just reverse the result.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I was going for a simpler approach of hard-coding a starting divisor (say, 1,000,000,000), since this is such a beginner class. However, c99tutorials method is just as easy (easier?) and more flexible, so I would take that route.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inputting commas
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2005, 01:41 PM
  2. to add commas
    By sweetly in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2003, 01:03 PM
  3. questions about commas again
    By CuriousNoob in forum C Programming
    Replies: 14
    Last Post: 01-12-2003, 01:32 AM
  4. Display with commas
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2001, 05:24 PM