Trouble plotting stars on a 2D array graph- No idea where I went wrong

I tried searching all over the internet for some help on this problem, but I couldn't find even the smallest amount of help for it. The problem is to make a graph of 30 x 30 integers taken from an input file (which I will paste below). After that is done, the program should find any number whose self, and it's four neighbors (above, below, left and right respectively) average greater than 6.0 and return a * in the box. If the average of any given number and it's surrounding neighbors is less than 6.0, the program should return a blank. I've coded for every internal line, and also for the corners, top, bottom, and sides, since they will have less than four neighbors.

The issue that I'm having now- after more hours of tinkering than I care to think about, makes no sense. First, three *'s appear where they shouldn't. Two appear on 0's surrounding an integer 20 (but not ON the 20) even though the average of those numbers is 4 both times. And, again, a third star appears on a 0 with a 12 to the bottom and to the right even though this average would be 4.8. No matter what I do or try, I really can't figure why that would be. The second issue I'm having is the last row- row 30. In just that row (which should have no stars whatsoever) it returns 21 stars, for no reason that I can even begin to fathom. I am at a complete loss here, and would really appreciate any help that anyone can offer. Below is the input file, my code, and the latest output, in that order. Thanks in advance!

ASG_starplot.txt:

1 0 0 0 0 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 0 3 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 3 0 0 0 0 4 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 2
0 0 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0 2 0 9 18 9 0 0 0 0 0 0 0 1
0 1 4 18 4 0 4 5 4 0 0 0 0 4 0 0 1 1 2 0 9 0 0 3 0 3 0 0 0 0
0 0 0 4 0 0 0 4 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 6 0
0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0
0 0 0 12 0 0 0 2 0 0 0 0 0 0 0 0 0 2 0 0 4 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 10

Code:

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//Date: 11/9/11
//Description: A program that reads from a data file to produce a 30 x 30 array and identifies stars with a '*'.

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

const int MAX_SIZE = 30;

bool LoadArray(ifstream &input, int intensity[][MAX_SIZE]);

void Plot(ofstream &output, int intensity[][MAX_SIZE]);

int main()
{
	int numbersTable[MAX_SIZE][MAX_SIZE];

	ifstream input; //establish file input

	input.open("ASG716_starplot.txt", ios::in);
 
	if (!input) //response for access failure
	{
	cerr << "Cannot open file." << endl;
	}//end if

	ofstream outfile; //establish file output

	outfile.open("ASG716_output.txt");

	if(!outfile)
	{
	cerr << "Memory shortage.\n";
	}//end if

	LoadArray(input, numbersTable);

	if(!LoadArray(input, numbersTable))
	{
		cerr << "Bad data on input file\n";
	}

	outfile << setw(30) << "Star Graph\n" << setw(32) << "by Don Emory\n";

	Plot(outfile, numbersTable);

	return 0;
}

bool LoadArray(ifstream &input, int intensity[][MAX_SIZE])
{ 
	for(int i = 0; i < MAX_SIZE; i++)
		for(int j = 0; j < MAX_SIZE; j++)
		{
			input >> intensity[i][j];
		}
	if(!input.eof() && input.fail())
		return false;
	else if(!input.eof() && input.good())
		return false;
	else
		return true;
}
void Plot(ofstream &output, int intensity[][MAX_SIZE])
{
	double sum = 0.0;

	double average = 0.0;

	output << setw(62) << setfill('-');

	for(int i = 0; i < MAX_SIZE; i++)
	{
		output << "\n";

		output << "|";

		for(int j = 0; j < MAX_SIZE; j++)
		{
		sum = 0.0;

		average = 0.0;

		if((intensity[i][j]) >= 1)
		{
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i + 1][j]) + (intensity[i][j - 1]) + (intensity[i][j + 1]);

		average = sum / 5.0;			
	
		if(average > 6.0)
			output << "*|";
		else 
			output << " |";
		}

		else if((intensity[0][0]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i + 1][j]) + (intensity[i][j + 1]);
	
		average = sum / 3.0;

		if(average > 6.0)	
			output << "*|";
		else
			output << " |";
		}
		
		else if((intensity[0][MAX_SIZE]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i + 1][j]) + (intensity[i][j - 1]);
	
		average = sum / 3.0;

			if(average > 6.0)
				output << "*|";
			else
			output << " |";
		}

		else if((intensity[MAX_SIZE][0]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i][j + 1]);
	
		average = sum / 3.0;

			if(average > 6.0)
				output << "*|";
			else
				output << " |";
		}

		else if((intensity[MAX_SIZE][MAX_SIZE]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i][j - 1]);
	
		average = sum / 3.0;

			if(average > 6.0)
				output << "*|";
			else
				output << " |";
		}

		else if((intensity[0][j]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i + 1][j]) + (intensity[i][j - 1]) + (intensity[i][j + 1]);
	
		average = sum / 4.0;

			if(average > 6.0)
				output << "*|";
			else
				output << " |";
		}

		else if((intensity[MAX_SIZE][j]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i][j - 1]) + (intensity[i][j + 1]);
	
		average = sum / 4.0;

			if(average > 6.0)
				output << "*|";
			else 
				output << " |";
		}

		else if((intensity[i][0]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i + 1][j]) + (intensity[i][j + 1]);
	
		average = sum / 4.0;

			if(average > 6.0)
				output << "*|";
			else
				output << " |";
		}

		else if((intensity[i][MAX_SIZE]) >= 1)
		{	
		sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i + 1][j]) + (intensity[i][j - 1]);
	
		average = sum / 4.0;

			if(average > 6.0)
				output << "*|";
			else
				output << " |";
		}

		}
	output << "\n" << setw(62) << setfill('-');
	}
}


Note: For the both the input file and the output file, they will be easier to read/understand when copied and pasted into a notepad file since they don't align right here.
Last edited on
Current Output File:

-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | |*| | | | | | | | | |
-------------------------------------------------------------
| | | |*| | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | |*| | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | |*| | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | |*| | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | |*| | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | |*| | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | |*| | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | |*| | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | |*| | | | | | | | | | | | | | | | |*| | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
-------------------------------------------------------------
| |*|*|*| |*|*| |*|*|*| | |*|*| | |*| |*|*|*|*|*| |*|*|*|*|*|
Any ideas/suggestions/help out there? I could really use some guidance. Thanks in advance!
Hey @sgiusti88,
I've got a few suggestions for you. Your plot function is extremely messy. You got to rewrite it (and more cleanly).
I've spotted one mistake though. In this code
 
sum = (intensity[i][j]) + (intensity[i - 1][j]) + (intensity[i + 1][j]) + (intensity[i][j - 1]) + (intensity[i][j + 1]);


in your plot function. When i=0 and j=0. think what i-1 and j-1 will get you. Complete bogus. So that code is wrong and there may be much more codes like this.

What you need to do is cool down. I can tell you that this is no easy problem. So, take a rest (about 10 minutes). Go for a walk. Come back and then take a pencil and notebook and write what you think must be done. Write the psuedocode first before jumping to your keyboard. I hope
this will help you out.
So, I hope you returned from your walk. Here, I got something for you.

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

void Plot(ofstream &output, int intensity[][MAX_SIZE])
{	double sum = 0.0;
	double average = 0.0;
	output << setw(62) << setfill('-');

	for(int i = 0; i < MAX_SIZE; i++)
	{
		output << "\n";
		output << "|";

		for(int j = 0; j < MAX_SIZE; j++)
		{
            sum = 0.0;
            average = 0.0;

            //Check if Right 4 element exists
                //If yes
                //Add the Right 4 and current element and calculate average.
                //Write suitable output
            if (j+4 < MAX_SIZE)
                {
                    average = intensity[i][j]+intensity[i][j+1]+intensity[i][j+2]
                            + intensity[i][j+3] + intensity[i][j+4];
                    average /= 5;

                    if (j>6.0)
                    //Display Output
                    ;
                }

            //Check if Left 4 element exists
                //If yes
                //Add the Left 4 and current element and calculate average.
                //Write suitable output

            //Check if Top 4 element exists
                //If yes
                //Add the Top 4 and current element and calculate average.
                //Write suitable output

            //Check if Bottom 4 element exists
                //If yes
                //Add the Bottom 4 and current element and calculate average.
                //Write suitable output
		}

	output << "\n" << setw(62) << setfill('-');
	}
}


Wonder what you are doing. A project?? or homework? Just curious you know.
Last edited on
Topic archived. No new replies allowed.