#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Question Simple C program, trouble with arrays, pointers, strings


    First of all, I'm very new in C programming. Currently I need to write a simple program which converts a decimal number to the color code for resistances or capacitors in electrical circuits. For example: I input the number "4700", and it should print on the screen the color code which is yellow (4) violet (7) red (exp. 2). This is what I have so far:
    Code:
        int nDecimalcounter = 0;
        double dInput;
        char szColor[10][7] = {"black", "brown", "red", "orange", "yellow",
            "green", "blue", "violet", "grey", "white"};
    
        printf("Give decimal value: ");
        scanf("%lf", &dInput);
    
        while (dInput > 99) {
    
            dInput = dInput / 10;
            nDecimalcounter = nDecimalcounter + 1;
        }
    So if I for example enter the number 4700, I'm left with an int value for decimalcounter, and a (double with the first 2 characters in it.
    So here are my 2 problems:
    1. I need to separate the 2 characters in the double, I need to end up with (double) 4 and (double) 7.
    2. Once I have 3 separate numbers, I don't know how to assign the correct colors to each separate number, and print these on the screen (instead of the numbers 4, 7, and 2). I'm thinking I need to use pointers or something like that but it confuses me.

    Can anyone help me with this? (even the smallest suggestions are welcome)
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    If you are so new to C programming, why not switch to Python? You'll be new to it too, but it will appear sometimes easier and more handy for your purposes (I do not mean C is bad).

    1. I need to separate the 2 characters in the double, I need to end up with (double) 4 and (double) 7.
    I once added an article to my site about such conversion:
    Number to digits


    Strictly speaking it is almost the same what you've done with decimalCounter already...

    Other idea is to use sprintf or ftoa to convert value to string and fetch characters from here.

    2. Once I have 3 separate numbers, I don't know how to assign the correct colors to each separate number
    Why? Array is just the thing invented for fetching something by index. In your case it is something like:

    Code:
    digit = 4;
    printf("%s", szColor[digit]);

    even the smallest suggestions are welcome
    Most sincere (and important) suggestion is - try to learn the language in bit more systematic way. For example lay your hands on book by Kernigan and Ritchie. Also I dare recommend to practice on simpler problems (like these ) before approaching more complex.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Thank you for your reply.
    First of all, I've got the program working correctly now by using remainders to separate the number in digits, and then use the variable as array-index. Actually a pretty simple solution but I just didn't think of it. So thanks again for these suggestions!

    If you are so new to C programming, why not switch to Python? You'll be new to it too, but it will appear sometimes easier and more handy for your purposes (I do not mean C is bad).
    Most sincere (and important) suggestion is - try to learn the language in bit more systematic way. For example lay your hands on book by Kernigan and Ritchie. Also I dare recommend to practice on simpler problems (like these ) before approaching more complex.
    Yes it's not quite ideal but this was an obligatory assignment for my study (must be done in C). I only have a very extensive book (problem solving and program design in C by Hanly and Koffman) which I hardly use because of the extensiveness, and a vague class. Don't really have time to 'get more prepared' for these assignments so I'm learning by youtube tut's, google and this forum.

IMN logo majestic logo threadwatch logo seochat tools logo