Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
October 5th, 2013, 10:58 AM
 Oarta
Registered User

Join Date: Sep 2013
Posts: 5
Time spent in forums: 2 h 33 m 25 sec
Reputation Power: 0
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
October 5th, 2013, 12:06 PM
 rodiongork
Contributing User

Join Date: Sep 2013
Location: Saint-Petersburg, Russia
Posts: 215
Time spent in forums: 1 Day 17 h 18 m 38 sec
Reputation Power: 28
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).

Quote:
 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.

Quote:
 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]);```

Quote:
 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.

#3
October 6th, 2013, 01:46 PM
 Oarta
Registered User

Join Date: Sep 2013
Posts: 5
Time spent in forums: 2 h 33 m 25 sec
Reputation Power: 0
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!

Quote:
 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).
Quote:
 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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Simple C program, trouble with arrays, pointers, strings

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |