Grid and averaging numbers help

Hey im taking an intro programming class and our last project is to make a 10x8 grid where the boxes on the outside four sides are given a constant number and as you move inwards on the grid the numbers are averaged by the surrounding boxes. Meaning the the middle box will be a very exact average of the 4 starting numbers or sides. The four starting side numbers will be 4 different numbers. Im really not very good at this. Can someone help me please! Im close to having a grid programmed but confused. Thank you!
Any help? Anyone? please
you will probably get more responses if you post the code you already have.
#include "stdafx.h"
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int grid[10][8];
int row;
int col;
int s = 0;

for (row = 0; row < 10; ++row)
{
for (col = 0; col < 8; ++col)
{
grid[row][col] = ++s
cout << grid[row][col] << " ";
}
cout << endl;
}
}


This is all I have im very lost. This is just the grid of rows and columns ive been working on
In the future please post you code using code tags. There is a little menu to the right of your screen when you post. Select the button that looks like this <> code tags will appear in the window. Place your code inside, it will format the code to make it legible. I'm going to repost your code below with code tags.

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
#include "stdafx.h"
 #include <cmath>
 #include <iostream>
 #include <string>
 #include <sstream>
 #include <vector>
 #include <time.h>
 using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
 {
 int grid[10][8];
 int row;
 int col;
 int s = 0;
 
for (row = 0; row < 10; ++row)
 {
 for (col = 0; col < 8; ++col)
 {
 grid[row][col] = ++s
 cout << grid[row][col] << " ";
 }
 cout << endl;
 }
 }
 


ok so i notice that you have too many include directives, you need to remove those. you only need <iostream> and using namespcace std; I would get rid of all the junk inside your main paramater list.
Meaning change this
int _tmain(int argc, _TCHAR* argv[])
to this
int main()

Your main function needs to return an integer. So you need a statement like return 0; at the bottem of main. But your real problem here is that your not using your array properly.
These statements
1
2
 grid[row][col] = ++s
 cout << grid[row][col] << " ";

are equivilant to cout << ++s << " ";

Try initializing your array like this.

1
2
3
4
5
6
7
8
9
10
	char grid[10][8] = {{'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
	                   {'-', '-', 'H','H', 'H', 'H', '-','-'},
	                   {'-', '-', 'H','H', 'H', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
			   {'-', '-', 'H','-', '-', 'H', '-','-'},
			   {'-', '-', 'H','-', '-', 'H', '-','-'},
	                   {'-', '-', 'H','-', '-', 'H', '-','-'},};
Last edited on
Thank you for help! So i have edited my project but am still having errors by the intializing array did you mean to do this?
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
char grid[10][8] = {{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','H', 'H', 'H', '-','-'},
{'-', '-', 'H','H', 'H', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},
{'-', '-', 'H','-', '-', 'H', '-','-'},};

cout<< ++s << " ";
}
cout << endl;
return 0
}
Sorry used code tags wrong. I mean like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
  char grid[10][8] = {{'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
	                   {'-', '-', 'H','H', 'H', 'H', '-','-'},
	                   {'-', '-', 'H','H', 'H', 'H', '-','-'},
                           {'-', '-', 'H','-', '-', 'H', '-','-'},
			   {'-', '-', 'H','-', '-', 'H', '-','-'},
			   {'-', '-', 'H','-', '-', 'H', '-','-'},
	                   {'-', '-', 'H','-', '-', 'H', '-','-'},};

			cout<< ++s << " ";
			}
		cout << endl;
return 0
}
On line 19 you have an extra brace }, and on line 21 you need a ; after return 0


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
#include <iostream>
using namespace std;

int main()
{
	char grid[10][8] = {{'-', '-', 'H','-', '-', 'H', '-','-'},
                            {'-', '-', 'H','-', '-', 'H', '-','-'},
		            {'-', '-', 'H','-', '-', 'H', '-','-'},
		            {'-', '-', 'H','-', '-', 'H', '-','-'},
		            {'-', '-', 'H','H', 'H', 'H', '-','-'},
		            {'-', '-', 'H','H', 'H', 'H', '-','-'},
		            {'-', '-', 'H','-', '-', 'H', '-','-'},
		            {'-', '-', 'H','-', '-', 'H', '-','-'},
		            {'-', '-', 'H','-', '-', 'H', '-','-'},
	                    {'-', '-', 'H','-', '-', 'H', '-','-'},};
				

	int row;
	int col;
	

	for (row = 0; row < 10; ++row)
	{
		for (col = 0; col < 8; col++)
		{
				
				cout << grid[row][col] << " ";
		}
		
		cout  << endl;
	}

	cin.ignore();
	return 0;
}
Last edited on
Oh yeah it works! That creates my 10x8 grid! But now im stuck on the last part. I need to be able to enter in 4 numbers, a (top, bottom, right side, left side) where the rest of the grid will average out. So a given point (row-, column-) will give me a number that is averaged by the surrounding numbers. Do you happen know how to do that?
That seems confusing but here is my project assignment. Ive been able to understand all of our projects but this one is way beyond our knowledge. If i fail this class i get dropped from the university i just got accepted to. Thank you so much for your help!!! So much appreciated!

Problem Statement-
The temperature distribution in a thin metal plate with constant temperatures on each side can be modeled using a two-dimensional grid, as show in the figure.


Assume that the temperature of an interior point can be computed as the average of the four adjacent temperatures. For example, the temperature for the square marked x in the figure would be the average of the temperatures for the four grey squares. When the temperature of an interior point changes, the temperatures of the points adjacent to it will also change.
Given that the temperatures for the four edges are constant, by repeatedly computing the temperatures for the interior points as the average of the four surrounding temperatures, eventually equilibrium will be achieved and the temperatures will become constant.
Assignment
Write a program to model the temperature distribution for a grid. Start by using a small 10 row by 8 column grid (as shown in the figure). Also try 9 by 8 for working with the test arrays. You should use constants for the grid dimensions so we can later increase it to 30 by 30 or even as large as 1,000 by 1,000.
1. The user will input the temperatures for the four sides. The temperatures will be floating point numbers (doubles) between 0 and 9999.99 degrees (Kelvin, not that it matters). For consistency the fixed temperatures for the sides should be entered starting at the top of the grid and moving clockwise.
2. The user may also input the co-ordinates and temperatures for two points on the interior of the grid. The user should enter x, y co-ordinates and a temperature for each point. Your program should handle any number of fixed temperature interior points.
3. Initialize an array with the temperatures of the four edges (as shown in the figure), and any fixed points the user enters and zero the remainder of the array.
4. Repeatedly compute the temperature for the interior points in the grid by averaging the temperatures in the four adjacent cells. Repeat this until the temperature in any cell does not change by more than 0.001 degree—tolerance or accuracy level.
5. Output the contents of the array to a file in a nice, columnar format using Output formatting.
a. You should be able to handle a variable number of row and columns.
b. Print a column number header and row numbers on the left.
c. Print a line identifying the fixed temperature interior points.

Topic archived. No new replies allowed.