Value assigned to s variable when uses %d instead %f [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I assume output to be 8 but the line s=a+b assign value 5 to s variable not sum of a+b. I know that I am using %d instead of %f.
#include <stdio.h>
void summ(int, int);
int main(){
int a = 3, b = 5;
summ(a, b);
return 0;
}
void summ(int a, int b){
float s;
s = a + b;
printf("%d", s);
}

You can't use any old format specifier in a printf(). It always has to match the type of the variable you are passing in. If you want to print a value as another type, you have to convert it first, then print the converted value. So either this has to be:
void summ(int a, int b)
{
float s;
s = a + b;
printf( "%f\n", s );
}
or
void summ(int a, int b)
{
int s;
s = a + b;
printf( "%d\n", s );
}
In the case of your sum function, you won't see a noticeable difference (except that %d does not print a ".0000" after the number, but you could have suppressed that by adding a length of 0 to the fraction of your %f by writing it as %.0f).
OTOH, if you have a division:
void divv(int a, int b)
{
float s;
s = a / b;
printf( "%f\n", s );
}
the CPU will perform a (often faster) integer division. That means that 3 / 5 (which would be 0.6) just gives you 0 because it didn't bother calculating the fraction. The compiler doesn't look at the type of variable an expression is assigned to, so it doesn't see that it could actually store the fraction. It just looks at the operands of the operator, which are both int, calculates an int, and expands it into a float. So if you wanted a precise result, you'd have to do:
void divv(int a, int b)
{
float s;
s = (float)a / (float)b;
printf( "%f\n", s );
}
because as soon as one of the arguments is a float, it will perform a floating point division and also calculate the fraction.

here is a possible version of the code that cleanly compiles and performs the desired functionality.
#include <stdio.h>
void summ(int, int);
int main( void )
{
int a = 3;
int b = 5;
summ(a, b);
return 0;
} // end function: main
void summ(int a, int b)
{
float s;
s = (float)(a + b);
//printf("%d", s);
printf( "%f\n", s );
} // end function: summ
The output from the above code is:
8.000000

Related

How come does the c program print the number "2" please help explained? [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 1 year ago.
Improve this question
#include <stdio.h>
int fun1(int a, int b) {
a = a - 3;
b = a / 2;
return b;
}
int main() {
int a = 10;
int b = 17;
int c = fun1(b, a);
int d = fun1(c, b);
printf("%d", d);
}
the code runs I am just trying to figure out what it prints out and why.
In this function
int fun1(int a, int b){
a= a-3;
b=a/2;
return b;
}
the value of the parameter b is not used. So in fact the function may be rewritten like
int fun1(int a, int b){
a = a-3;
return a / 2;
}
or
int fun1(int a, int b){
return ( a - 3 ) / 2;
}
(though the compiler can issue a message that the parameter b is not used,)
In the first call
int c=fun1(b,a);
the parameter a of the function is initialized by the argument b that has the value 17
int b = 17;
So the return value of the function is calculated like ( 17 - 3 ) / 2 and is equal to 7.
In the second call of the function
int d=fun1(c,b);
the function parameter a is initialized by the argument c that has the value 7 due to the previous call of the function.
So the returned value of the function is calculated like ( 7 - 3 ) / 2 and is equal to 2. This value is assigned to the variable d that is outputted.

how can I fix this code so that I can calculate 2 fraction and return the sum as one floating point number? [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 2 years ago.
Improve this question
double fracsum (int a, int b, int c, int d){
float sum = 0;
int i;
for (i = 0; i < a; i++) {
sum += a;
}
return sum;
}
int main(void)
{
printf("%.3f %.3f %.3f\n",
fracsum(1,2,2,4),
fracsum(1,4,1,8),
fracsum(4,3,5,6));
return 0;
}
Did you mean:
float fracsum (float a, float b, float c, float d) {
return (a / b + c / d);
}
However the problem was maybe that you cannot divide int variables, you have to use float as argument type..
It's not clear to me exactly what you want. But I'd take a wild-elbow guess
that it's fracsum=(a/b)+(c/d). And if that's indeed what you want, then
double fracsum (int a, int b, int c, int d) {
return ( ((double)a)/((double)b) + ((double)c)/((double)d) ); }
Couldn't be much easier.

finding output to a simple but tricky program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
#include<stdio.h>
int main()
{
float a=5,b=2;
int c,d;
c=a%b;
d=a/2;
printf("%d\n",d);
return 0;
}
The program is incorrect because the operator % is not defined for float numbers.
From the C Standard (6.5.5 Multiplicative operators)
2 Each of the operands shall have arithmetic type. The operands of
the % operator shall have integer type.
As for this statement
d=a/2;
then there is a conversion from the expression with the float type a /2 to the type of the left-hand side operand that has the type int. So the value of d will be equal to 2.
Pehaps you mean a program similar to the following program
#include <stdio.h>
int main(void)
{
int a = 5, b = 2;
int c, d;
c = a % b;
d = a / b;
printf( "d = %d, c = %d\n", d, c );
return 0;
}
In this case the program output is
d = 2, c = 1
Instead of the two variables c and d and two expressions with the operators / and % you could use the standard function div declared in the header <stdlib.h>. For example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 5, b = 2;
div_t result = div( a, b );
printf( "quotient = %d, remainder = %d\n", result.quot, result.rem );
return 0;
}
The program output is
quotient = 2, remainder = 1

Why is my code not printing answer in float? [duplicate]

This question already has answers here:
Why is this simple piece of code not working?
(5 answers)
Closed 5 years ago.
#include <stdio.h>
float div ( int a,int b, int c, float x );
int main()
{
int a,b,c,x;
a=250;
b=85;
c=25;
x=div (a,b,c,x);
printf("%d ", x);
}
// your code goes here
float div (int a,int b, int c, float x)
{
x=((a-b)/c);
return(x);
}
Because x is declared as int instead of float.
Your code:
int x;
...
printf("%d ", x);
What you need instead:
float x;
...
printf("%f ", x);
And your div function is wrong too:
float div (int a,int b, int c, float x)
{
x=((a-b)/c); // << this will perform an integer division
return(x); // and therefore x will be truncated, even if it is a float
}
You need this:
float div (int a,int b, int c, float x)
{
x = ( ((float)(a-b) ) / c);
return x;
}
The (float) before (a-b) is called a cast and will ensure that (a-b) will be treaded as a float number during the division by c.
BTW the function can be simplified, you don't need the x parameter:
float div (int a,int b, int c)
{
float x = ( ((float)(a-b) ) / c);
return x;
}
or even simpler:
float div (int a,int b, int c)
{
return ((float)(a-b) ) / c;
}
Look closely at the statement
x=((a-b)/c);
Note the operands, they are all integers, so the integer division is performed and then, the result is converted to floating type upon assignment. You don't want that.
In case you want a floating point division, you need to make sure one of the operands are of type float/ double.
You can use a cast, like
x=(((float)a-b)/c);
to force floating point arithmetic.
That said, in the main(), you MUST use proper conversion specifier for float, %f.
prints out an integer:
printf("%d ", x);
prints out an floating point number:
printf("%f", x);
also x must be from type float

C File Input/Trapezoid Rule Program

Little bit of a 2 parter. First of all im trying to do this in all c. First of all I'll go ahead and post my program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <string.h>
double f(double x);
void Trap(double a, double b, int n, double* integral_p);
int main(int argc, char* argv[]) {
double integral=0.0; //Integral Result
double a=6, b=10; //Left and Right Points
int n; //Number of Trapezoids (Higher=more accurate)
int degree;
if (argc != 3) {
printf("Error: Invalid Command Line arguements, format:./trapezoid N filename");
exit(0);
}
n = atoi(argv[2]);
FILE *fp = fopen( argv[1], "r" );
# pragma omp parallel
Trap(a, b, n, &integral);
printf("With n = %d trapezoids....\n", n);
printf("of the integral from %f to %f = %.15e\n",a, b, integral);
return 0;
}
double f(double x) {
double return_val;
return_val = pow(3.0*x,5)+pow(2.5*x,4)+pow(-1.5*x,3)+pow(0*x,2)+pow(1.7*x,1)+4;
return return_val;
}
void Trap(double a, double b, int n, double* integral_p) {
double h, x, my_integral;
double local_a, local_b;
int i, local_n;
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
h = (b-a)/n;
local_n = n/thread_count;
local_a = a + my_rank*local_n*h;
local_b = local_a + local_n*h;
my_integral = (f(local_a) + f(local_b))/2.0;
for (i = 1; i <= local_n-1; i++) {
x = local_a + i*h;
my_integral += f(x);
}
my_integral = my_integral*h;
# pragma omp critical
*integral_p += my_integral;
}
As you can see, it calculates trapezoidal rule given an interval.
First of all it DOES work, if you hardcode the values and the function. But I need to read from a file in the format of
5
3.0 2.5 -1.5 0.0 1.7 4.0
6 10
Which means:
It is of degree 5 (no more than 50 ever)
3.0x^5 +2.5x^4 −1.5x^3 +1.7x+4 is the polynomial (we skip ^2 since it's 0)
and the Interval is from 6 to 10
My main concern is the f(x) function which I have hardcoded. I have NO IDEA how to make it take up to 50 besides literally typing out 50 POWS and reading in the values to see what they could be.......Anyone else have any ideas perhaps?
Also what would be the best way to read in the file? fgetc? Im not really sure when it comes to reading in C input (especially since everything i read in is an INT, is there some way to convert them?)
For a large degree polynomial, would something like this work?
double f(double x, double coeff[], int nCoeff)
{
double return_val = 0.0;
int exponent = nCoeff-1;
int i;
for(i=0; i<nCoeff-1; ++i, --exponent)
{
return_val = pow(coeff[i]*x, exponent) + return_val;
}
/* add on the final constant, 4, in our example */
return return_val + coeff[nCoeff-1];
}
In your example, you would call it like:
sampleCall()
{
double coefficients[] = {3.0, 2.5, -1.5, 0, 1.7, 4};
/* This expresses 3x^5 + 2.5x^4 + (-1.5x)^3 + 0x^2 + 1.7x + 4 */
my_integral = f(x, coefficients, 6);
}
By passing an array of coefficients (the exponents are assumed), you don't have to deal with variadic arguments. The hardest part is constructing the array, and that is pretty simple.
It should go without saying, if you put the coefficients array and number-of-coefficients into global variables, then the signature of f(x) doesn't need to change:
double f(double x)
{
// access glbl_coeff and glbl_NumOfCoeffs, instead of parameters
}
For you f() function consider making it variadic (varargs is another name)
http://www.gnu.org/s/libc/manual/html_node/Variadic-Functions.html
This way you could pass the function 1 arg telling it how many "pows" you want, with each susequent argument being a double value. Is this what you are asking for with the f() function part of your question?

Resources