Wrong output of program [duplicate] - c

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ float t=1/2;
printf("\n%.4f",t);
return 0;
}
i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not.
cant seem to find the problem.
please help.
Thanks.

I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.
TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

Related

Unexpected Output- long double [duplicate]

This question already has answers here:
printf and long double
(8 answers)
Closed 1 year ago.
I have written the code as follows
#include <stdio.h>
int main()
{
long double var = 3.1415926535;
printf("%.6Lf", var);
}
The output of the code is 0.000000. According to me output should be 3.141592. Please tell what mistake I am doing.
Try to use GDB compiler, run your code in steps and see where is the problem and your variable changes its value. This is how we learn program dont expect every time to others to solve your problem, this method must be the last. Take a look at this GDB tutorial and fix this problem at your own.

Very simple multi array c program - problem noob [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?
You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.

"warning: division by zero [-Wdiv-by-zero]" when not dividing by zero [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 3 years ago.
My code for the following simple coding exercise produces a "division by zero" warning, and I'm wondering why.
#include <stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
}
return 1;
}
temps.c: In function ‘main’:
temps.c:6:45: warning: division by zero [-Wdiv-by-zero]
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
I realised while writing this question that it's because I should have written 5.0/9.0, since C handles division with integers in a way that I didn't expect. Posting this anyway since I couldn't find this particular error linked to this particular problem on SO.

How to succesfully pass a 2D array to a function? [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 7 years ago.
GNU GCC compiler
Here is a function: int sumsintriangle(int *a,int n)
where a is a n*n matrix .
for some purpose I added
if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))
condition to my code which was working properly ;as the condition is true for the correct values.
but for the same code when I added
sum=sum + *(a+(i+1)*n+(j+1));
then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why??
Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?
Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?
If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly.
example:
int a[]; //declare an array
a[n] // will give you the element in position 9 of the array.
*a // will give you the first element, cause an array can be treated as a pointer (indeed it is).
I hope this answer help you. If not please tell me. Good luck!
Use This code code may be its work.
*(a+(i+1))*n+(j+1)

printing float value 0.9 in C [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
Please do flog me for this really basic question, but I still can not get why this happen.
I read that for printing several value behind coma, I should use %.f in C.
So I have this problem, counting 90/100. I expect to print 0.9
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90/100;
printf("%.1f\n", c);
}
And it shows me 0.0 ..(err..) . tried to change it into (printf("%f\n",c)) return me with 0.00000.. (err..)
Can anyone help me with this? (sorry, really new in programming..)
Thank you
The problem is that you are doing integer division. 90/100 = 0 in integer terms.
If you want to get 0.9, do : 90.0/100.0
The problem is at
c = 90/100;
Although, it will be assigned to a double data type, but the computation itself is all integer and that's why the value is 0.0.
Try,
c = 90.0/100;
it is integer division, do:
c = 90.0/100;
c = (float)90/100;
you need to make atleast one operant a double to evaluate the whole equation as double
You are dividing two integers, thus the result is an integer too. Try the following
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90.0/100;
printf("%.1f\n", c);
}

Resources