• C

Pleace Review this program I'm using Miracle C

Hello I'm using Miracle C for this program that I modified from last week one.  
Apparently, it work fine but I just want to know if there is a way to diminutive the code.


#include <stdio.h>
#include <math.h>

main()
{
/*HARD CODE Information from
the currency converter posted in the course materials*/
float USD = 1.00; /*1.00 USD = 1.00 United States Dollar*/
float EUR = 0.9223357; /*1.00 USD = 0.9223357 Europe EURO*/
float JPY = 118.218; /*1.00 USD = 118.218 Japan Yen*/
float MXN = 10.79889; /*1.00 USD = 10.79889 Mexico Pesos*/
float RUB = 31.9191; /*1.00 USD = 31.9191 Russian Ruble*/
float SEK = 8.48792; /*1.00 USD = 8.48792 Swedish Kronor*/
char chrUserInput[16];
float sum = 0.0;
float sum0 = 0.0;
float sum1 = 0.0;
float sum2 = 0.0;
float sum3 = 0.0;


/*print title of program*/
printf("\n\tMultiple Currency Conversion");/*In previous Version the Title was just Currency Convertion*/
printf("\n");
/*Ask for amout of $ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t$\a");
scanf("%f", &USD);

/*Calculation*/

sum= (USD * EUR);
sum0= (USD * JPY);
sum1= (USD * MXN);
sum2= (USD * RUB);
sum3= (USD * SEK);

/*Amount Converted will be show*/

printf ("\n");
printf ("With that amount you will get...\n");/*statement that user will see on the top of the convertions*/
printf ("\n");
printf ("\t%3.2f\tEUR\n", sum);
printf ("\n");
printf ("\t%3.2f\tJPY\n", sum0);
printf ("\n");
printf ("\t%3.2f\tMXN\n", sum1);
printf ("\n");
printf ("\t%3.2f\tRUB\n", sum2);
printf ("\n");
printf ("\t%3.2f\tSEK\n\n", sum3);


/* End the Program with the message */
printf ("\tConversion complete \"I DID IT AGAIN\"\a");

}
martinez1112Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Since you're doing the same thing 5 times, why not make a loop of it ?

Since the only difference is the monetary conversion value, you put those in an array - something like this :

float conv[] = { 0.9223357, /* EUR : 1.00 USD = 0.9223357 Europe EURO */
                       118.218, /* JPY : 1.00 USD = 118.218 Japan Yen */
                       10.79889, /* MXN : 1.00 USD = 10.79889 Mexico Pesos */
                       31.9191, /* RUB : 1.00 USD = 31.9191 Russian Ruble */
                       8.48792 }; /* SEK : 1.00 USD = 8.48792 Swedish Kronor */

float conv_str[] = { "EUR", "JPY", "MXN", "RUB", "SEK" };

And then use a loop to calculate the sum and print the result :

int i;
float sum;
for (i = 0; i < 5; i++) {
  // calculate sum based on the conv array and the input value
  // print the result based on the conv_str array
}

A next step could be to put both of the above arrays into 1 array, using a struct !!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
martinez1112Author Commented:
I will try that.
0
Infinity08Commented:
Post your code once you have something, and We'll take a look at it ...
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

martinez1112Author Commented:
I will post the code after I get it done later.  Thanks for your help.
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi martinez1112,

The loop's not a bad idea, but perhaps not the end-all, be-all in this case.  A better approach may be to name the variables appropriately.  You did a wonder job of giving the conversion rates meaningful names (USD, EUR, etc.) then called the results sum0, sum1, etc.

totalEuros = USD * EUR;
totalYen = USD * JPY;

etc....


Good Luck!
Kent
0
martinez1112Author Commented:
Do you mean

istead of   sum= (USD * EUR);
                sum0= (USD * JPY);


Change that to   totalEuros = USD * EUR;
                         totalYen = USD * JPY;

in the /*Calculatio*/  area?
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi martinez1112,

Yes.  It will make the program a lot more understandable at a glance.


Kent
0
martinez1112Author Commented:
another question, If A change that in the /*calculation*/ area, I will have to change in the hard code area also

float sum = 0.0;
float sum0 = 0.0;
float sum1 = 0.0;
float sum2 = 0.0;
float sum3 = 0.0;

to  totalEuros = 0.0;
     totalYen = 0.0;
and so on.
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi martinez1112,

Yep.


Kent
0
martinez1112Author Commented:
Great I did it.
thanks Ken
It does look a lot better.

Gabe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.