I have code that needs to run some functions with arguments from stdin. First function counts factorial of argument, second one counts length and square of round with radius presented by argument. Third needs just to printf its arguments.But after my input I have very strange result. My IDE is Xcode
Input:
5
1.3
8
8
fgd
Expected output:
120
Obvod: 8.168134 Obsah: 5.309287
88fgd
Real output:
120
Obvod: 8.168134 Obsah: 5.309287
88
fg
Whats wrong with last input? Thanks a lot in advance for your answer! The whole code below:
#include <stdio.h>
#include <stdlib.h>
int factorial (int value)
{
int fac = value;
if (value == 0)
{
return 1;
}
else if (value < 0)
{
return 0;
}
for (int i = 1; i < value; i++)
{
fac *= value - i;
}
return fac;
}
void radius (float rad, float* lep, float* sqp)
{
const float pi = 3.14159;
if (rad < 0)
{
printf("Obvod: 0 Obsah: 0\n");
}
float lenght = 2 * pi * rad;
float square = pi * (rad * rad);
*lep = lenght;
*sqp = square;
printf("Obvod: %f Obsah: %f\n", lenght, square);
}
void read_array_data(int h, int w, char x1, char x2, char x3)
{
printf("%i%i%c%c%c\n", h, w, x1, x2, x3);
}
int main()
{
char c1;
char c2;
char c3;
int f, height, width;
float r;
float radius_container, square_container;
float* p1 = &radius_container;
float* p2 = &square_container;
scanf("%i", &f);
scanf("%f", &r);
scanf("%i", &height);
scanf("%i", &width);
scanf("%c%c%c", &c1, &c2, &c3);
printf("%i\n", factorial(f));
radius(r, p1, p2);
read_array_data(height, width, c1, c2, c3);
}