Changing from scanf to reading a data file into an array

C language. My program is written, I just need to fix it

Page 1 of 1

1 Replies - 3977 Views - Last Post: 28 January 2010 - 08:59 AM Rate Topic: -----

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Changing from scanf to reading a data file into an array

Posted 28 January 2010 - 07:52 AM

I'm working on a program thats reads a .DAT file into an array and then calculates the minimum, maximum, and average of each column in the array. The code I've written so far uses scanf to ask the user for each element in the array. I need to know how to replace the
int** buildTable(void) (shown in my program)
with this:
int** buildTable (int data [], int size, int range)
{
//	Local Declarations
	int   dataIn;
	int   loader	 = 0;
	int   lineSize   = 8;
	int   numPrinted = 0;
		int   i;

	FILE* fpData;
if (!(fpData = fopen ("test.dat", "r")))
	   printf("Error opening file\a\a\n") , exit (100); 

	while (loader < size 
		&& fscanf(fpData, "%d", &dataIn) != EOF)
	   if (dataIn >= 0 && dataIn <= range)
		   data[loader++] = dataIn;

	printf("\n\nArray read from file:\n\n");
	printf("Rows/Columns:   ");
	for (i = 0; i < 2; i++)
		printf("%3d ", data[i]);

	printf("\n\n");
	for (i = 2; i < size; i++)
	   {
		numPrinted++;
		printf("%3d ", data[i]);
		if (numPrinted >= lineSize)
		   {
			printf("\n");
			numPrinted = 0;
		   } // if 
	   } // for 



And this is the data I need to read:

4 8
838 758 113 515 51 627 10 419
212 86 749 767 84 60 225 543
89 183 137 566 966 978 495 311
367 54 31 145 882 736 524 505

where the 4 and 8 represent rows/columns and the rest form the array.

When I simply try replacing it, I get a lot of compile errors I don't know how to fix. How can I add this in and still get it to compile and calculate properly? Also, my program shows how to find min, max, and average of each row. However, I need it for each column instead. Do I do this simply by changing all rows to columns and all columns to rows?

This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

//	Function Declarations
int** buildTable   (void);
void  fillTable	(int** table); 
void  processTable (int** table); 
int   smaller	  (int   first, int second);
int   larger	   (int   first, int second);
int   rowMinimum   (int*  rowPtr);
int   rowMaximum   (int*  rowPtr);
float rowAverage   (int*  rowPtr);

int main (void)
{
//	Local Declarations
   int** table;

//	Statements
	table = buildTable(); 
	fillTable	(table); 
	processTable (table); 
	getch();
	return 0; 
}	// main
int** buildTable (void) 
{
//	Local Declarations
	int   rowNum;
	int   colNum;
	int** table;
	int   row;

//	Statements
	printf("\nEnter the number of rows in the table: "); 
	scanf ("%d", &rowNum); 
	table = (int**) calloc(rowNum + 1, sizeof(int*)); 
	for (row = 0; row < rowNum; row++) 
		{
		 printf("Enter number of integers in row %d: ", 
				 row + 1);
		 scanf ("%d", &colNum); 
		 table[row] = (int*)calloc(colNum + 1, 
								   sizeof(int));
		 table[row] [0] = colNum; 
		} // for
	table[row] = NULL;
	return table;
}	// buildTable

void fillTable (int** table) 
{
//	Local Declarations
	int row = 0;
	int column;
//	Statements
	printf("\n =============================");
	printf("\n Now we fill the table.\n");
	printf("\n For each row enter the data");
	printf("\n and press return: ");
	printf("\n =============================\n");

	while (table[row] != NULL) 
	   {
		printf("\n row %d (%d integers) =====> ", 
				row + 1, table[row][0]);
		for ( column = 1; 
				 column <= *table[row]; 
				 column++)
			 scanf("%d", table[row] + column); 
		row++;
	   } // while
	return;
}	// fillTable

void processTable (int** table)
{
//	Local Declarations 
int   row = 0;
int   rowMin;
int   rowMax;
float rowAve;

//	Statements
	while (table[row] != NULL)
	  {
	   rowMin = rowMinimum (table[row]);
	   rowMax = rowMaximum (table[row]);
	   rowAve = rowAverage (table[row]);
	   printf("\nThe statistics for row %d ", row + 1);
	   printf("\nThe minimum: %5d",	rowMin); 
	   printf("\nThe maximum: %5d",	rowMax); 
	   printf("\nThe average: %8.2f ", rowAve);
	   row++;
	  } // while
	return;
}	// processTable

int rowMinimum (int* rowPtr) 
{

//	Local Declarations
	int rowMin = INT_MAX;
	int column;
//	Statements
	for ( column = 1; column <= *rowPtr; column++) 
		 rowMin = smaller (rowMin, *(rowPtr + column)); 
	return rowMin;
}	// rowMinimum

int rowMaximum (int* rowPtr) 
{
//	Local Declarations
	int rowMax = INT_MIN;
	int column;
//	Statements
	for ( column = 1; column <= *rowPtr; column++) 
		 rowMax = larger  (rowMax, *(rowPtr + column)); 
	return rowMax;
}	// rowMaximum

float  rowAverage (int* rowPtr) 
{
//	Local Declarations
	float  total = 0;
	float  rowAve;
	int column; 
//	Statements
	for (column = 1; column <= *rowPtr; column++)
		 total += (float)*(rowPtr + column);
	rowAve = total / *rowPtr;
	return rowAve;
}	// rowAverage

int  smaller (int first, int second)
{
//	Statements
	return (first < second ? first : second);
}	// smaller

int larger (int first, int second)
{
//	Statements
	return (first > second ? first : second);
}	// larger


This post has been edited by ninjawesome222: 28 January 2010 - 07:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Changing from scanf to reading a data file into an array

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Changing from scanf to reading a data file into an array

Posted 28 January 2010 - 08:59 AM

The first fscanf is to read the row and column numbers. You can do this with the fscanf call:

		fscanf(fpData, "%d%d", &rowIn, &colIn)



Once you have this information, use the row and column information to control the loops.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1