always garbage in integer variable - c

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);

Related

project doesn't print the results

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;
}

Format '%f' expects argument of type 'double', but argument 2 has type 'float (*)(float *)'

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;
//...

Why don't float operations work properly?

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;
}

why do i get 0 instead the result of calculation x - y and x/y in C

In this code I thought I would get the result of calculation x / y and x - y but the program shows 0 for i and j. What is wrong?
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It is undefined behavior, when you call the calculate() function within the same printf and i as well as j are calculated within that printf (same function). By the way, it is not a good idea to use global variables ( i, j ) ... For test purpose only, you could calculate() before the next printf of i and j.
You can test that behavior with:
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f", calculate(a, b));
printf(" %f %f\n", i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It might be related to the parsing, reference, and execution order of the arguments in printf function. The printf function uses the arguments right-to-left direction. You can easily check the order through below codes.
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
//printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
printf("\nThe results are: %f %f %f\n", i, j, calculate(a, b));
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}

Why is my average not the correct answer? (C code program to find mean)

I am bashing my head because I cannot figure out why my C code keeps printing the wrong average of a set of n numbers!
This is my code below:
int main()
{
int i;
int n;
int sum = 0.0;
int lowest;
int highest;
float average;
int range;
int middle;
double median;
printf("\nEnter the amount of numbers you want?\n");
scanf("%d",&n);
int numbs[n];
int temp[n];
for(i = 0;i < n; i++)
{
printf("\nEnter a number from 0 to 15: ");
scanf("%d",&temp[i]);
}
while (temp[i] < 0 || temp[i] > 15) than 15
{
printf("This number is not from 0 to 15! Please re-enter another number: ");
scanf("%d",&temp[i]);
}
numbs[i] = temp[i];
sum += numbs[i];
}
int sortt = 0, j, x;
for (x = 1; x < n; x++) {
for (j = 0; j < n - x; j++) {
if (numbs[j] > numbs[j + 1]) {
sortt = numbs[j];
numbs[j] = numbs[j + 1];
numbs[j + 1] = sortt;
}
}
}
lowest = numbs[0];
highest = numbs[n-1];
middle = n/2;
if (n % 2)
{
median = numbs[middle];
}
else
{
median = (numbs[middle - 1] + numbs[middle]) / 2.0;
}
average = sum/n;
range = highest - lowest;
printf("\nSum: %d", sum);
printf("\nAverage: %.2f", average);
printf("\nMedian: %.2f", median);
printf("\nRange: %d\n", range);
return 0;
}
This is my input and output below. You can see that 8 divided by 3 is not 2, it is 2.67! I've tried using double and float.
Input & Output:
You need to correct the following line:
average = sum/n;
to
average = (float)sum/n;
You have to cast your return value into float. Think about it as a function with the following definition:
float divide(int x,int y){
return x/y; // returns an integer instead of float.
}
While this definition:
float divide(int x,int y){
return (float)x/y; // creates a temporary float variable and returns it immediately as the returned value of the function.
}
In addition, declaring int sum=0.0 is definitely going to show you a warning when compiling with -Wall. Try to follow warnings that you get from your compiler and fix all of them before you run your program.
8 divided by 3 is 2, remainder 2. 8 and 3 are integers, and when you divide two integers, you use integer division with integer rules.
Also, this line might be confusing you:
int sum = 0.0;
Since sum is an int, this just sets sum to zero.
And:
average = sum/n;
Since both sum and n are integers, this is integer division. What you do with a result does not affect how that result is computed -- C's rules are complex enough already.
/*Here see you can intake all values as float instead */
#include <stdio.h>
#include <stdlib.h>
void main()
{
float i,n,a,b,sum,ave;
printf("This is a program to calculate the average of 'n' numbers \n");
printf("Of How many numbers do you want to calculate average \n");
scanf("%f", &n);
printf("Enter the first number \n");
scanf("%f", &a);
sum = a;
for (i=1;i<n;i++)
{
printf("Enter another number \n");
scanf("%f", &b);
sum = sum + b;
}
ave = (sum/n);
printf("The average of the %f number is %f", n, ave);
getchar();
}

Resources