That is my code can you modify it using C++ so that in the end a functions is be
ID: 3764058 • Letter: T
Question
That is my code can you modify it using C++ so that in the end a functions is being called upon to output the new arrays at the bottom instead
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void printArray(int arr[][21], int rows, int cols);
int main() {
int rows, cols;
cout << fixed << setprecision(1);
cout << "Enter the number of rows and columns of the array: ";
cin >> rows;
cin >> cols;
while ((rows > 35 || cols > 20)) {
cout << "Invalid dimensions. ";
cout << "Enter the number of rows and columns of the array: ";
cin >> rows >> cols;
}
double arr[35][20] = { 0 };
double epsilon;
cout << "please enter the value of epsilon";
cin >> epsilon;
double top, bottom, left, right;
cout << "Enter the Top edge, bottom edge, left edge and right edge temperatures(in that order): ";
cin >> top >> bottom >> left >> right;
while ((top < 0 || top > 300 || bottom < 0 || bottom > 300 || left < 0 || left > 300 || right < 0 || right > 300)) {
cout << "All temperatures must be between 0 and 300. ";
cout << "Enter the Top edge, bottom edge, left edge and right edge temperatures: ";
cin >> top >> bottom >> left >> right;
}
for (int i = 1; i < cols - 1; i++) {
arr[0][i] = top;
arr[rows - 1][i] = bottom;
}
for (int i = 1; i < rows - 1; i++) {
arr[i][0] = left;
arr[i][cols - 1] = right;
}
arr[0][0] = (arr[0][1] + arr[1][0]) / 2;
arr[0][cols - 1] = (arr[1][cols - 1] + arr[0][cols - 2]) / 2;
arr[rows - 1][0] = (arr[rows - 1][1] + arr[rows - 2][0]) / 2;
arr[rows - 1][cols - 1] = (arr[rows - 2][cols - 1] + arr[rows - 1][cols - 2]) / 2;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] < 100) cout << " ";
if (arr[i][j] < 10) cout << " ";
cout << " " << arr[i][j];
}
cout << " ";
}
cout << " ";
int iterations = 0;
bool allEpsilon = false;
while (!allEpsilon)
{
allEpsilon = true;
for (int i = 1; i < rows - 1; i++) {
for (int j = 1; j < cols - 1; j++) {
double temp = arr[i][j];
arr[i][j] = (arr[i - 1][j] + arr[i][j - 1] + arr[i + 1][j] + arr[i][j + 1]) / 4.0;
if (fabs(arr[i][j] - temp) > epsilon) allEpsilon = false;
}
}
iterations++;
}
{
cout << "The plate reached equillibrium after " << iterations << " iterations ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] < 100) cout << " ";
if (arr[i][j] < 10) cout << " ";
cout << " " << arr[i][j];
}
cout << " ";
}
}
}
Explanation / Answer
Answer :
void printArray(int arr[][21], int rows, int cols)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] < 100) cout << " ";
if (arr[i][j] < 10) cout << " ";
cout << " " << arr[i][j];
}
cout << " ";
}
}
Call The Method As :
printArray(arr,35,20);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.