0

The point is to read a file and then display the information from a 2D array. Then find the highest value in each column and divide all the numbers in each column by the high value for that column. Then display the quotients (which I put back into the original 2D array and then reprinted it). Its all straight out with no functions. But well separated with comments. I can't get the file to read. That is my first problem, so I can't even continue to debug the rest. If it helps the file data is this:
1.267 0.167 0.250 2.670 1.000
3.240 0.376 0.375 3.400 1.128
7.564 0.668 0.500 4.303 1.270
5.041 1.043 0.625 5.313 1.410
4.660 1.502 0.750 7.650 1.693
5.727 2.044 0.875 3.600 2.257

#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;

// Globals
const int max_rows = 6, max_cols = 5;

// Declarations
int i, j, sub, num_rows, num_cols;
int max_value[max_cols], num_array [max_rows][max_cols];
ifstream infile ("C:\\wk03_data(2010.04).txt");

int main()
{
// Read file into array
infile>> num_rows>> num_cols;
for (i = 0; i < num_rows; i++)
{
	for (j = 0; j < num_cols; j++)
	{
		infile>>num_array [i][j];
	}
}

// Print array
for (i = 0; i < num_rows; i++)
{
	for (j = 0; j < num_cols; j++)
	{
		cout<< num_array[i][j]<< " ";
	}
	cout<< endl;
}

// Find max values
sub = 0;
i = 0, j = 0;
max_value[sub] = num_array[i][j];
for (i = 0; i < num_rows; i++)
{
	while (num_array[i][j] < num_array[i+1][j])
	{
		max_value[sub] = num_array[i+1][j];
	}
}

sub = 1;
i = 0, j = 1;
max_value[sub] = num_array[i][j];
for (i = 0; i < num_rows; i++)
{
	while (num_array[i][j] < num_array[i+1][j])
	{
		max_value[sub] = num_array[i+1][j];
	}
}

sub = 2;
i = 0, j = 2;
max_value[sub] = num_array[i][j];
for (i = 0; i < num_rows; i++)
{
	while (num_array[i][j] < num_array[i+1][j])
	{
		max_value[sub] = num_array[i+1][j];
	}
}

sub = 3;
i = 0, j = 3;
max_value[sub] = num_array[i][j];
for (i = 0; i < num_rows; i++)
{
	while (num_array[i][j] < num_array[i+1][j])
	{
		max_value[sub] = num_array[i+1][j];
	}
}

sub = 4;
i = 0, j = 4;
max_value[sub] = num_array[i][j];
for (i = 0; i < num_rows; i++)
{
	while (num_array[i][j] < num_array[i+1][j])
	{
		max_value[sub] = num_array[i+1][j];
	}
}

// Divide each number in a column by the columns highest value
i = 0, j = 0, sub = 0;
for (i = 0; i < num_rows; i++)
{
	num_array[i][j] = num_array[i][j] / max_value[sub];
}

i = 0, j = 1, sub = 1;
for (i = 0; i < num_rows; i++)
{
	num_array[i][j] = num_array[i][j] / max_value[sub];
}

i = 0, j = 2, sub = 2;
for (i = 0; i < num_rows; i++)
{
	num_array[i][j] = num_array[i][j] / max_value[sub];
}

i = 0, j = 3, sub = 3;
for (i = 0; i < num_rows; i++)
{
	num_array[i][j] = num_array[i][j] / max_value[sub];
}

i = 0, j = 4, sub = 4;
for (i = 0; i < num_rows; i++)
{
	num_array[i][j] = num_array[i][j] / max_value[sub];
}

// Display each quotient
for (i = 0; i < num_rows; i++)
{
	for (j = 0; j < num_cols; j++)
	{
		cout<< num_array[i][j]<< " ";
	}
	cout<< endl;
}
}
2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by StuXYZ
0

Two problems (initailly).

First problem: You are expecting two integers in the first line of the file.
I modified your file to have 6 5 as the first line.

Second problem: You are reading double precision number into and integer array. Not good. So remove your
definition of num_array, and then add something like this line: double num_array [max_rows][max_cols]; and your program runs successfully to the print array loop.

Since you seem to have figured out how to test in parts (e.g. by putting a print statement in as soon as you have done something. You should be fine with the rest of the program. Just move you print loops, down each time. [or put the print into a function].

Edited by StuXYZ: n/a

0

Two problems (initailly).

First problem: You are expecting two integers in the first line of the file.
I modified your file to have 6 5 as the first line.

Second problem: You are reading double precision number into and integer array. Not good. So remove your
definition of num_array, and then add something like this line: double num_array [max_rows][max_cols]; and your program runs successfully to the print array loop.

Since you seem to have figured out how to test in parts (e.g. by putting a print statement in as soon as you have done something. You should be fine with the rest of the program. Just move you print loops, down each time. [or put the print into a function].

I entered in the 6 5 into the file and changed the 2D array to a double one. Thanks for that. I still cannot get it to run. It opens and just says hit enter to continue and closes. Anybody know what I'm doing wrong?

0

My guess then is that you are running underwindows. Just put
a std::cin>>i; at the end .

You could also put some simple output like: std::cout<<"number of rows =="<<num_row<<std::endl; and make sure it says SOMETHING!!!

[You might even want to put that before you start reading the file.

0

My guess then is that you are running underwindows. Just put
a std::cin>>i; at the end .

You could also put some simple output like: std::cout<<"number of rows =="<<num_row<<std::endl; and make sure it says SOMETHING!!!

[You might even want to put that before you start reading the file.

Where exactly should I put the std::cin>>i; ? Should I do it for row and col? Or just the declaration? I am using windows by the way, could you elaborate on how to use the std cin? Thank you so much for helping me.

0

Ok as far as I know [And I have never coded on a windows box], it often seems that
beginners have the problem that they run the program, it creates a window, then closes the window when the program ends. That is not how it happens on a unix/linux terminal.

This sounds sort of like how you described it, so I suggested the std::cin>>i; as a way to delaying the end of the program. It you put a std::cout<<"THIS is my program"<<std::endl; as the first line after int main() { and you don't see it, then you have the problem.

If that is the case, then add the std::cin after the print statement and see if you
can see the output. If you do, all is fine and you can add the std::cin at the last
line of the program. [This assumes that you have not exits before the last line in
main.]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.