Jump to content

Read from print to Program

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Sov

Sov

    Newbie

  • Members
  • Pip
  • 4 posts
I'm in the process of writing a console program that reads from one text file and prints into the next after altering the info. The file being read from is arranged as follows:
2
54
5
12538
notice that the lines that are under the single digit lines have the same number of characters as the digit above it. (the actual file can have different numbers than the ones above)
The goal is to take in the longer strings of numbers and output a table with two rows, the first row representing the first multi digit row, the second representing both of the multi digit rows (for example, for the sum column, row one would display 9, and row 2 would display 28) The columns should display the categories of information, those being ( Total number of elements[2 and7 in this case], the sum of the values, the range[4 to five and 1 to 8], the mean, the variance, and the standard deviation. Printed out, the table should look like this:
_________________________________________________________
|# of data| sum | range | mean | variance | standard deviation|
_________________________________________________________
| 5| 27| 3 to 7| 5.400| 2.300| 1.517 |
_________________________________________________________
| 7| 34| 2 to 7| 4.857| 3.143| 1.773 |
_________________________________________________________
so far, this is the code that I have.
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <conio.h>

#define STRLEN 256

#pragma warning(disable: 4996)





int main(int argc, char *argv[])

{

	FILE * inFileHandle = NULL;

	FILE * outFileHandle = NULL;

	char filename[STRLEN];

	int numberofdata = 0;

	int i = 0;

	int n = 0;

	int sum = 0;

	int nTotal = 0;

	int square = 0;

	int sumofsquares = 0;

	double mean = 0.0;

	double variance = 0.0;

	double standarddeviantion = 0.0;


	puts("Standard Deviation Program");

	printf("__________________________\n");

	printf("Working model written by Nathan Jernigan\n");

	

	

	if(argc > 1)

	{

		strncpy(filename, argv[1], STRLEN);

	}

	else

	{

		printf("Please enter the name of the File from which to read the data:", filename);

		fgets(filename, STRLEN, stdin);

		if(filename[strlen(filename) - 1] == '\n')

			filename[strlen(filename) - 1] = '\0';

		else

			while(getchar() != '\n');

	}

	

	inFileHandle = fopen(filename, "r");

	if(inFileHandle == NULL)

	{

		printf("File %s could not be opened.\n"

			"Press Any Key to Continue", filename );

		getch();

		return EXIT_FAILURE;

	}

	

	if(argc > 2)

	{

		strncpy(filename, argv[2], STRLEN);

	}

	else

	{

		puts("Please enter the name of the file to write the report: ");

		gets(filename);

	}


	outFileHandle = fopen(filename, "w");

	if(outFileHandle == NULL)

	{

		printf("File %s could not be opened.\n"

			"Press Any Key to Continue", filename );

		getch();

		return EXIT_FAILURE;


	}


	while(fscanf(inFileHandle, "%d", &numberofdata) == 1)

	{

		for(i = 0; i < numberofdata; i++)

		{

			fscanf(inFileHandle, "%d", &n);

			nTotal++;

		    sum += n;

			mean = (sum / nTotal);

			square = n^2;

			sumofsquares += n^2;			

		}

		variance = ((sumofsquares - (sum^2 / nTotal)) / (nTotal - 1));

		standarddeviantion = sqrt(variance);


	fprintf(outFileHandle, "%d  ", nTotal);

	fprintf(outFileHandle, "%d  ", sum);

	fprintf(outFileHandle, "%5.3lf  ", mean);	

	fprintf(outFileHandle, "%5.3lf  ", variance); 

	fprintf(outFileHandle, "%5.3lf  ", standarddeviantion);

		

	}

	fclose(inFileHandle);

	fclose(outFileHandle);

	puts("\nPress Any Key to Continue");




	getch();

	return EXIT_SUCCESS;

}
Take note that the sumofsquares function is not working properly, thus, neither is the variance section. Also note, that I do wish the table to have the tabular lines.

#2
Neo_the_chosen_one

Neo_the_chosen_one

    Newbie

  • Members
  • Pip
  • 4 posts
Hi ,
you should use the function "pow" wich is in the library math.h for sumofsquares :)
regards.

#3
Sov

Sov

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks, that will solve my problems with the last three sections. Can anyone tell me how to arrange it in the table format I showed above? Or how to read from the second and third lines?
and just checking, if this is the right format. I would replace sumofsquares += n^2; with sumofsquares += pow(n, 2); correct?

#4
Sov

Sov

    Newbie

  • Members
  • Pip
  • 4 posts
To clarify, I need to know how to get the program to do with the second and third lines what it's already doing with the first and second. Once it's done that, I need to know how to format it into a table. I'm pretty sure that I would need a second while loop for the next two lines but I'm not positive on what to put for its parameters.

#5
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
Formatted output: PRINTF - formatted output to standard output.
// d-_-b+

#6
Sov

Sov

    Newbie

  • Members
  • Pip
  • 4 posts
To be honest I'm extremely new to this, and can't tell if the information I need is actually in that link. If you give me a code snippet that I can use in a while loop, I should be able to handle the rest. I believe I need to use fscanf for the loop, I just need to know what to put in the () to make it read from the third line




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users