Solved

# Pleace Review this program I'm using Miracle C

Posted on 2006-04-24
309 Views
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");

}
0
Question by:martinez1112

LVL 53

Accepted Solution

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

Author Comment

I will try that.
0

LVL 53

Expert Comment

Post your code once you have something, and We'll take a look at it ...
0

Author Comment

I will post the code after I get it done later.  Thanks for your help.
0

LVL 45

Expert Comment

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

Author Comment

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

LVL 45

Expert Comment

Hi martinez1112,

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

Kent
0

Author Comment

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

LVL 45

Expert Comment

Hi martinez1112,

Yep.

Kent
0

Author Comment

Great I did it.
thanks Ken
It does look a lot better.

Gabe
0

## Featured Post

### Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.