Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I only get the same number over and over again.

What I have tried:

C
// yprog.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ERROR 0
#define MAX_INPUT_LINE 80
#define SUCCESS 1

int main(int argc, char **argv)
{
	double mag = 0;
	double maxy = 0;
	double miny = 0;

	FILE *filein;
	char filein_name[MAX_INPUT_LINE];
	FILE *fileout;
	char fileout_name[MAX_INPUT_LINE];
	char input[MAX_INPUT_LINE];
	if (argc < 2)
	{
		printf("\n");
		printf("Yprog:\n");
		printf("  Enter the input file name :\n");

		if (fgets(input, MAX_INPUT_LINE, stdin) != NULL)
		{
			sscanf(input, "%s", filein_name);
		}
		else
		{
			printf("\n");
			printf("Yprog - Fatal error!\n");
			printf("  Input error!\n");
			return ERROR;
		}

		printf("\n");
		printf("Yprog:\n");
		printf("  Enter the output file name:\n");

		if (fgets(input, MAX_INPUT_LINE, stdin) != NULL)
		{
			sscanf(input, "%s", fileout_name);
		}
		else
		{
			printf("\n");
			printf("Yprog - Fatal error!\n");
			printf("  Input error!\n");
			return ERROR;
		}

		fileout = fopen(fileout_name, "w");

		if (fileout == NULL)
		{
			printf("\n");
			printf("Yprog - Fatal error!\n");
			printf("  Cannot open the output file %s.\n", fileout_name);
			return ERROR;
		}
	}
	else
	{
		strcpy(filein_name, argv[1]);
		fileout = stdout;
	}

	filein = fopen(filein_name, "r");

	if (filein == NULL)
	{
		printf("\n");
		printf("Yprog - Fatal error!\n");
		printf("  Cannot open the input file %s.\n", filein_name);
		return ERROR;
	}

	printf("Enter maximum value from file for y : ");
	scanf("%lg", &maxy);
	printf("Enter minimum value from file for y : ");
	scanf("%lg", &miny);

	// todo?

	{
		int n, c;
		double sum = 0, var;

		printf("Enter the number of sample values in the file minus 1.\n");	// 11025
		scanf("%d", &n);

		printf("using %d sample values.\n", n + 1);

		fseek(filein, 0, SEEK_SET);
		for (c = 0; c <= n; c = c + 1)
		{
			fscanf(filein, "%lg", &var);

			sum = var * (maxy / 65536.0);
			// todo?

			// todo?
			fprintf(fileout, "%lg\n", &sum);
		}
		getchar();
		fclose(filein);
		fclose(fileout);
		printf("Yprog: \n  File converted.\n");
		return 0;
	}

	return 0;
}
Posted
Updated 28-Aug-23 8:00am
v3
Comments
Richard MacCutchan 28-Aug-23 5:55am    
What is contained in the input file, what are the values you enter, and what results do you see?
Richard C. Andrews 28-Aug-23 5:59am    
It's just a long list of numbers gathered from an ADC with values ranging from +10 to -10.
The values printed (which is incorrect) have exponents too large to be reasonable.
Richard C. Andrews 28-Aug-23 6:01am    
The goal is to squash the values in the file to a range of 1 to -1.
Richard MacCutchan 28-Aug-23 6:33am    
Sorry but it's impossible to guess what the problem is. You need to use the debugger and check the results of each calculation.
Richard MacCutchan 28-Aug-23 6:56am    
I have maxy = 10.0 and used the range -5 to +5. Using your calculations I get the following results:
val: -5, sum: -0.000762939
val: -4, sum: -0.000610352
val: -3, sum: -0.000457764
val: -2, sum: -0.000305176
val: -1, sum: -0.000152588
val: 0, sum: 0
val: 1, sum: 0.000152588
val: 2, sum: 0.000305176
val: 3, sum: 0.000457764
val: 4, sum: 0.000610352
val: 5, sum: 0.000762939

Which all look resonable.

1 solution

Your issue is with your loop calculating and printing the same value because of how the value is printed using 'fprintf'. The format specifier '%lg' is used for reading a double value with 'fscanf', but when you print with 'fprintf', you should just use the variable 'sum' without the '&' operator -
C
fprintf(fileout, "%lg\n", sum);


You are also trying to scale the 'var' value using 'maxy' and '65536.0', but you're assigning the scaled value directly to 'sum' in each iteration of your loop. This means that in each iteration, 'sum' is being overwritten with a new value. To accumulate the scaled values, you should add the scaled value to 'sum' instead of assigning it -
C
sum += var * (maxy / 65536.0);


Your loop should look like this -
C
for (c = 0; c <= n; c = c + 1)
{
    fscanf(filein, "%lg", &var);

    sum += var * (maxy / 65536.0);

    fprintf(fileout, "%lg\n", sum);
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 28-Aug-23 10:48am    
Well spotted, I certainly missed that.
Andre Oosthuizen 28-Aug-23 10:58am    
Thanks Richard
Richard C. Andrews 28-Aug-23 12:49pm    
Oh, thank you. it runs and I get expected results. Now I can progress with the code.
Andre Oosthuizen 29-Aug-23 4:06am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900