Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-03-2004, 05:55 AM   #16 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
To me, the hardcoding part is the defined currencys ie:
Code:
const float CA_CURR = 1.263400;
Else it would have to be a file holding this info, and at runtime you would read in the current currencys.
The last part woudl be a much better way, the file could be designes like:
Code:
Oct 30, 2004
c CAD 1.2634
C CNY 8.2781
etc
Then you read first line, to decide, which current date the currencys represents.
You would read the following lines into a struct array like this:
Code:
typedef struct CURRENCY{
    char delim;
    char ilc[4];
    float currency;
    struct CURRENCY* next;
    struct CURRENCY* prev;
} CURRENCY;
Then you could scan through the array of structs looking for the from/to currency needed.


.... hmmmm perhaps I should make a proggy for wallstreet
Altho they might have some very very sofisticated ones...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-03-2004, 09:13 AM   #17 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
To me, the hardcoding part is the defined currencys
Ah yes.
__________________
Valmont is offline   Reply With Quote
Old 10-04-2004, 07:48 AM   #18 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
OMG!! I did not think it would be so involved!! You are a wonderful person. I hope I do not get questioned about this because I have no idea about what you did. I am just beginning this language, that looks very advanced. This is the latest explaination I received. It looks like it is just maybe half of what you did. I am not sure.

This may help you get started. I'm ordering things in the order that your program should have them in order to help you get started laying out your own version. You will have to fill in the details


#include statement(s) are first

Start main program (i.e. void main() )

declare variables (5 of these will hold the 5 currency conversion rates. 5 other ones will hold the amount's of foreign currency, and you will also need a "dollars" variable. You may need more variables depending on how you write your program.)

initialize variables as appropriate

Hard coded equations (5) for calculating the amount of foreign currency that equals 1.0 US dollars.

output statements to output the results.

end of the program.

Now, for the hard coded equations part. It should look something like this.

roubles=conversionfactor*dollars;

If you use the correct conversion factor then the variable roubles should hold the number of roubles that are equivalent to 1.0 US Dollars. This variable is then used in your output statement. You of course need to do this for 5 currencies. You will have 5 currency variables and 5 conversion factor variables. A common mistake is to use the wrong conversion factors (i.e. the one that gives numbers of dollars for 1.0 rouble).

I am very very grateful to you for taking the time that you did. I am so sorry it took you so long. As I said, and this is why I need help, I did not know it would be so involved.......
julia is offline   Reply With Quote
Old 10-04-2004, 08:45 AM   #19 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Here is a program, which will do that...
Keep my previus example as the solution, when the teacher tells you to conver all these examples into a currency conversion program...
Code:
#include <stdio.h>

int main()
{
  /* defining the calculation variables in this program */
  float CA_CALC, EU_CALC, FR_CALC, IT_CALC, JP_CALC, US_CALC;

  /* The conversion factor from USD to currency */
  const float CA_CON =    1.263400;
  const float EU_CON =    0.804645;
  const float FR_CON =    5.278000;
  const float IT_CON = 1558.090000;
  const float JP_CON =  110.180000;
  const float US_CON =    1.000000;

  /* the amount desired in the foreign currency */
  const float CA_AMM =    3.00;
  const float EU_AMM =   12.80;
  const float FR_AMM =    5.27;
  const float IT_AMM = 1200.00;
  const float JP_AMM =  110.18;
  const float US_AMM =    8.00;

  /* hardcoded equations of the ammount of foreign
   * currency equal to 1 USD */
  CA_CALC = US_CON * CA_CON;
  EU_CALC = US_CON * EU_CON;
  FR_CALC = US_CON * FR_CON;
  IT_CALC = US_CON * IT_CON;
  JP_CALC = US_CON * JP_CON;

  /* showing the ammount of foreign currency equals ONE (1) USD */
  printf("\t Currency conversion.\nConversion rates as of " __DATE__ "\n");
  printf("At this moment the currency is as follows:\n");
  printf("CAD %f\t=\t%f USD\n", CA_CALC, US_CON);
  printf("EUR %f\t=\t%f USD\n", EU_CALC, US_CON);
  printf("FRF %f\t=\t%f USD\n", FR_CALC, US_CON);
  printf("ITL %f\t=\t%f USD\n", IT_CALC, US_CON);
  printf("JPY %f\t=\t%f USD\n", JP_CALC, US_CON);
  printf("USD %f\t=\t%f USD\n", US_CON, US_CON);
  printf("\n");

  /* hardcoded calculation of the USD required to get
   * desired ammount in foreign currency
   */

  CA_CALC = CA_AMM  / CA_CON;
  EU_CALC = EU_AMM  / EU_CON;
  FR_CALC = FR_AMM  / FR_CON;
  IT_CALC = IT_AMM  / IT_CON;
  JP_CALC = JP_AMM  / JP_CON;

  /* showing the nessesary USD needed for ammount of foreign currency */

  printf("%f USD needed for %f CAD\n", CA_CALC, CA_AMM);
  printf("%f USD needed for %f EUR\n", EU_CALC, EU_AMM);
  printf("%f USD needed for %f FRF\n", FR_CALC, FR_AMM);
  printf("%f USD needed for %f ITL\n", IT_CALC, IT_AMM);
  printf("%f USD needed for %f JPY\n", JP_CALC, JP_AMM);
  printf("%f USD needed for %f USD\n", US_AMM, US_AMM);
  printf("\n");


  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-04-2004, 04:45 PM   #20 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Redhead,

In the meantime, I just wanted to let you see that I figured it out. I'm sure your's is better but at least I tried. Please see the following:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(void)
{
float eur_to_usd_rate = 1.2422;
float cad_to_usd_rate = 0.7912;
float cny_to_usd_rate = 0.1208;
float jpy_to_usd_rate = 0.0091;
float sek_to_usd_rate = 0.1373;
float equivalent_usd_amt;
int usd_amt = 1;

printf(" Currency Conversion\n");
printf("\n\n");
printf("Conversion rates as of September 30, 2004\n");
printf("\n\n");

printf("The exchange rate for the Euro is 1.2422 divided into 1 US
Dollar = 0.8050\n");
equivalent_usd_amt = usd_amt / eur_to_usd_rate;
printf("The equivalent USD Amount for the Euro is equal to %4.4f\n",
equivalent_usd_amt);
printf("\n\n");
printf("The exchange rate for the CAD is 0.7912 divided into 1 US
Dollar = 1.2639\n");
equivalent_usd_amt = usd_amt / cad_to_usd_rate;
printf("The equivalent USD Amount for the Canadian Dollar is equal to
%4.4f\n", equivalent_usd_amt);
printf("\n\n");
printf("The exchange rate for the CNY is 0.1208 divided into 1 US
Dollar = 8.2781\n");
equivalent_usd_amt = usd_amt / cny_to_usd_rate;
printf("The equivalent USD Amount for the Chinese Renminbi is equal
to %4.4f\n", equivalent_usd_amt);
printf("\n\n");
printf("The exchange rate for the JPY is 0.0091 divided into 1 US
Dollar = 109.8901\n");
equivalent_usd_amt = usd_amt / jpy_to_usd_rate;
printf("The equivalent USD Amount for the Japanese Yen is equal to
%4.4f\n", equivalent_usd_amt);
printf("\n\n");
printf("The exchange rate for the SEK is 0.1373 divided into 1 US
Dollar = 7.2833\n");
equivalent_usd_amt = usd_amt / sek_to_usd_rate;
printf("The equivalent USD Amount for the Swidish Krona is equal to
%4.4f\n", equivalent_usd_amt);
}


I want to thank you very much for your help with this. I really appreciate it.

Regards,
Julia
julia is offline   Reply With Quote
Old 10-04-2004, 05:51 PM   #21 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
float eur_to_usd_rate = 1.2422;
float cad_to_usd_rate = 0.7912;
float cny_to_usd_rate = 0.1208; 
float jpy_to_usd_rate = 0.0091; 
float sek_to_usd_rate = 0.1373;
For performance reasons, you'd like perhaps to make "hard coded" objects constants. Check out how redhead made it constant. Basically you're saying with this that the rates won't change (much). Otherwise "hard coding" doesn't make sense. But I'm sure you'll find out soon enough .

Perhaps you'll be interested in adapting his style of how define main():
Code:
int main()
{
   return 0;
}
vs "old style" incompatible versions:
Code:
void main(void)
{
}
Quote:
...but at least I tried.
Now that's something that is really appreciated around here .
__________________
Valmont is offline   Reply With Quote
Old 10-05-2004, 01:42 AM   #22 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Julia,

Your solution is probably what the teacher is looking for, he's not thinking of you as knowing every part in programming, thus this slow approach.
But you don't need to include string.h and stdlib.h those were only needed in my program, to access
strcpy(char *s1, const char *s2); malloc(size_t size) and free(void *ptr)

Good job on the giving it a try part, thats half the work..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:53 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting