What is wrong with this program? From Celsius to Fahrenheit [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include <stdio.h>
main() {
int i;
printf("Celsius Fahr\n");
for(i=0; i<20; i++)
printf("%7d %4.2f\n", i, convert(i));
return 0;
}
int convert(int a) {
float b;
b=(1.8*a)+32;
return b;
}
It gives me as output all the Fahrenheit numbers as zero. What did I do wrong?

Change main() to int main().
Change int convert(int a) to float convert(int a).
Add a function prototype float convert(int); before int main().
Use proper indentation.
Modified code :-
#include <stdio.h>
float convert(int); // Funtion prototype
int main()
{
int i;
printf("Celsius Fahr\n");
for (i = 0; i < 20; i++)
printf("%7d %4.2f\n", i, convert(i));
return 0;
}
float convert(int a)
{
float b;
b = (1.8 * a) + 32;
return b;
}
Output :-
Celsius Fahr
0 32.00
1 33.80
2 35.60
3 37.40
4 39.20
5 41.00
6 42.80
7 44.60
8 46.40
9 48.20
10 50.00
11 51.80
12 53.60
13 55.40
14 57.20
15 59.00
16 60.80
17 62.60
18 64.40
19 66.20

Related

Multiple errors in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not able to understand where I am going wrong. Please help! I am new to the website. Appreciate all the help. Thanks a lot :D
#include <stdio.h>
int main()
{
printf("Hello World")
}
int factorial(int x) {
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int a = 9;
int b;
b = factorial(int a);
printf("%i", b);
I have corrected the code and added some comments. I also rearranged the factorial slightly, so that it works for 0! which is 1.
#include <stdio.h>
int factorial(int x) { // added the argument type int
int product = 1; // use another variable
for(int i = 2; i <= x; i++) {
product *= i;
}
return product;
}
int main()
{
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
Note that you can only generate up to 12! and after that you get overflow due to the range of a 32-bit int.
First here printf("Hello World") you are missing ;
Second add this part to your main.
int main()
{
printf("Hello World");
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
and when you are calling your function in main,you shouldn't send int a to function b = factorial(int a) ,because by saying int a instead of a you are redefining it.(so it will be uninitialized,if redefinition is not error)
also as said in comments you should add a prototype for factorial before main or move it before main.
Finally your loop in factorial is infinitive ,for(i=1; i < x; i++) since you're doing x *= i;
this condition i < x is never true.
you will increase x until int type has not enough space for it. so a garbage value will be assigned to it ,and you will exit the loop.

pow function in C not working as intended [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have the following equation I need to represent in C:
20 * 2^((1-x)/5)
my c representation is as follows, but it seems like the pow function is always returning a high integer value (1073741824) with my n values ranging from 1-5.
double x = 20 * pow(2.0, (n/5.0));
I assume it is because both arguments are not double values, but I do not see why. Is there a different way to format this equation to get it to work?
I just compiled the example you give and it works
#include <stdio.h>
#include <math.h>
int main () {
for (int n = 1; n <= 5; ++n) {
double x = 20 * pow(2.0, ((1-n)/5.0));
printf("%lf ", x);
}
}
Output
20.000000 17.411011 15.157166 13.195079 11.486984
Make sure you use int n and not unsigned n. In case of unsigned you will get (1 - n) overflow and pow will return inf.
Your compiler is assuming pow() returns an int,
Remeber to #include <math.h> for the proper prototype
#include <math.h>
#include <stdio.h>
// ipow works as pow when the compiler assumes an int return value
int ipow(double base, double exp) {
double res = pow(base, exp);
return *(int*)((void*)&res);
}
int main(void) {
for (int n = 1; n < 6; n++) {
double x = 20 * pow(2.0, ((1-n)/5.0)); // with correct prototype
double y = 20 * ipow(2.0, ((1-n)/5.0)); // when compiler assumes int
printf("n=%d, x=%f, y=%f\n", n, x, y);
}
return 0;
}
See https://ideone.com/XvPWX6
Output:
n=1, x=20.000000, y=0.000000
n=2, x=17.411011, y=422418048.000000
n=3, x=15.157166, y=1240833840.000000
n=4, x=13.195079, y=-1971682192.000000
n=5, x=11.486984, y=-2036727536.000000

why the output of C program is wrong?i have checked for a long time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Here is the C source file i write, after i run it, i input 1 1 -2 1 , but the output is not x1=1.000000,x2=1.000000? the output is x1=2.000000,x2=2.000000 .
#include <stdio.h>
#include <math.h>
void fun(float a, float b, float c);
int main()
{
int n,i;
float a, b, c;
printf("please enter the times you want to try:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("please enter the a,b,c:\n");
scanf("%f%f%f", &a, &b, &c);
fun(a, b, c);
}
return 0;
}
void fun(float d, float e, float f) {
float x1, x2;
if (e*e - 4 * d*f < 0)
printf("no answer\n");
if (e*e - 4 * d*f >= 0) {
x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d); //should work better
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);
printf("the roots are x1=%f x2=%f\n", x1, x2);
}
}
x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d);
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);

Composite Simpson's Rule in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to program Composite Simpson's Rule in C. The formula is:
where x_j=a+jh for j=0, 1, ..., n-1, n with h=(b-a)/n; in particular, x_0=a and x_n=b.
For some reason, the first and second loop have the same value. I checked over this a lot of times, but I can't seem to find my mistake.
#include <stdio.h>
#include <math.h>
float f(float);
float a;
float b;
float x;
float h;
int n;
int j;
a=0;
b=2;
n=8;
h = (n - j) / b;
float first;
float second;
int main() {
sum = (h / 3.0f) * (f(h) + f(n));
printf("%f\n", sum);
second = (4.0f) * h * f(a);
}
printf("second sum: %f\n",second );
sum = sum + first + second;
printf("%f\n", sum);
return 0;
}
The answer should be around 3.1 (The value of the final sum)
Your divisions won't probably do what you expect:
(2 / 3) == 0
Dividing int with int will result in int.
Use float constants (2.0f / 3.0f)
Edit:
You still have the same problem with the other n / 2.
And you should use %f when printing floats: printf("first sum: %f\n",first);
The value of the integral is: 3.109337

How can I edit this function call to scan only an half of the array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
#define N 10
int find_largest(int *, int );
int main(void) {
int a[] = {1,2,23,4,5,2,14,6,8,10};
printf("%d\n", find_largest (a, N));
}
int find_largest(int *a, int n) {
int i, max;
max = a[0];
for (i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
How can I edit the function call so that the program prints the maximum number of the second half of the array, so among these elements: {2, 14, 6, 8, 10}?
As I said, I should edit only this line:
printf("%d\n", find_largest (a, N));
Thank you :)
You can change that line as:
printf("%d\n", find_largest (a+N/2, (N+1)/2));
The (N+1)/2 can handle the exception when N is an odd number.
Change
printf("%d\n", find_largest (a, N));
to
printf("%d\n", find_largest (a + N/2, N/2));
printf("%d\n", find_largest (a+N/2, (N+1)/2));

Resources