when i run the program I enter a value for n and the program ends whys is that?
//fahrenheit values for celcius degrees between 0-n
#include<stdio.h>
int main()
{
int n; double c=0, f;
printf("n:"); scanf("%d", n);
while(c<=n)
{
f=(9*c+160)/5;
printf("%f celcius = %f fahrenhayt \n", c, f);
c++;
}
return 0;
}
i was expecting the program to print the values from 0-n line by line but after entering the value program just ends
You forgot to provide the address of n to scanf by adding & in front of the n:
scanf("%d", &n);
Try again. Here is the code corrected.
#include<stdio.h>
int main()
{
int n; double c=0, f;
printf("n:"); scanf("%d", &n);
while(c <= n) {
f = (9 * c + 160) / 5;
printf("%f celcius = %f fahrenhayt \n", c, f);
c++;
}
return 0;
}
Related
I am new to c and I am trying to make a calculator that asks if you want to calculate the standard deviation of a set or the average of a set. However when I run the code I get the error seen in the picture. Error screen. I don't know how to correct it. I believe I have also made some mistakes throught the code so if you see anything else a miss would you mind pointing it out. Thank you.
#include <stdio.h>
#include <math.h>
float StandardDev(float data[]);
float Average(float data[]);
float Average(float data[]) {
int n;
float num[100], sum = 0.0, avg;
avg = sum / n;
return 0;
}
float StandardDev(float data[]){
float sum = 0.0, mean, SD = 0.0;
int i, n;
for (i = 0; i < n; ++i) {
sum += data[i];
}
mean = sum / n;
for (i = 0; i < n; ++i) {
SD += pow(data[i] - mean, 2);
}
return sqrt(SD / 10);
}
int main()
{
int n, i;
float num[100], sum = 0.0, c;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%lf", &num[i]);
sum += num[i];
}
printf("Do you want to calculate the Standard Deviation(1) of a set or find the Average(2) of a set? ");
scanf("%u", &c);
if(c==1){
printf("The Standard Deviation of your set is %f", StandardDev);
}
else{
printf("The average of your set is %f", Average);
}
return(0);
}
You declared an array with the element type float
float num[100], sum = 0.0, c;
So you need to use the conversion specifier %f instead of %lf. The last is designed for objects of the type double
scanf("%f", &num[i]);
In this call of scanf
scanf("%u", &c);
you are using the conversion specifier %u designated for objects of the type unsigned int with an object of the type float
Declare the variable c like
unsigned int c;
In these calls of printf
printf("The Standard Deviation of your set is %f", StandardDev);
printf("The average of your set is %f", Average);
You are trying to output function pointers instead of results of the function calls.
It seems you mean something like the following
printf("The Standard Deviation of your set is %f", StandardDev( num ));
printf("The average of your set is %f", Average( num ) );
Pay attention to that the program will have undefined behavior at least because you are using uninitialized variables in the functions like for example
int n;
float num[100], sum = 0.0, avg;
avg = sum / n;
//...
I'm new to C, but i have previously coded in C++ and C#. I have written this code as an assignment, but the float operations don't work properly. What it's supposed to do is, by entering two positive integers, n and m, the end result should be this a sum of a sum with n and the square root of a multiplication.
My problem is that, even though the first sum works, both the multiplication and the square root (and in the end the final sum) don't work. In the end, whatever two numbers n and m i write, the sum will be ok and the other two will be completely innaccurate - either 1, both the multiplication and the final sum, or something that makes no sense (to be precise, "1.#INF00").
This is the code i have written. Does anybody know what i did wrong, or how can I fix this?
float sum(int n)
{
float s = 0;
for(float i = 1; i<=(float)n; i++)
{
s += (2*i)/(3*i*i+4);
}
return s;
}
float multiplication(int m)
{
float p = 1;
for(float j = 1; j <= (float)m; j++)
{
p *= (float)(j*j+1);
}
return p;
}
int main()
{
int n;
int m;
scanf("%i", &n);
scanf("%i", &m);
float s = sum(&n);
float p = multiplication(&m);
float e = s + (float)sqrt(p);
printf("The sum is %f \n", s);
printf("The multiplication is %f \n", p);
printf("The final expression is %f \n", e);
getch();
return 0;
}
You should pass the integer values, not pointers, to the functions sum and multiplication.
float sum(int n)
{
float s = 0;
for(float i = 1; i<=(float)n; i++)
{
s += (2*i)/(3*i*i+4);
}
return s;
}
float multiplication(int m)
{
float p = 1;
for(float j = 1; j <= (float)m; j++)
{
p *= (float)(j*j+1);
}
return p;
}
int main()
{
int n;
int m;
scanf("%i", &n);
scanf("%i", &m);
float s = sum(n); /* pass an integer, not a pointer */
float p = multiplication(m); /* pass an integer, not a pointer */
float e = s + (float)sqrt(p);
printf("The sum is %f \n", s);
printf("The multiplication is %f \n", p);
printf("The final expression is %f \n", e);
getch();
return 0;
}
I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}
I have to calculate the arithmetic and geometrical mean of numbers entered by the user in C language. The algorithm works fine, but I don't know how to do the enter numbers until 0 is pressed part. I have tried many things but nothing works. Here is what I have tried to do until now. Thanks for the help.
int main() {
int n, i, m, j, arr[50], sum = 0, prod = 1;
printf("Enter numbers until you press number 0:");
scanf("%d",&n);
while (n != 0) {
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum = sum + arr[i];
prod = prod * arr[i];
}
}
int armean = sum / n;
float geomean = pow(prod, (float)1 / n);
printf("Arithmetic Mean = %d\n", armean);
printf("Geometric Mean = %f\n", geomean);
getch();
}
Your code is asking for the number of values in advance and subsequently reading that many values. That's not what you were asked to do.
You need to ask for numbers in a loop and exit the loop when the number that you read is 0. You don't even need an array:
int n = 0, i, m, j, sum=0, prod=1;
while (1) {
int value;
scanf("%d",&value);
if (value == 0) {
break;
}
sum=sum+value;
prod=prod*value;
n++;
}
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
You have to break the for loop when value 0 entered; so you should check for arr[i].
While loop is not required.
Please go through below code; this could be help full:
#include <stdio.h>
int main()
{
int n, i, m, j, arr[50], sum=0, prod=1;
printf("Enter numbers until you press number 0:");
for(i=0; i<50; i++)
{
scanf("%d",&arr[i]);
if (arr[i] == 0)
{
break;
}
sum=sum+arr[i];
prod=prod*arr[i];
}
printf ("%d %d\n",sum, prod);
n = i+1;
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
getch();
return 0;
}
what dbush said is right, you don't need array and are not asking the number in advance but what he did not tell is how can you find the number of values
int main()
{
int n, sum=0, prod=1, num;
printf("Enter numbers until you press number 0:\n");
for(n=0; ; n++)
{
scanf("%d",&num);
if(num==0)
break;
sum=sum+num;
prod=prod*num;
}
printf("sum is %d \n",sum);
printf("prod is %d \n",prod);
printf("n is %d \n",n);
float armean=sum/n; //why int?
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
//getch(); why getch(), you are not using turboc are you?
}
There is no need for an array, but you should test if the number entered in 0 after reading it from the user. It would be better also to use floating point arithmetic to avoid arithmetic overflow, which would occur quickly on the product of values.
In any case, you must include <math.h> for pow to be correctly defined, you should test the return value of scanf() and avoid dividing by 0 if no numbers were entered before 0.
#include <stdio.h>
#include <math.h>
int main() {
int n = 0;
double value, sum = 0, product = 1;
printf("Enter numbers, end with 0: ");
while (scanf("%lf", &value) == 1 && value != 0) {
sum += value;
product *= value;
n++;
}
if (n > 0) {
printf("Arithmetic mean = %g\n", sum / n);
printf("Geometric mean = %g\n", pow(product, 1.0 / n));
getch();
}
return 0;
}
this code is an Integral program that calculate functions f(x)
and here .. the function is f(x)= x^2
int main()
{
float integral;
float a=0,b=0;
int n=1024;
float h;
float x;
int i;
float f(float x);
printf("Enter a, b \n");
scanf("%f %f" , &a, &b);
printf("Enter n\n");
scanf("%d" , &n);
h=(b-a)/n;
integral = (f(a)+f(b))/2;
x=a;
for (i = 0; i <= n-1; n++)
{
x = x + h;
integral = integral + f(x);
}
integral = integral * h ;
printf("with n = %d trapezoids, our esrimate\n", n );
printf("of the integral from %f to %f = %f\n", a , b , integral);
system("pause");
return 0;
}
float f(float x)
{
return x*x;
}
the output of n always garbage .. i don't know why
where is my wrong ??
the question is stupid
bu i really tired looking for the wrong
Here's your error: for (i = 0; i <= n-1; n++). You increment n instead of i.
The bug lies here, probably typo:
for (i = 0; i <= n-1; n++)
By the way there is another typo in the program: esrimate should be estimate :)
you have to clean the keyboard memory
fflush
printf("Enter a, b \n");
scanf("%f %f" , &a, &b);
fflush(stdin);
printf("Enter n\n");
scanf("%d" , &n);