1 Replies - 2211 Views - Last Post: 06 November 2009 - 06:33 AM Rate Topic: -----

#1 js500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 09

Pulling numbers from an input file to sum

Posted 05 November 2009 - 09:37 PM

I am pulling numbers from an input file and i have already done the sum and average of the rows (horizontally). Now I need to get the sum and average of the columns (vertically). For some reason I cannot get the numbers to pull correctly or there is an error in the code because it prints out zeros instead of the number that i need. Please help...
here are the numbers for the input file:
-0.043200 -0.003471 0.000000
-0.040326 -0.004851 -0.000737
-0.018204 -0.004246 -0.001530
0.022249 0.008891 0.004870
0.074892 0.044237 0.032171
0.129600 0.100233 0.089016
0.174747 0.160100 0.161792
0.200242 9.199106 0.214417
0.174747 0.160100 0.161792


 #include <stdio.h>

main()
{
	  /* pointer to input and output file */
	  FILE *inp,
		   *outp;
	  
	  /* declare variables */
	  double sum = 0;	
	  double average = 0;
	  double a1, a2,a3;
	  double input_status;
	  double c1, c2, c3;
	  double sum_c1, sum_c2, sum_c3;
	  int i;
	  int count_lines;
	  
	  /* set the increment starting at zero */
	  i = 0;
	  sum_c1 =0;
	  sum_c2 =0;
	  sum_c3 =0;
	  
	  /* open the input and output files */
	  inp = fopen("number1.dat", "r");
	  outp = fopen("number1.out", "w");
	  

	  
	  /* displaying data */
	  fprintf(outp, "			#1		  #2		 #3		Sum	Average\n");
	  fprintf(outp, "	 _________  __________  _________  _________  _________\n");
	  
	  /* getting numbers from input file */
	  input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);
	  
	  /* first while loop to compute sum and average */
	  while (input_status != EOF, i<=0)
	  {
			i += 1;
			sum = a1 + a2 + a3;
			average = sum / 3;
			fprintf(outp, "   %d %lf  %lf   %lf  %lf  %lf\n", i, a1, a2, a3, sum, average);
			input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);   
	  }
	  
	  while (input_status != EOF, i<=2)
	  {
			i += 1;
			sum = a1 + a2 + a3;
			average = sum / 3;
			fprintf(outp, "   %d %lf  %lf   %lf  %lf  %lf\n", i, a1, a2, a3, sum, average);
			input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);   
	  }
	  
	  /* created second while loop so the columns would line up after numbers were positive */
	  while (input_status != EOF, i>=2 && i<=8)
	  {
			i += 1;
			sum = a1 + a2 + a3;
			average = sum / 3;
			fprintf(outp, "   %d  %lf   %lf	%lf   %lf   %lf\n", i, a1, a2, a3, sum, average);
			input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);   
	  }
	  
	  fprintf(outp, "	 _________  __________  _________  _________  _________\n\n");
	  
	  while (input_status != EOF && i>= 0 && i <= 8)
	  {
		  input_status = fscanf(inp, "%lf %lf %lf", &c1, &c2, &c3);
		  sum_c1 = sum_c1 + c1;
		  sum_c2 = sum_c2 + c2;
		  sum_c3 = sum_c3 + c3;
		  i+=1; 
	  }
	  
	  fprintf(outp, "Sum  %lf  %lf  %lf\n", &sum_c1, &sum_c2, &sum_c3);
	  fprintf(outp, "	 _________  __________  _________  _________  _________\n\n");
	  
	  
	  
	  fprintf(outp, "Avg.\n");
	  fprintf(outp, "\n");
	  
	  /* close the input and output files */
	  fclose(inp);
	  fclose(outp);
	  
	  system("pause");
	  
}


This post has been edited by js500: 05 November 2009 - 10:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pulling numbers from an input file to sum

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Pulling numbers from an input file to sum

Posted 06 November 2009 - 06:33 AM

while (input_status != EOF, i<=0)

I'm thinking the comma operator is not what you want there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1