CODE GIVES WRONG ANSWER

My solution won't work. Had to use typecasting because x was previously used as int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

int main(void)
{

	(int) x;
	double y;
	double quotient;


	printf("Enter a value for x:>	");
	scanf("%f", &x);

	printf("Enter a value for y:>   ");
	scanf("%lf", &y);


	quotient = (int) x  / y;
	printf("When x and y are both type double then x / y = %.2f\n", quotient);

	printf("When x and y are both type int then x / y = %.1f\n", quotient);

	printf("When x is of type double and y is of type int then x / y = %.2f\n", quotient);

	printf("When x is of type int and y is of type double then x / y = %.2f\n", quotient);

}
How can I get it to compile ? I can't declare x a double cause then it'll give me an error of redefinition;different basic types. Was everything else correct?
closed account (GwU9E3v7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>


int main(void)
{

	int x;
	double y;
	double quotient;


	printf("Enter a value for x:>	");
	scanf("%f", &x);

	printf("Enter a value for y:>   ");
	scanf("%lf", &y);


	quotient = x/y;
	printf("When x and y are both type double then x / y = %.2f\n", quotient);

	printf("When x and y are both type int then x / y = %.1f\n", quotient);

	printf("When x is of type double and y is of type int then x / y = %.2f\n", quotient);

	printf("When x is of type int and y is of type double then x / y = %.2f\n", quotient);

}


That got it to compile. But check if it is supposed to work your way.

:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>

int main()
{
    double first ;
    printf(" Enter a value for the first number:>	");
    scanf( "%lf", &first );

    double second ;
    printf("Enter a value for the second number:>   ");
    scanf( "%lf", &second );

    // assume that first and second are within [INT_MIN,INT_MAX]

    {
        const double x = first ;
        const double y = second ;
        if( y != 0 )
        {
            const double quotient = x / y;
            printf("\nWhen x and y are both type double,  x / y =  %.2f / %.2f = %.2f\n", x, y, quotient);
        }
    }

    {
        const int x = first ;
        const int y = second ;
        if( y != 0 )
        {
            const double quotient = x / y;
            printf("When x and y are both type int,  x / y = %d / %d = %.2f\n", x, y, quotient);
        }
    }

    {
        const double x = first ;
        const int y = second ;
        if( y != 0 )
        {
            const double quotient = x / y;
            printf("When x is of type double and y is of type int,  x / y = %.2f / %d = %.2f\n", x, y, quotient);
        }
    }

    {
        const int x = first ;
        const double y = second ;
        if( y != 0 )
        {
            const double quotient = x / y;
	        printf("When x is of type int and y is of type double,  x / y = %d / %.2f = %.2f\n", x, y, quotient);
        }
    }
}
I had already used int x in the 2nd part of the program, It's a 3 part assignment. So when I do int x it would say redefinition.
If the same name x is to be reused for something else, place the declaration in a separate block scope.

1
2
3
4
5
6
7
8
9
10
11
int x = 5 ;

// double x = 23.4 ;  // *** error: redefinition

{
    double x = 23.4 ; // fine, this hides the name 'x' in the outer scope 
    x += 1 ; // refers to double 'x' in the current (inner) scope 
    // ...
}

x += 1 ; // refers to int 'x' in the current (outer) scope  
closed account (GwU9E3v7)
You said,
It's a 3 part assignment. So when I do int x it would say redefinition.


and before, you said
How can I get it to compile ?
.

I only answered the
How can I get it to compile ?
part.

But for the
It's a 3 part assignment.
part, i am sorry but we don't do other people's homework or work. it would be nice for you to think of your own answer than asking for other answers for you.

I did get it to compile, and i think JLBorges was nice of him to do it for you.
Topic archived. No new replies allowed.