2d array -NEW QUESTION

y my output is not right?

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

const int rows=6, columns=4;
int data[rows][columns];

int main()
{	
	int choice;
	ifstream outfile;
	bool again=true;
	//while(again)
//	  {
		cout<<"\n1 - 5  Display the data"
			  "\n2 - 5  Calculate the Highest and Lowest Data"
			  "\n3 - 5  Calculate the Average value of the Data"
			  "\n4 - 5  Change the Data"
			  "\n5 - 5  QUIT";
		cout<<"\n\nEnter your choice: ";
		cin>>choice;
		switch(choice)
		{
			case 1:
				outfile.open("FactoryData.txt", ios::out|ios::app);
				cout << "Data in \"FactoryData.txt\"" << endl;
				for (int i = 0; i < rows; i++)
				{
					for(int j = 0; j < columns; j++)
					{
						outfile>>data[i][j];
						cout <<data[i][j];
					}
				}
                                outfile.close();
				break;
		}
		
//	  }
	return 0;
}



output:
1 - 5  Display the data
2 - 5  Calculate the Highest and Lowest Data
3 - 5  Calculate the Average value of the Da
4 - 5  Change the Data
5 - 5  QUIT

Enter your choice: 1
Data in "FactoryData.txt"
000000000000000000000000
Press Enter to return to Quincy...





Last edited on
I think its the layout of the file.. you need to get each line as a string and then extract the numbers from it.
what should i do?
closed account (D80DSL3A)
No. The problem is that you opened the file for writing to it.
It looks like you're trying to read values into data[i][j].
Open file for reading instead. Change line 28 to:
outfile.open("FactoryData.txt" );
actually never mind what i said as you know exactly how many numbers there are (i must be getting sleepy lol reading stuff wrong).
strange i just copy/pasted your code and it works fine for me prints out the right values. Are you sure FactoryData.txt is in the right directory etc? . perhaps add a if(is_open) condition there and see what happens?
thanks


i did this before.....but did not work.. :(
oh thats simple .. just add do this
1
2
3
4
5
6
7
8
9
10
11
outfile.open("FactoryData.txt", ios::out|ios::app);
				cout << "Data in \"FactoryData.txt\"" << endl;
				for (int i = 0; i < rows; i++)
				{
					for(int j = 0; j < columns; j++)
					{
						outfile>>data[i][j];
						cout <<data[i][j]<<" ";
					}
					cout<<endl;
				}


this is new question...i try to find the lowest value of the data..but the output show me 0...where is wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

const int rows=6, columns=4;
int data[rows][columns];

void getdata();
void highlow();

ifstream outfile; 
int high=0, low=0, highrow, highcol, lowrow, lowcol;
int i, j;

int main()
{	
	int choice;
	
	bool again=true;
	
	outfile.open("FactoryData.txt", ios::out);
	//while(again)
//	  {
		cout<<"\n1 - 5  Display the data"
			  "\n2 - 5  Calculate the Highest and Lowest Data"
			  "\n3 - 5  Calculate the Average value of the Data"
			  "\n4 - 5  Change the Data"
			  "\n5 - 5  QUIT";
		cout<<"\n\nEnter your choice: ";
		cin>>choice;
		if (outfile.is_open())
		{
			switch(choice)
			{
				case 1:	   
					getdata();
					break;
				case 2:
					highlow();
					break;
			}
		}
		else
			cout<<"Unable to open the file!";	
//	  }
	outfile.close();
	return 0;
}

void getdata()
{
	cout << "\nData in \"FactoryData.txt\"\n" << endl;	
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{	
			outfile>>data[i][j];
			cout <<data[i][j]<<"	";  						
		}
		cout<<"\n";
	}
}

void highlow()
{
        int low=data[0][0];
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{	
			outfile>>data[i][j];
			if (data[i][j]>high)
			{
				high=data[i][j];
				highrow=i;
				highcol=j;
			}
			if (data[i][j]<low)
			{
				low=data[i][j];
				lowrow=i;
				lowcol=j;
			}
		}
	}
	cout<<"\nThe Highest Value:"<<"\nROW "<<highrow<<" COLUMN "<<highcol<<"\nVALUE: "<<high << "\n";
	cout<<"\n\nThe Lowest Value:"<<"\nROW "<<lowrow<<" COLUMN "<<lowcol<<"\nVALUE: "<<low << "\n";
}


1 - 5  Display the data
2 - 5  Calculate the Highest and Lowest Data
3 - 5  Calculate the Average value of the Data
4 - 5  Change the Data
5 - 5  QUIT

Enter your choice: 2

The Highest Value:
ROW 5 COLUMN 2
VALUE: 920


The Lowest Value:
ROW 0 COLUMN 0
VALUE: 0

Press Enter to return to Quincy... 
Last edited on
void life...thanks....
to answer your second question it is because you've set low to 0 the 2nd if statement in your highlow function will never be true as nothing in your data file is less than 0. if u want to find the lowest, u need to set low to something in your data and then compare with everthing else. Also might be helpful to cout highrow/lowrow + 1 . Computers might start numbering everything from 0 but we humans start from 1 :)
do u mean??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void highlow()
{
        int low=data[0][0];
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{	
			outfile>>data[i][j];
			if (data[i][j]>high)
			{
				high=data[i][j];
				highrow=i;
				highcol=j;
			}
			if (data[i][j]<low)
			{
				low=data[i][j];
				lowrow=i;
				lowcol=j;
			}
		}
	}
	cout<<"\nThe Highest Value:"<<"\nROW "<<highrow<<" COLUMN "<<highcol<<"\nVALUE: "<<high << "\n";
	cout<<"\n\nThe Lowest Value:"<<"\nROW "<<lowrow<<" COLUMN "<<lowcol<<"\nVALUE: "<<low << "\n";
}
i know where is wrong? but i do not know how to fix it
Last edited on
Yeah thats kindof what i mean. Settling low = data[0][0] there won't help as you havent initialized the array data yet (so it will contain some gibberish value) how about calling your getdata function before it and and then set low to data[0][0] and then it just becomes a problem of sorting the array data and you dont need to read from the file again. i.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void highlow()
{
    getdata();
    low = data[0][0];
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{
			if (data[i][j]>high)
			{
				high=data[i][j];
				highrow=i;
				highcol=j;
			}
			if (data[i][j]<low)
			{
				low=data[i][j];
				lowrow=i;
				lowcol=j;
			}
		}
	}
	cout<<"\nThe Highest Value:"<<"\nROW "<<highrow+1<<" COLUMN "<<highcol+1<<"\nVALUE: "<<high << "\n";
	cout<<"\n\nThe Lowest Value:"<<"\nROW "<<lowrow+<<" COLUMN "<<lowcol+<<"\nVALUE: "<<low << "\n";
}


only problem is this will print the array too..so perhaps remove cout statements from getdata and put the them in main itself.. its better to do that anyway
Last edited on
thanks...u help me a lot..
so yeah your whole program ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream> //dont really need the other librarys  

using namespace std;

const int rows=6, columns=4;
int data[rows][columns];

void getdata();
void highlow();

ifstream outfile;  
int high=0, low, highrow, highcol, lowrow, lowcol;
int i, j;                      //its generally considered bad pratice to have global variables like these where its not really necessary 

int main()
{
	int choice;

	bool again=true;

	outfile.open("FactoryData.txt", ios::out);
	//while(again)
//	  {
		cout<<"1 - 5  Display the data"
			  "\n2 - 5  Calculate the Highest and Lowest Data"
			  "\n3 - 5  Calculate the Average value of the Data"
			  "\n4 - 5  Change the Data"
			  "\n5 - 5  QUIT";
		cout<<"\n\nEnter your choice: ";
		cin>>choice;
		if (outfile.is_open())
		{
			switch(choice)
			{
				case 1:
				    cout << "\nData in \"FactoryData.txt\"\n" << endl;
					getdata();
					for (int i = 0; i < rows; i++)
                    {
                        for(int j = 0; j < columns; j++)
		                 {
		                    cout<<data[i][j]<<"\t";

		                 }
                         cout<<"\n";
                    }
					break;
				case 2:
					highlow();
					break;
			}
		}
		else
			cout<<"Unable to open the file!";
//	  }
	outfile.close();
	return 0;
}

void getdata()
{
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{
			outfile>>data[i][j];

		}
	}
}

void highlow()
{
    getdata();
    low = data[0][0];
	for (int i = 0; i < rows; i++)
	{
		for(int j = 0; j < columns; j++)
		{
			if (data[i][j]>high)
			{
				high=data[i][j];
				highrow=i;
				highcol=j;
			}
			if (data[i][j]<low)
			{
				low=data[i][j];
				lowrow=i;
				lowcol=j;
			}
		}
	}
	cout<<"\nThe Highest Value:"<<"\nROW "<<highrow+1<<" COLUMN "<<highcol+1<<"\nVALUE: "<<high << "\n";
	cout<<"\n\nThe Lowest Value:"<<"\nROW "<<lowrow+1<<" COLUMN "<<lowcol+1<<"\nVALUE: "<<low << "\n";
}


And your welcome
Last edited on
Topic archived. No new replies allowed.