Google

Monday, April 12, 2010

CSC202 Revision April 14 - Part 3

QUESTION:

Note that (a), (b),(c) and (d) below are all related to the same program. The program converts
any given currency into USD.
(a) Write the a function prototype called convert() that takes the value of any currency and
returns the USD currency.

(b) Write a main function, then, declare variable currency of type float and declare another
variable usd_currency of type float.

(c) Create a perpetual loop using the statement for(;;)
Within this loop, do the following:
(i) prompt the user: “Enter currency value, 0 to quit: “
(ii) get the input using function scanf() and storing it in variable currency
(iii) using an if statement, quit the program if user enters 0.
(iv) prompt the user: “Enter Currency/USD exchange rate: “
(v) get the input using function scanf() and storing it in variable rate
(vi) call function convert(), passing it the value currency and rate and
storing the return value in usd_currency.
(vii) print the result, eg, “100.00 = USD 139.00”

(d) Write the function definition for the function convert().

SOLUTION:
(a)
float usd convert(float foreign);

(b)
float rate=0;
void main(){
float currency, usd_currency;
}

(c)
for(;;){
printf("Enter currency value, 0 to quit: ");
scanf("%f",&currency);
if(currency==0) return;
printf("Enter Currency/USD exchange rate: ");
scanf("%f",&rate);
usd_currency = convert(currency,rate);
printf("%.2f = USD %.2f\n",currency,usd_currency);
}
(c)
float convert(float currency){
return currency * rate;
}