So what I am trying to do with my code is make a 2D array with dimensions 6 X 8.
ID: 3642054 • Letter: S
Question
So what I am trying to do with my code is make a 2D array with dimensions 6 X 8. Then I want the array to start out with the double value "0.0" in each corner. While the top, bottom, left, and right row is filled with a user desired double value. Then, whatever is left is filled as "0.0". What I don't understand is why I get 0 for all my values and if I don't declare the array in the main() I get all decimal values with exponential value which is obviously wrong.#include <iostream>
#include <iomanip> //Needed for setw().
using namespace std;
//Global varaibles.
const int N=6,K=8;
//Function prototypes.
void setup_table(double table[N][K],double top, double right,
double bottom,double left)
{
table[0][0]= 0;
table[0][8]= 0;
table[6][0]= 0;
table[6][8]= 0;
for (int e=1; e<K; e++)
{
table [0][e] = top;
}
for (int f=1; f<N; f++)
{
table [f][6] = right;
}
for (int g=1; g<K; g++)
{
table [4][g] = bottom;
}
for (int h=1; h<N; h++)
{
table[h][0] = left;
}
for (int i=1; i<N; i++)
{
for(int j=1; j<K; j++)
{
table[i][j] = 0.0;
}
}
}
int display(double table[N][K])
{
for(int i=0; i<N; i++)
{
for(int j=0; j<K; j++)
{
cout << setw(4)<< table[N][K];
}
cout << endl;
}
}
int main()
{
double grid [N][K];
double top, right, bottom, left; //Variables to store initial temps.
cout << "Enter initial temperatures (top, right, bottom, left): ";
cin >> top >> right >> bottom >> left;
setup_table(grid,top,right,bottom,left);
//Initializing the array.
display(grid);
double tolerance; //Variable to store the data for tolerance.
cout << "Enter tolerance for equilibrium: ";
cin >> tolerance;
return 0;
}
Explanation / Answer
The reason you were seeing so many exponents is because of the line in your display function: "coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.