# Reading Data into a 2-dimensional array and calculating min, max, avrg

Page 1 of 1

## 9 Replies - 7207 Views - Last Post: 28 January 2010 - 09:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=152349&amp;s=b894b65949cc4ba3aab12f8686dc1c8c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ninjawesome222

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

# Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 04:18 PM

I need help reading data from a .DAT file into an array. Then, calculate the min, max, and average of each column of the two-dimensional array. I did a very sloppy job of finding the average of each column, and I know there's an easier way, but I'm not sure what it is. I'm a beginner and this is very confusing. I am programming in C. This is my .dat file 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 first two numbers represent # of rows/columns and the rest are the array. This is what I've programmed so far:

#include <stdio.h>
#include <stdlib.h>

#define MAX_ELMNTS  34
#define MAX_RANGE  999

//	Function Declarations

int** buildTable   (int numbers[], int size, int range);
int main (void)
{
//	Local Declarations
int** table;
int   nums		[MAX_ELMNTS];
int   frequency  [MAX_RANGE];

//	Statements
table = buildTable(nums, MAX_ELMNTS, MAX_RANGE);

getch();
return 0;
}	// main

int** buildTable (int data [], int size, int range)
{
//	Local Declarations
int   dataIn;
int   lineSize   = 8;
int   numPrinted = 0;
int   i;
FILE* fpData;
int   col1, col2, col3, col4;
int   col5, col6, col7, col8;
float avrg;

//	Statements
if (!(fpData = fopen ("example.dat", "r")))
printf("Error opening file\a\a\n") , exit (100);

&& fscanf(fpData, "%d", &dataIn) != EOF)
if (dataIn >= 0 && dataIn <= range)

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

col1 = data[2] + data[10] + data[18] + data[26];
avrg = col1 / 4.00;
printf("\n\nCol 1 Avrg = %.2f", avrg);

col2 = data[3] + data[11] + data[19] + data[27];
avrg = col2 / 4.00;
printf("\nCol 2 Avrg = %.2f", avrg);

col3 = data[4] + data[12] + data[20] + data[28];
avrg = col3 / 4.00;
printf("\nCol 3 Avrg = %.2f", avrg);

col4 = data[5] + data[13] + data[21] + data[29];
avrg = col4 / 4.00;
printf("\nCol 4 Avrg = %.2f", avrg);

col5 = data[6] + data[14] + data[22] + data[30];
avrg = col5 / 4.00;
printf("\nCol 5 Avrg = %.2f", avrg);

col6 = data[7] + data[15] + data[23] + data[31];
avrg = col6 / 4.00;
printf("\nCol 6 Avrg = %.2f", avrg);

col7 = data[8] + data[16] + data[24] + data[32];
avrg = col7 / 4.00;
printf("\nCol 7 Avrg = %.2f", avrg);

col8 = data[9] + data[17] + data[25] + data[33];
avrg = col8 / 4.00;
printf("\nCol 8 Avrg = %.2f", avrg);

return;
}	// buildTable

This post has been edited by ninjawesome222: 27 January 2010 - 07:25 PM

Is This A Good Question/Topic? 0

## Replies To: Reading Data into a 2-dimensional array and calculating min, max, avrg

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 04:25 PM

Welcome to Dream.In.Code!

Thanks!

### #3 ninjawesome222

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

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 04:29 PM

n8wxs, on 27 Jan, 2010 - 03:25 PM, said:

Welcome to Dream.In.Code!

Thanks!

okay, I didn't know how to do it so thanks.

### #4 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 04:47 PM

You are declaring buildTable() as returning an int ** but you are not returning that:
...
col8 = data[9] + data[17] + data[25] + data[33];
avrg = col8 / 4.00;
printf("\nCol 8 Avrg = %.2f", avrg);

return;
}	// buildTable

I would fix this by changing
//	Function Declarations

int** buildTable   (int numbers[], int size, int range);

to
//	Function Declarations

void buildTable   (int numbers[], int size, int range);

and changing
//	Statements
table = buildTable(nums, MAX_ELMNTS, MAX_RANGE);

to
//	Statements
buildTable(nums, MAX_ELMNTS, MAX_RANGE);

and finally
//	Statements
int** buildTable (int data [], int size, int range)

to
//	Statements
void buildTable (int data [], int size, int range)

### #5 ninjawesome222

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

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 07:08 PM

n8wxs, on 27 Jan, 2010 - 03:47 PM, said:

You are declaring buildTable() as returning an int ** but you are not returning that:
...
col8 = data[9] + data[17] + data[25] + data[33];
avrg = col8 / 4.00;
printf("\nCol 8 Avrg = %.2f", avrg);

return;
}	// buildTable

I would fix this by changing
//	Function Declarations

int** buildTable   (int numbers[], int size, int range);

to
//	Function Declarations

void buildTable   (int numbers[], int size, int range);

and changing
//	Statements
table = buildTable(nums, MAX_ELMNTS, MAX_RANGE);

to
//	Statements
buildTable(nums, MAX_ELMNTS, MAX_RANGE);

and finally
//	Statements
int** buildTable (int data [], int size, int range)

to
//	Statements
void buildTable (int data [], int size, int range)

Okay, I changed my program to fit what you said and it looks much neater than before but it stills does exactly what it did before. How can I write the program so it knows what numbers are in each of the 8 columns? I'm pretty sure I can find the min and max in each column if I knew how to do this. Also, is there a simpler way to write the "average" function without having to copy it for each column?

### #6 taylorc8

• B&

Reputation: 150
• Posts: 1,572
• Joined: 21-July 09

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 08:21 PM

Yes, the average function can be written with something similar to:
void rowAverage(int number)
{
cout << "Row # " << number;
}

looping will allow you to do this.

### #7 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 27 January 2010 - 08:40 PM

Using this code:
#include "stdafx.h"

#include <iostream>

//#include <stdio.h>
//#include <stdlib.h>
#include <cstdio>
#include <cstdlib>

#define MAX_ELMNTS  34
#define MAX_RANGE  999

//	Function Declarations

using namespace std;

void buildTable   (int numbers[], int size, int range);

int _tmain(int argc, _TCHAR* argv[]) {
//	Local Declarations
//int** table;
int   nums		[MAX_ELMNTS];
int   frequency  [MAX_RANGE];

//	Statements
//table = buildTable(nums, MAX_ELMNTS, MAX_RANGE);
buildTable(nums, MAX_ELMNTS, MAX_RANGE);

system("pause");
return 0;
}
void buildTable (int data [], int size, int range)
{
//	Local Declarations
int   dataIn;
int   lineSize   = 8;
int   numPrinted = 0;
int   i;
FILE* fpData;
int   col1, col2, col3, col4;
int   col5, col6, col7, col8;
float avrg;

//	Statements
if (!(fpData = fopen ("example.dat", "r")))
printf("Error opening file\a\a\n") , exit (100);

&& fscanf(fpData, "%d", &dataIn) != EOF)
if (dataIn >= 0 && dataIn <= range)

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

col1 = data[2] + data[10] + data[18] + data[26];
avrg = col1 / 4.00;
printf("\n\nCol 1 Avrg = %.2f", avrg);

col2 = data[3] + data[11] + data[19] + data[27];
avrg = col2 / 4.00;
printf("\nCol 2 Avrg = %.2f", avrg);

col3 = data[4] + data[12] + data[20] + data[28];
avrg = col3 / 4.00;
printf("\nCol 3 Avrg = %.2f", avrg);

col4 = data[5] + data[13] + data[21] + data[29];
avrg = col4 / 4.00;
printf("\nCol 4 Avrg = %.2f", avrg);

col5 = data[6] + data[14] + data[22] + data[30];
avrg = col5 / 4.00;
printf("\nCol 5 Avrg = %.2f", avrg);

col6 = data[7] + data[15] + data[23] + data[31];
avrg = col6 / 4.00;
printf("\nCol 6 Avrg = %.2f", avrg);

col7 = data[8] + data[16] + data[24] + data[32];
avrg = col7 / 4.00;
printf("\nCol 7 Avrg = %.2f", avrg);

col8 = data[9] + data[17] + data[25] + data[33];
avrg = col8 / 4.00;
printf("\nCol 8 Avrg = %.2f\n", avrg);

return;
}	// buildTable

I get this output:

Quote

Rows/Columns: 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

Col 1 Avrg = 376.50
Col 2 Avrg = 270.25
Col 3 Avrg = 257.50
Col 4 Avrg = 498.25
Col 5 Avrg = 495.75
Col 6 Avrg = 600.25
Col 7 Avrg = 313.50
Col 8 Avrg = 444.50
Press any key to continue . . .

### #8 ninjawesome222

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

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 28 January 2010 - 07:37 AM

n8wxs, on 27 Jan, 2010 - 07:40 PM, said:

I get this output:

Rows/Columns: 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

Col 1 Avrg = 376.50
Col 2 Avrg = 270.25
Col 3 Avrg = 257.50
Col 4 Avrg = 498.25
Col 5 Avrg = 495.75
Col 6 Avrg = 600.25
Col 7 Avrg = 313.50
Col 8 Avrg = 444.50
Press any key to continue . . .

Yes, this is what i get even without changing it. How can I change the array into rows and columns so I can find the max, min, and average of each column?

### #9 baavgai

• Dreaming Coder

Reputation: 7183
• Posts: 14,970
• Joined: 16-October 07

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 28 January 2010 - 09:09 AM

Min and max you can figure out along the way. Make min and max equal to the first column. Every column after that, compare what you have and replace min or max as needed. For average, keep a running total of all columns, divide by number of columns at the end.

However, you aren't using a 2D array, data is a 1D array, so I expect that's wrong. Since the data file includes the dimensions, it's probable that you're expected to dynamically allocate the array, not just declare a MAX size.

Currently, it doesn't appear you're close to meeting the assignment requirements.

The framework of the program would presumably look something like:
int* buildRow(int cols);
int** buildTable(int rows, int cols);
void freeTable(int** table, int rows);
void loadTable(const char *fileName, int** table, int *rows, int *cols);

int **table, *colTotals, rows, cols, row, col, total = 0;

colTotals = buildRow(cols);
for(col=0; col<cols; col++) { colTotals[col] = 0; }

for(row=0; row<rows; row++) {
for(col=0; col<cols; col++) {
colTotals[col] += table[row][col];
total += table[row][col];
}
}
/* ... */
free(colTotals);
freeTable(table, rows);

Hope this helps.

### #10 ninjawesome222

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

## Re: Reading Data into a 2-dimensional array and calculating min, max, avrg

Posted 28 January 2010 - 09:19 AM

I see how this helps, but I don't know how to place it into my program. What I mean by this is what do I keep and what do I delete so it will work?