Input are not able to calculate and print out in C - c

I've tried to build a simple calculator for physics force experiments.
//
// main.c
#include <stdio.h>
int main() {
char name[20];
int month,date;
int difference_percentage1, difference_percentage2, difference_percentage3;
double force1, force2, force3;
scanf("%s", name);
scanf("%d %d", &month, &date);
scanf("%lf %lf %lf", &force1, &force2, &force3);
double Favg=(force1 + force2 + force3)/3.000;
puts(name);
difference_percentage1=100*(force1-Favg)/Favg;
difference_percentage2=100*(force2-Favg)/Favg;
difference_percentage3=100*(force3-Favg)/Favg;
printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
getchar();
return 0;
}
The calculate doesn't match what I've typed for scanf().

The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.
Edit: one more reason to post text instead of a screenshot of the code! (o;
Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"

Related

A basic error using loops and functions in C

For some reason my code will consistently print out zeros.
I was supposed to make a code in which I enter three numbers,
The first number
The ratio
The amount of numbers to be displayed
The code should display those numbers.
Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void forloop(int firstNum,int ratio,int repeats);
int main(void)
{
int firstNum = 0;
int ratio = 0;
int repeats = 0;
printf("First number of the series: ");
scanf("%d", &firstNum);
printf("the ratio of the series: ");
scanf("%d", &ratio);
printf("the amount of numbers to display is ");
scanf("%d", &repeats);
forloop(firstNum,ratio,repeats);
}
void forloop(int firstNum,int ratio,int repeats)
{
int i = 0;
for (i=1; i!=repeats+1;i++)
{
printf("%d ", firstNum*(pow( ratio, i)));
}
}
This should fix the issue with printing zeros:
void forloop(int firstNum,int ratio,int repeats)
{
int i = 0;
for (i=1; i!=repeats+1;i++)
{
printf("%.0f ", firstNum*(pow((double)ratio,(double)i)));
}
}
As mentioned by #JonathanLeffler in the comments, you have a bug. The line
printf("%d ", firstNum*(pow( ratio, i)));
is wrong because pow returns a double but in the printf specifications %d expects an integer.
So, you can either change the specifier in your printf to a floating point one, i.e., %f, %g, %e, %F, %G, %E et cetera or cast pow's output to int by doing (int)pow(ratio, i).
In the first case, as #JadMrad said, if you are printing a double and you don't want to have decimal numbers, you can specify the number of digits at the right of the decimal point using "%.Nf" instead of "%f" where N is the number of decimal points that you want.
Nevertheless, in your case it seems to me that you are expecting always integer numbers. Then,
printf("%d ", firstNum*((int)pow( ratio, i)));
sounds like a better solution to me.

C not reading values from input correctly

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

User enter 5 characters and averge sums out the 5 numbers in C

I been having issues having my C code work. I have 1 warning, which states Warning: too many arguments for format. I am a beginner in C so I haven't encountered this issue yet. Any ideas on how to fix it and I cannot use conditions as I am in the beginning segment of my course learning from the start. I just need to know what I did wrong so I can fix the issue. Here's the code below:
#include <stdio.h>
int main() {
float firstNumber, secondNumber, thirdNumber;
float fourthNumber, fifthNumber;
float sumAverage1 = (firstNumber+secondNumber+thirdNumber);
float sumAverage2 = (fourthNumber+fifthNumber);
long a = 1000000000;
long b = 1250000000;
long c = 1500000000;
long d = 1750000000;
long e = 2000000000;
printf("A is %li\n", a);
printf("B is %li\n", b);
printf("C is %li\n", c);
printf("D is %li\n", d);
printf("E is %li\n", e);
printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
printf("You cannot enter a decimal integer and enter numbers below 100.\n");
scanf("%f", &firstNumber);
scanf("%f",&secondNumber);
scanf("%f",&thirdNumber);
scanf("%f",&fourthNumber);
scanf("%f",&fifthNumber);
printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);
system("pause");
return 0;
}
The line:
printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);
Has an argument but no format specifier. Also, that expression is unparenthesized; the division has higher precedence than the addition, so what you're calculating is sumAverage1+(sumAverage2/5), which is integer division, which is probably not what you want.
What you probably want is:
printf("Your numbers average out to: %f\n", (double)(sumAverage1+sumAverage2)/5.0);
Here this will solve all your problem.
#include <stdio.h>
#include <stdlib.h>
int main() {
float firstNumber, secondNumber, thirdNumber,sumAverage1;
float fourthNumber, fifthNumber,sumAverage2;
long a = 1000000000;
long b = 1250000000;
long c = 1500000000;
long d = 1750000000;
long e = 2000000000;
printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
printf("You cannot enter a decimal integer and enter numbers below 100.\n");
scanf("%f",&firstNumber);
scanf("%f",&secondNumber);
scanf("%f",&thirdNumber);
scanf("%f",&fourthNumber);
scanf("%f",&fifthNumber);
sumAverage1 = (firstNumber+secondNumber+thirdNumber);
sumAverage2 = (fourthNumber+fifthNumber);
printf("A is %li\n", a);
printf("B is %li\n", b);
printf("C is %li\n", c);
printf("D is %li\n", d);
printf("E is %li\n", e);
printf("Your numbers average out to:%f\n", (sumAverage1+sumAverage2)/5);
system("pause");
return 0;
}
I ran this on my Visual Studio and it works just fine. Hope it Solves your Problem.
You need to change the printf format specifier, but your scanf does not capture the extra dangling newline. You need to either clear the buffer, fflush(stdin) after each scanf() or you need an extra scanf("%c") to get rid of the newline character.
See scanf() leaves the new line char in buffer?

How to print variables in a structure?

#include <stdio.h>
#include <math.h>
#define G 9.81
typedef struct
{
double weight;
double drag;
double time;
} USER_INPUT;
double calculateVelocity(USER_INPUT);
int main(int argc, char **argv)
{
USER_INPUT userInput;
double velocity;
printf("Please enter weight, drag and time: ");
scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time);
velocity = calculateVelocity(userInput);
printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity);
return 0;
}
double calculateVelocity(USER_INPUT data)
{
double velocity;
// TODO compute velocity
return velocity;
}
In the main function, I want to display the result.
How can I print variables defined in the structure?
I tried %f, which returns 0.000000, and %d returns a random number.
You are doing mistake while printing,
Please note '&' is used to get the address of any variable, not the value.So when you are printing:
printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity);
you are actually printing the adderss of variables:
&userInput.time (address of (userInput.time)),
&userInput.weight(address of (userInput.weight)), &userInput.drag (address of (userInput.drag)).
You want to print their values, not address, hence remove '&' while printing:
ie;
printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity);
Well not sure about all the physics or which equation so I kind of faked that, but I got your program to compile and run so it should be pretty easy for you to fix it from here. Feel free to ask questions about the pointer stuff and why addresses were and weren't used in different places.
#include <stdio.h>
#include <math.h>
#define G 9.81
#define P 1.00 // ?
#define A 1.00 // ?
typedef struct {
double weight;
double drag;
double time;
} userInput_t;
double calculateVelocity(userInput_t *); // Terminal velocity?
double calculateVelocity(userInput_t *data) {
return sqrt((2 * data->weight * G)/(P * A * data->drag));
}
int main(void) {
userInput_t userInput;
printf("Please enter weight, drag and time: ");
scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time);
double velocity = calculateVelocity(&userInput);
printf("\nAt t = %f, the parachutist with weight %f kg\n"
"and a drag coefficient %f kg/s\n"
"will have a velocity of %f m/s^2\n",
userInput.time, userInput.weight, userInput.drag, velocity);
}
int printf(const char *format, ...);
is NOT
int scanf(const char *format, ...);
scanf works with memory addresses of variables, while printf with variables itselves.
It's UB trying to
printf ("%d", memory_addres_of_variable);
because the right way to print memory addresses is with %zu starting from C99 and later, and %lu with ANSI C 90.
That's why you aree seeing a random number.
The issue is not related to struct. You are passing addresses of variables instead of their values to printf. So the solution is simple: Remove the & before variable names in the printf call.
This is a common mistake: scanf needs the addresses of variables to be able to alter their values, while printf just takes the values. Unfortunately even the prototypes do not make it evident:
int printf(const char *format, ...);
int scanf(const char *format, ...);
And you need to use %f for double variables (derived from the type called float), %d is used for int (decimal).
The result:
printf("At t = %f , the parachutist with weight %f kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity);
References:
http://en.cppreference.com/w/cpp/io/c/fprintf
http://en.cppreference.com/w/c/io/fscanf

Reading Scientific Notation in C

I am trying to read a file that has the following contents:
1.0000000e+01 2.9265380e+03 5.0821200e+02 4.3231640e+01
2.0000000e+01 1.0170240e+04 9.2798610e+02 4.0723180e+01
3.0000000e+01 2.1486260e+04 1.1832420e+03 1.0328000e+01
4.0000000e+01 3.3835080e+04 1.1882285e+03 -9.3307000e+00
5.0000000e+01 4.5250830e+04 1.0899705e+03 -1.0320900e+01
6.0000000e+01 5.5634490e+04 9.8935650e+02 -9.8019000e+00
7.0000000e+01 6.5037960e+04 8.9134700e+02 -9.8000000e+00
but I can't seem to find a proper way to read the scientific notation. Here is what I have of the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// This is the array to store the input
double time[24], altitude[24], velocity[24], acceleration[24];
double var1, var2, var3, var4;
//This is the pointer declaration for opening a file
FILE * fp = fopen("rocket.txt", "r");
int i = 0;
while(fscanf(fp,"%g %f %f %f", &var1, &var2, &var3, &var4) > 0){
time[i] = var1;
altitude[i] = var2;
velocity[i] = var3;
acceleration[i] = var4;
printf("Time: %f \n", &time[i]);
i++;
}
printf("Time: %f", &time[0]);
fclose(fp);
return(0);
}
I've tried multiple combinations of %f, %g, %d to try and print out the result but I can never get the right thing.
If anyone can point me on the right direction I will greatly appreciate it.
What you want to use is %lf for input and %e for output in scientific notation:
scanf("%lf", &input);
printf("%e\n", input);
You could use a, e, f, or g in the conversion specifier like this:
fscanf(fp, "%a", &input); // NOTE: only with C99 compilers
fscanf(fp, "%e", &input);
fscanf(fp, "%f", &input);
fscanf(fp, "%g", &input);
They will all work for parsing floats, but for doubles you will need to use the length modifier "l" like this:
fscanf(fp, "%le", &input);
To print the values, you can use any of the specifiers but you don't need the length modifier "l":
printf("%e ", input); // or f or g (or a C99 compilers only)
printf("%le ", input); // produces the same thing
A really helpful reference is here:
http://en.cppreference.com/w/c/io/fscanf

Resources