2D Array Problem Full Code, Compiles, But Doesn't Work :(

Hello All - First post here

Help would be MUCH appreciated!

I have a problem for my Computation in Physics/Astronomy Course. We are learning c++, and have to answer the following problem (click the link below). The original problem is for Fortran 95, but we already solved it for Fortran and now are converting it to C++.

Problem:

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CD0QFjAD&url=http%3A%2F%2Fstudentoffortune.com%2Fquestion%2F1489383%2FTranslate-FORTRAN-code-to-CInstruct%2F2504407-Must%2520translate%2520this%2520FORTRAN%2520code%2520to%2520C.docx&ei=YUKfT-HmN-3H6AHTpcmlAg&usg=AFQjCNGFAss2hl7kn5CaKmldLnFJsJEnfg

My Attempted Solution: Compiles, but doesn't give proper output (at all!)

I had my window with the terminal in it set really wide. Sorry for the mess. If you copy and paste it into your terminal and make it nice and wide it should read very nicely.


//-----------------------------------------------------------------------------------------------------------------
//Purpose: Calculates the steady-state temperature distribution throughout a metallic plate divided into 100 small
// segments.
//
// Initial conditions are such that the outer edges of the plate are a constant 20 deg C, and position
// (3,8) is a constant 100 deg C. All other segments begin at 50 deg C and vary.
//
// Also displays temperature at (5,5) after a steady-state solution has been found.
//
//Author: Anthony Pecchillo
//Date: April 29th, 2012
//Class: PHY277
//-----------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <cmath>
using namespace std;

const int nNumRow = 10; //Number of Rows in Grid |This way we can test different
const int nNumCol = 10; //Number of Columns in Grid |size grids!

double maxx(double delt[][nNumCol], int nNumRow); //Declares Function to find Max Value of 2D Array

int main() //Beginning of Main Function
{
int nRow, nCol; //Index Variables
double delt[nNumRow][nNumCol]; //Array of Temp Differences
double oldTemp[nNumRow][nNumCol]; //Array of Preveious Temps
double temp[nNumRow][nNumCol] = //Declare and Initialize Temperature of Each Segment in Grid of Array of Temps
{
{ 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 100.0, 50.0, 20.0}, //Notice position (3,8) is 100 deg C
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 20.0 },
{ 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0 }
};

//Iterate Temps
for (int count = 0; maxx(delt, nNumRow) < 0.01; count++) //If MaxVal of AbsVal of Temp Diffs < 0.01
{
//Set oldTemp = temp
for (int m = 0; m < nNumRow; m++)
{
for (int n = 0; n < nNumCol; n++)
{
oldTemp[m][n] = temp[m][n];
}
}
//Iterate Temps for All Variable Segments
for (int nRow = 1; nRow < (nNumRow - 1); nRow++) //(nNumRow - 1) because grid in C++ goes from 0 to 9
{
for (int nCol = 1; nCol < (nNumCol - 1); nCol++) //(nNumCol - 1) because grid in C++ goes from 0 to 9
{
if (nRow != 2 && nCol != 7) //Again, (2,7) in C++ is same as (3,8) in Fortran
temp[nRow][nCol] = 0.25 * (temp[nRow+1][nCol] + temp[nRow-1][nCol] + temp[nRow][nCol+1] + temp[nRow][nCol-1]);
} // ^^^^^^^^Formula to Adjust Temp of Each Segment
}
//Set New Values for All Elements of "delt" Array
for (int o = 0; o < nNumRow; o++)
{
for (int p = 0; p < nNumCol; p++)
{
delt[o][p] = temp[o][p] - oldTemp[o][p];
}
}

if (maxx(delt, nNumRow) < 0.01) //If MaxVal of Temp Diffs < 0.01
cout << "We have converged in" << count << "iterations!" << endl;
for (int nRow = 0; nRow < 10; nRow++)
{
for (int nCol = 0; nCol < 10; nCol++)
{
cout << temp[nRow][nCol] << " "; //Displays Steady-State Temp Distribution to User
}
cout << endl; //Line Break
}
break; //Breaks For Loop (The one incrementing the variabls "count")
}

//Display Value of Temperature at (5,5) to the User
cout << "The final temperature at (5,5) is:" << temp[4][4] << "degrees C" << endl;

return(0);
} //End of Main Function

double maxx(double delt[][nNumCol], int nNumRow) //Function Definition to find Max Value of 2D Array
{
int i, j; //Index Variables
double mymax; //Ultimately Becomes Max Element in Array

for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
if (fabs(delt[i][j]) > mymax)
mymax = fabs(delt[i][j]);
}
return mymax;
}


use code tags please, much easier to read
There seem to be braces missing in maxx().

Are they there in your original code?

Aside from that mymax is not initialized in maxx() so result of (fabs(delt[i][j]) > mymax) is unpredictable.

delt[] doesn't hold sensible values when you first pass it to maxx it in this line.

for (int count = 0; maxx(delt, nNumRow) < 0.01; count++) //If MaxVal of AbsVal of Temp Diffs < 0.01

change that line to while(true) to loop until you hit break;
You will have to declare count and increment it explicitly if you do that.

Move the break to this if
1
2
3
4
5
if (maxx(delt, nNumRow) < 0.01) //If MaxVal of Temp Diffs < 0.01
{
cout << "We have converged in" << count << "iterations!" << endl;
break;
}


and move the if to the end of the while loop so you display the final state.

You will need to rethink this logic.
if (nRow != 2 && nCol != 7)

This evaluates to false for every segment in nRow == 2 and every segment in nCol == 7.

What is the answer supposed to be? 29.89?
Last edited on
Topic archived. No new replies allowed.