Help getting the right output for a function

Hello,

I am having trouble getting the right output for one of my functions. I am able to compute the correct average for the first function, but for my second my answer is off by a few numbers.

I really just want to understand what I am doing wrong, and what I can do to fix it.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

const int SIZE = 10; // 10 original
const double CHANGE = 0.1;


void Initial(double hot_plate[][SIZE])
{
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
if (row == 0 || row == 9) // row == 9 original
{
if (col == 0 || col == 9) // row == 0 original
{
hot_plate[row][col] = 0;
}
else
{
hot_plate[row][col] = 100;
}
}
else
{
hot_plate[row][col] = 0;
}
}
}
}

void Print(double hot_plate[][SIZE])
{
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
cout << fixed << setprecision(4) << setw(10) << hot_plate[row][col];
if (col < SIZE - 1) {
cout << ",";
}
}
cout << endl;
}
}

void FirstAverage(double hot_plate[][SIZE], double copy_hot_plate[][SIZE])
{
const double NEIGHBORS = 4.0;
double average_temp = 0.0;
int row = 0;
int col = 0;

for (int row = 1; row < SIZE - 1; row++)
{
for (int col = 1; col < SIZE - 1; col++)
{
average_temp = (hot_plate[row][col - 1] + hot_plate[row + 1][col] + hot_plate[row][col + 1] + hot_plate[row - 1][col]) / NEIGHBORS;
copy_hot_plate[row][col] = average_temp;

}

}




}






void MyAverage(double hot_plate[][SIZE])// This is the function I am having trouble computing the right average.
{
const double NEIGHBORS = 4.0;
double average_temp = 0.0;
double largest_change = 0.0;

do
{
largest_change = 0;
for (int row = 1; row < SIZE - 1; row++)
{
for (int col = 1; col < SIZE - 1; col++)
{
double old_value = hot_plate[row][col];
average_temp = (hot_plate[row][col - 1] + hot_plate[row + 1][col] + hot_plate[row][col + 1] + hot_plate[row - 1][col]) / NEIGHBORS;
hot_plate[row][col] = average_temp;

double change = hot_plate[row][col] - old_value;

if (change > largest_change)
{
largest_change = change;
}
}
}


} while (largest_change > CHANGE);

}

void SaveFile(double hot_plate[][SIZE])
{
ofstream out;
out.open("Hotplate.csv");

for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
out << hot_plate[row][col];
if (col < SIZE - 1) {
out << ",";
}

}
out << endl;
}
}

int main()
{
double hot_plate[SIZE][SIZE];
double copy_hot_plate[SIZE][SIZE];

cout << "Hotplate simulator" << endl << endl;
cout << "Printing initial plate..." << endl;
Initial(hot_plate);
Initial(copy_hot_plate);
Print(hot_plate);

cout << endl << endl;

cout << "Printing plate after one iteration..." << endl;
FirstAverage(hot_plate, copy_hot_plate);
Print(copy_hot_plate);

cout << endl << endl;

cout << "Printing final plate..." << endl;
MyAverage(hot_plate);
Print(hot_plate);

cout << endl << "Outputting final plate to file \"Hotplate.csv\"" << endl;
SaveFile(hot_plate);


system("pause");
return 0;
}[/code]
Seems to me that you're making a program that simulates a hot plate.

First I'd take out the idea of taking the average of the current position into another function
1
2
3
4
5
6
7
8
9
10
11
12
double Average4(double a, double b, double c, double d) {
    return (a + b + c + d)/4.0;
}

double NeighborAverage(double hot_plate[SIZE][SIZE], int row, int col) {
    return Average4(
        hot_plate[row][col - 1], 
        hot_plate[row + 1][col], 
        hot_plate[row][col + 1], 
        hot_plate[row - 1][col]
    );
}

Now call this function in those parts where its asking to calculate the average.
average = NeighborAverage(hot_plate, row, col);

This should make the actual calculation repeatable and printable. But I think the real error is here.
double change = hot_plate[row][col] - old_value;
Knowing that all parts of the plate start at 100 except the edges that start at 0, the new average for each position should be lower than the previous for the first iteration. Meaning you'll get a negative change everywhere. Meaning that change value should be negative.
making this largest_change > CHANGE probably not be what you'd like since change is positive 0.1. You'd probably prefer std::abs(largest_change) > CHANGE as your do-while loop exit condition.

Finally know that floating point numbers are actually not exact. Due to the way the computer calculates floating point numbers it may have a few errors that can be better reasoned about and ignored by printing out the entire value.
Topic archived. No new replies allowed.