[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Create and use a function to perform a calculation

Posted on 2004-08-31
Medium Priority
282 Views
I must have a mental block.  I am having the hardest time trying to create and use a function to perform a calculation.  Listed below is the code.
I am trying to pass variables to a function and have it calcuate a float amount (dest_amt) using the convert function I attempted to creete.
I also need to figure out a way to accept an int return using a function for the menu selection instead of a scanf.  I started to create the accept function
for this.

#include <stdio.h>

float convert (int y, float dollar_amt, float conv_factor, float dest_amt);
int main()

{
/* Initialize variables */
static float conv_factor[5]={1.4013345291,110.864745,.54896794,.817126982,1.332196526};
static int x,y;
static float dollar_amt,dest_amt;
static char* title[1]={"Currency Converter"};
static char* input_amt[1]={"Input amount to convert in U.S. Dollars \$ "};
static char* disp[2]={"The amount you input in U.S. Dollars is worth ", " in "};
static char* cur[5]={"Australian Dollars.", "Japanese Yen.", "British Pounds.", "European Union Euros.", "Canadian Dollars."};
static char* cur_menu[6]={"1) Australian Dollars", "2) Japanese Yen", "3) British Pounds", "4) European Union Euros", "5) Canadian Dollars", "6) Stop Program"};
/* Input Section creating the menu options */

{
printf(title[0]);
printf("\n\n");
x = 0;

while (x !=6)
{
printf("\n");
x = x + 1;
}
printf("\n\n");

return 0;
return main();
/* Prompt for input amount */
printf("\n");
printf(input_amt[0]);
scanf("%f",&dollar_amt);
/* Function to calculate conversion and display data */
float convert(int y, float dollar_amt, float conv_factor[y])
{
float dest_amt=0;
dest_amt=(dollar_amt*conv_factor[y]);
return (dest_amt);
}

printf(disp[0]);
printf("%f",dest_amt);
printf(disp[1]);
printf(cur[y]);
printf("\n\n");

}
return 0;

}
0
Question by:nfunaro
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 23

Accepted Solution

brettmjohnson earned 2000 total points
ID: 11948368
You are going overboard on your arrays.

> static char* title[1]={"Currency Converter"};
> ...
>   printf(title[0]);
>   printf("\n\n");

can be simplified to

char* title = "Currency Converter";
...
printf("%s\n\n",title);

Or even simpler:

puts("Currency Converter\n");

>      x = 0;
>
>       while (x !=6)
>               {
>       printf("\n");
>       x = x + 1;
>            }

This loop has an initialization (x = 0), a top-of-loop-test (while (x != 6)), and an increment at the end ( x = x + 1 ).
When all these are present, it is preferable to use the for() loop syntax for much less clutter:

for (x = 0; ( x < 6); x++)

>        return main();

I am not sure recursion is the appropriate way to handle this type of error.
Your command loop already repeatedly prompts for a valid input.  I think
a break, continue, or even goto would be a better control flow construct
than recursion.

>     float convert(int y, float dollar_amt, float conv_factor[y])
>     {
>      float dest_amt=0;
>      dest_amt=(dollar_amt*conv_factor[y]);
>     return (dest_amt);
>     }

You cannot implement the function in the middle of the main() function.
It needs to stand alone.  However, since it does nothing other than a
multiplication, why not simply do that in-line and forego the extra function:

dest_amount = dollar_amt * conv_factor[y];

>        printf(disp[0]);
>       printf("%f",dest_amt);
>       printf(disp[1]);
>      printf(cur[y]);
>      printf("\n\n");

printf takes a format string that allows it to format for display several items,
strings numbers, delimiters, etc.  Learn how to use it effectively since long
lists of calls to printf displaying one item at a time is not only clutter, it is very
slow to process.

printf("%s %f %s %s\n\n", disp[0], dest_amt, disp[1], cur[y]);

Make some of these simplifications, learn how to properly use printf() to
format several items for display at once, eliminate extraneous clutter and
complexity, and your true desired design will be much more clear to you.

0

Author Comment

ID: 11948397
This is really great feedback.  There is, however, one small problem.  I need to use a function to bring back the calculated amount.  This was the main reason for the question.  Everything else was fantastic help though.  Can you help with the function piece?
0

## Featured Post

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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
Course of the Month14 days, 11 hours left to enroll