C not reading values from input correctly - c

I am very new to C, and while working on a project which requires pulling an indeterminate amount of values from the console, I am finding that it is not pulling the correct values. It seems like addresses, which I believe means it is a pointer issue, but I can't seem to find it.
int getVals(int degree){
double sum;
double x;
double coefs[degree];
for(int counter = 0; counter<=degree; counter = counter+1){
double nxt;
scanf(" %d", &nxt);
coefs[counter] = nxt;
printf("coefs[%d] = %d\n", counter, coefs[counter]);
}
printf(" x ? ");
scanf(" %d", &x);
printf("degree %d x %d\n", degree, x);
sum = poly(x, degree, coefs);
printf ("polynomial evaluate to: %lf\n", sum);
int newDegree;
scanf(" %d", &newDegree);
degree = newDegree;
if(degree>-1){
getVals(degree);
}
else
return degree;
}
Note: poly returns a double result of the evaluated polynomial
I am getting the following infinite loop after entering a degree of 1 and a coefficient of 1.5. It does not allow me to enter an x.
Infinite loop

In scanf(" %d", &newDegree); you should use the "%lf" format specifier (since your values is a double, not an int). Change the format specifier in all your calls to scanf() and "%f" in calls to printf().
Please refer to the documentation at this links printf(3), scanf(3).

Related

Complex numbers through scanf in C

I have problem with following fragment of code:
int main()
{
int n = 3;
complex_t t[3];
for(int i = 0; i < n; i++)
{
double x, y;
printf("Enter complex number: ");
scanf("%f %f", &x, &y);
t[i].re = x;
t[i].im = y;
}
}
When I am trying to pass x, and y to the program, it doesn't change value, and is 0.00000.
Can you help me?
You've declared x and y as doubles, but then went on to use the format specifier for floats (%f). So the end result is reading them in as if they were regular floats, then jamming the result into a double leaving you with unexpected values when trying to actually use them.
You need to use the format specifier specifically for doubles (%lf) here.
scanf("%lf %lf", &x, &y);
See format string specifications for more information about different format specifiers.

what does the variabel in printf do

Why didn't this code work after adding "res=pow(arr[i],x)"
I want it to print like this
"printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));"
The code doesn't work untill i print like this
"printf("%d * %d = %d \n",i+1,x,res)));"
#include<stdio.h>
#include<math.h>
int main()
{
int arr[5];
for(int i=0 ; i<5 ; i++){
printf("enter the numbers %d\n",i+1);
scanf("%d",&arr[i]);
}
int x;
printf("what is power would you like...\n");
scanf(" %d",&x);
printf("The power of the array elements is...\n");
for(int i=0 ; i<5 ; i++){
printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));
}
return 0; // 1*2=1*1 , 3*2=3*3
}
pow returns a double. You need %lf format for the third argument or you'll get undefined behaviour trying to format a floating point value with an integer %d format (most compilers issue a warning about this BTW).
quickfix
printf("%d * %d = %lf \n",i+1,x,pow(arr[i],x));
Assigning the result to an integer workarounds the issue. That's why it works then (but sometimes it leads to rounding errors so beware!)
You may have a look at integer power algorithms instead. pow is more suited for floating point operations.

Unexpected behavior after scanf() in do..while loop

Hi I am studying C language by myself.
My question is, is there something I missed which need to know when working with scanf() that takes char value?
To practice do...while loop, I wrote some code like below but it did not work as I expected.
#include <stdio.h>
int main()
{
char y_or_n;
int x =1;
int y;
do
{
printf("ENTER A NUMBER\n");
scanf("%d", &y);
printf("THE NUMBER WILL BE ADDED TO x WHICH IS %d\n", x);
x = x+y;
printf("x TURNED INTO %d\n", x);
printf("KEEP DOING THIS?(y/n)\n");
scanf(" %s", &y_or_n);
printf("x is %d\n", x);
}
while(y_or_n =='y');
printf("GOOD BYE\n");
return 0;
}
For the first loop, it worked as I expected.
For example, when I entered 7, x turned into 8. But after scanf() was executed, value of x was changed into 0.
So from second loop, value of x changed temporarily into value of y and changed again into 0.
I guessed that there is something wrong with scanf() function and modified the code slightly: changed type of y_or_n into integer so that scanf() takes integer value.
The modified code is like below
include <stdio.h>
int main()
{
int y_or_n;
int x =1;
int y;
do
{
printf("ENTER A NUMBER\n");
scanf("%d", &y);
printf("THE NUMBER WILL BE ADDED TO x WHICH IS %d\n", x);
x = x+y;
printf("x TURNED INTO %d\n", x);
printf("KEEP DOING THIS?(y/n)\n");
scanf(" %d", &y_or_n);
printf("x is %d\n", x);
}
while(y_or_n ==1);
printf("GOOD BYE\n");
return 0;
}
This time the code worked as I expected.
Value of x was not changed into 0 even after an execution of scanf() and every time I entered a number that number was added to x.
If my question is not clear, please let me know.
Thank you for reading.
%s is for reading null-terminated strings, so passing pointer to one-byte buffer for that is bad.
It seems the variable x is placed just after the variable y_or_n on the memory and writing of terminating null-character by scanf() is setting value of x to 0.
To read one character, use %c instead.
char y_or_n;
/* ... */
scanf(" %c", &y_or_n);

C language pointer

Write a program that uses two pointer variables to read two double numbers and display the absolute value of their sum?
this is my code and I don not know where it gets wrong:
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%f %f", p1,p2);
if(*p1+*p2 >= 0)
printf("%f\n", *p1+*p2);
else
printf("%f\n", -(*p1+*p2));
return 0;
}
if you have to scan doubles you use "lf" and to print them as well. It was your only mistake. "f" it is just for floats.
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%lf %lf", p1,p2);
if(*p1+*p2 >= 0)
printf("%lf\n", *p1+*p2);
else
printf("%lf\n", -(*p1+*p2));
return 0;
}
http://www.cplusplus.com/reference/cstdio/scanf/
Please consult this website or a similar one for errors or warnings.
%f is used for float values, and c compilers often issue warnings or stop compilation when type conversions occur without specifying them.
%lf is used for doubles.

Squared Equation calculator not working in C

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

Resources