1 Replies - 1558 Views - Last Post: 23 April 2009 - 03:10 AM Rate Topic: -----

#1 edge2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 09

confused about file input / strings and arrays (detailed)

Post icon  Posted 22 April 2009 - 04:41 PM

Im new to the whole programming thing and I am getting confused about how to make my code work the way it should.

"Write a program to take Knumbers and scores from an input file (score.dat), calculate the sum and average of the scores for each row
and each column, and display the scores, the sum, and the average in a form of table like the figure below. Use a string to handle Knumbers and an array to calculate the sum and average of each score.

Output should look like this (the spacing is kind of off when I post it on the forum)


Knumber Score1 Score2 Score3 Score4 Score5 Sum Average
--------- ------- ------- -------- -------- -------- ----- ----------

K001 80 90 75 87 85 417 83.00
K002 100 99 89 95 70 453 90.00
K003 70 76 87 95 100 428 85.00
K004 50 60 79 75 99 363 72.00
K005 70 80 90 100 60 400 80.00
K006 50 60 78 45 62 295 59.00
K007 89 98 90 95 97 469 93.00

--------- -------- ------- -------- -------- ------- ----- ----------

Sum 509 563 588 592 573
Average 72 80 84 84 81



My input file (score.dat) contains the following information on it

K001 80 90 75 87 85
K002 100 99 89 95 70
K003 70 76 87 95 100
K004 50 60 79 75 99
K005 70 80 90 100 60
K006 50 60 78 45 62
K007 89 98 90 95 97



and the following is my code
--I know its horribly wrong but I am stuck and have been sitting here sifting through notes trying to figure it out for about 2 hours now--



#include <stdio.h>
#include <string.h>

main(void)
{
		  FILE *imp;					   /* File input pointer */
		  int scoreArray[5];			 /* array to handle scores */
		  int score;						 /*  Integer values */
		  int i=0,a=0;
		  int input_status;
		  char knumber[10];				/*  Character string to handle K numbers */
		  double average=0, sum=0;	   /* double data types to determine sum and average */
		  
		  
		  imp = fopen("score.dat", "r");				/* Opens the .dat file (scores.dat)  */
		  
		  input_status = fscanf(imp, "%d", &knumber);		  /* returned status value  */

		  
		  for(a=0; a<8; a++){							  /* Loop to sum up individual columns */  
				   for(i=0; i<6; i++){					   /* Loop to sum up individual rows */
				   scoreArray[i] += score;
				   
				   }}

		  fclose(imp);
 

system("pause");
}




right now it compiles with no error but executes with no data at all

My questions are how to associate the strings and arrays (my loops use a and i integers cuz thats what we have been doing in class but im not sure the program knows to associate the data from the input file to those integer types)
Also we never really learned how to make the execution of a program look like a table as in the example above.

any help would be greatly appreciated, thank you ;)

Is This A Good Question/Topic? 0
  • +

Replies To: confused about file input / strings and arrays (detailed)

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: confused about file input / strings and arrays (detailed)

Posted 23 April 2009 - 03:10 AM

Welcome to DIC!

I take it you are writing in C and not C++?

1 - You are looking at a two dimensional (2D) array. A 9 by 9 2D array called 'theArray' is declared like this
int theArray[9][9];

2 - Since your array is made up of rows and columns use the meaningful variable names 'row' and 'col' instead of 'i' and 'a' which are meaningless in this context.

3 - If I have an int value stored in a variable called 'theInt' I can store it in a location in 'theArray' defined by 'row' and 'col' (they have to have a value associated with them) like this
theArray[row][col] = theInt;

4 - main() always returns an int. Write it like this
int main(void)
and have it
return 0;
at its end.

There are a few ideas to get you started.
Use them in partnership with reading your textbook or reading here
http://www.cprogramm....html#ctutorial
and show us how you get along.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1