# This multivariable problem seems so simple but I can't figure it out.

This is a discussion on This multivariable problem seems so simple but I can't figure it out. within the C Programming forums, part of the General Programming Boards category; I need to make a program that calculates the area of a circle but I have to use an int, ...

1. ## This multivariable problem seems so simple but I can't figure it out.

I need to make a program that calculates the area of a circle but I have to use an int, char, and float. No matter what I do though I can't get the second and third variables to output meaningful values, only the first. This is what I have written down so far.
Code:
```#include <stdio.h>
#include "conio.h"

#define PI 3.14159

int main()

{
/* The following is a program to demonstrate several different c numerical input funtions:interger, float, character.*/

float radius1, area_float;  //declares the radius that will be used for float

printf("Input radius for float, please? ");  // Promts the user to enter a value for float
scanf("%f", &radius1 ); // obtains the value from the float promt

area_float = PI * radius1 * radius1; //Equation for area of a cricle
printf("area1 = %f\n", area_float); //Prints the answer for float
//Forces the command console to pause because c is used.

printf("Input radius for int value, please? ");

area_int = PI * radius * radius;

printf("area2 =%d\n", area_int);
_getch();

printf("Input radius for char value, please? ");
printf("area3 =%c2\n", area_char);
_getch();
return 0;

}```
I'm not sure what to do and I've been working on this for hours Thanks for any help or advice!

2. printf("Input radius for int value, please? ");

area_int = PI * radius * radius;

printf("area2 =%d\n", area_int);
_getch();
You are calculating area first (the variable area_int has some arbitrary value stored upon declaration and it is this arbitrary value that is used in your calculation) and then you are asking the user to input the value of radius. So the area that you see is actually garbage.

The order should be reversed as below
Code:
```printf("Input radius for int value, please? ");

area_int = PI * radius * radius;

printf("area2 =%d\n", area_int);
_getch();```
In the part where you are using char, note that char uses the ascii value of the variable and does NOT read 1 as the number 1. rather it interprets it as the ascii character for 1. Hence the final answer that you see is actually the ascii symbol for the value that is obtained upon calculation of pi*r^2.

You need to convert the characters to their respective values (in int).

3. > area_int = PI * radius * radius;
> scanf("%d", &radius );
> printf("area2 =%d\n", area_int);
Should you read the value, before trying to do some maths on it?

Let's say you type in '4', where the ASCII value is 52.
So 52 * 52 * 3, will give you some value a lot larger than a char, and will not produce something meaningful when printed as a single char.

4. But I don't understand why the int value for the radius still returns nonsense, it returns a small negative number. Not only that but I get an error saying that radius is being used without being initialized.

5. Because you are using radius without initializing it, that is why you get the error saying that you are using radius without initializing it. Don't do that. Initialize first, use later.

6. Originally Posted by caysonmars
But I don't understand why the int value for the radius still returns nonsense, it returns a small negative number. Not only that but I get an error saying that radius is being used without being initialized.
Did you make the changes that were suggested?

Radius is being use without being intialized because you are using in your calculations it before you load it with any meaningful value.
Hense, everyone's suggestion that you change the order of those lines.

C isn't smart --and especially not mind reader smart-- it simply does stuff in the order it finds it.

7. > Not only that but I get an error saying that radius is being used without being initialized.
You should pay attention to it.

Consider this
> area_int = PI * radius * radius; <-- switch on kettle
> scanf("%d", &radius ); <-- add water to kettle

Do you understand cause and effect?

C doesn't do "deferred execution", where you can just list a bunch of instructions in any order, and then add information later on and have it re-calculate all dependencies. If radius doesn't have the right value by the time you hit the calculation, then you're stuffed.

8. Jeez I can't believe how many simple mistakes I made. I switched the code around so many times I think that's how the area got messed up. This is what I have now, the first two variables actually work! But for some reason the program doesn't allow me to enter in a character. It just displays ASCII instead of prompting me for input after the first two variables are entered. I'm not sure why this is. I erased and retyped the code a couple of times but I can't fix the problem.

Code:
```#include <stdio.h>
#include "conio.h"

#define PI 3.14159

int main ()
{

area = PI * radius * radius;
printf("Area = %d\n", area);
_getch();

area1 = PI * radius1 * radius1;
printf("Float area= %f\n", area1);
_getch();

area2 = PI * radius2 * radius2;
printf("Char Area = %c\n", area2);
_getch();

return 0;

}```

9. Assuming you don't want enter-key to count as a character, then you need to use " %c". Also, if the area is, let's say 74, then printing 74 as a character gets you J -- you don't get to see 74.

10. I did use "%c" after scanf

11. Originally Posted by caysonmars
I did use "%c" after scanf
Which is why it's wrong. (I.e., read the post again.)

Popular pages Recent additions