Reading Scientific Notation in C - 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

Related

Input are not able to calculate and print out in 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"

fscanf not reading/recognizing float numbers?

I'm trying to read from a file that has the format:
ID: x y z ...... other crap
The first line looks like this:
0: 0.82 1.4133 1.89 0.255 0.1563 armTexture.jpg 0.340 0.241 0.01389
I only need the x y z float numbers, the rest of the line is garbage.
My code currently looks like this:
int i;
char buffer[2];
float x, y, z;
FILE* vertFile = fopen(fileName, "r"); //open file
fscanf(vertFile, "%i", &i); //skips the ID number
fscanf(vertFile, "%[^f]", buffer); //skip anything that is not a float (skips the : and white space before xyz)
//get vert data
vert vertice = { 0, 0, 0 };
fscanf(vertFile, "%f", &x);
fscanf(vertFile, "%f", &y);
fscanf(vertFile, "%f", &z);
fclose(vertFile);
It's been changed a little for debugging (originally the first two scanfs used * to ignore the input).
When I run this, x, y, z do not change. If I make it
int result = fscanf(vertFile, "%f", &x);
result is 0, which I believe tells me it isn't recognizing the numbers as floats at all? I tried switching xyz to doubles and using %lf as well, but that didn't work either.
What could I be doing wrong?
%[^f] doesn't skip non-floats, it skips anything that's not the letter 'f'.
Try %*d: instead. * discards the number read, and a literal : tells it to skip over the colon. You can also combine all those individual reads.
fscanf(vertFile, "%*d: %f %f %f", &x, &y, &z);

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.

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

writing to a struct with a struct

I am reading from a txt file using fscanf.
for (int i = 0; i < totalLines; ++i)
{
fscanf(fp,"%s %s %f %f[^\n]", &cp[i].name, &cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
printf("Name: %s\nAnimal: %s\nLat: %.6f\nLong: %.6f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);
}
The problem is it does not set/print the innermost structs members or variables which are 2 doubles lng and lat are not being assigned. What the best way to make this work? am I on the right track? I tried to cast it with a (double) but that through errors. any ideas?
UPDATE
I have updated the code to how it should be and im still getting a 0 value on all. any ideas why?
Considering your structure is as follows:
/* Structure Holding Coordinates */
typedef struct location
{
double lat;
double longt;
}location_t;
typedef struct geo
{
char name[64];
char animal[64];
location_t loc;
} geo_t;
Your Reading is to be done as follows:
geo_t arr[NO_ENTRIES]; /* NO_ENTRIES is total lines in file */
for(i = 0; i< NO_ENTRIES; i++)
fscanf(fp, " %s %s %lf %lf", arr[i].name, arr[i].animal, &arr[i].loc.lat, &arr[i].loc.longt);
printf("After Reading\n\n\n");
for(i = 0; i< NO_ENTRIES; i++)
printf(" %s %s %f %f\n", arr[i].name, arr[i].animal, arr[i].loc.lat, arr[i].loc.longt);
See it here: http://ideone.com/99dmvL
For double, use %lf, just like what you did in the printf. So change the code to:
fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
As u are trying to read long double values u should use %lf instead of %s where u are scanning double values
example : fscanf(fp,"%lf",&double_value);
use %f instead %s for double values.
fscanf(fp,"%s %s %f %f", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
Also your printf will have an error. I suggest you do these:
printf("Name: %s\nAnimal: %s\nLat: %f\nLong: %f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);
Don't use "%lf". Your variables are doubles, so you should use "%f".
In your printf calls, if needed use a format specifier like this: "%.5f". It tells printf to output 5 digits after the decimal point.
You can also refer the fscanf & fprintf documentation.
You have to get the value using the floating point control string. Not a string control string.
fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);

Resources