Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pleace Review this program I'm using Miracle C

Posted on 2006-04-24
10
Medium Priority
?
313 Views
Last Modified: 2010-04-15
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
Comment
Question by:martinez1112
  • 5
  • 3
  • 2
10 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 16522807
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

by:martinez1112
ID: 16522825
I will try that.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16522838
Post your code once you have something, and We'll take a look at it ...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:martinez1112
ID: 16522944
I will post the code after I get it done later.  Thanks for your help.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16526383
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

by:martinez1112
ID: 16527597
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 46

Expert Comment

by:Kent Olsen
ID: 16527692
Hi martinez1112,

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


Kent
0
 

Author Comment

by:martinez1112
ID: 16527742
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 46

Expert Comment

by:Kent Olsen
ID: 16527811
Hi martinez1112,

Yep.


Kent
0
 

Author Comment

by:martinez1112
ID: 16527920
Great I did it.
thanks Ken
It does look a lot better.

Gabe
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question