Pointing Array between functions
Hi everyone, I'm stuck here and I need help on how am I suppose to point the array to another function to edit.
This program is suppose to take in a matrix with side (NxN) and will output the matrix by adding the four nearby number together.
eg.
0 1 2
3 4 5
6 7 8
will output
8 12
20 24
The code works, but somehow at the second row, [1][0] it adds the new [0][0] and [0][1]...
Anybody would be kind enough to brainstorm with me?
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include<iostream>
#include<string>
#include<cstdlib>
#include <iomanip>
using namespace std;
int ask();
int initialize_matrix(int);
int is_positive_int(string);
int** iterative_calculate_result(int**, int);
/*********************************************************************
** Function: main()
** Description: The main function that contains other functions
** Parameters: -
** Pre-Conditions: -
** Post-Conditions: Runs the functions
*********************************************************************/
int main() {
int N;
N = ask();
initialize_matrix(N);
}
/*********************************************************************
** Function: ask()
** Description: Asks for N from user
** Parameters: None
** Pre-Conditions: main() is being run
** Post-Conditions: Returns N to main()
*********************************************************************/
int ask() {
string Nstr;
int pos, N;
cout << "Enter N for Matrix (N x N): " << endl;
cin >> Nstr;
pos = is_positive_int(Nstr);
if(pos == 1) {
N = atoi(Nstr.c_str());
return N; }
else {
cout << "Error try again!" << endl;
exit(1); }
}
/*********************************************************************
** Function: is_positive_int(string Nstr)
** Description: Takes the string from ask() to check whether positive
** Parameters: Nstr
** Pre-Conditions: Nstr
** Post-Conditions: returns is positive or not
*********************************************************************/
int is_positive_int(string Nstr) {
for(int i = 0; i < Nstr.size(); i++ )
{
if(Nstr.at(i) == '.' || Nstr.at(i) == '-')
return 0;
else
return 1;
}
}
/*********************************************************************
** Function: initialize_matrix(int N)
** Description: Takes input for matrix and prints the matrix
** Parameters: N
** Pre-Conditions: N is positive
** Post-Conditions: Prints the matrix
*********************************************************************/
int initialize_matrix(int N) {
int ROW = N, COL = N;
int **mat;
int k = 0;
mat = new int*[ROW];
for(int i = 0; i < ROW; i++)
mat[i] = new int[COL];
for(int i = 0; i < ROW; i++)
for(int j = 0; j < COL; j++)
mat[i][j] = k++;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cout << mat[i][j];
cout << " ";
}
cout << endl;
}
mat = iterative_calculate_result(mat, N);
}
/*********************************************************************
** Function: iterative_calculate_result(int** arr, int N)
** Description: Calculates the 4 nearest number in the matrix
** Parameters:
** Pre-Conditions:
** Post-Conditions:
*********************************************************************/
int** iterative_calculate_result(int** arr, int N) {
int a, b, c ,d;
int ** p;
p = arr;
for(int i = 0; i < N-1; i++) {
for(int j = 0; j < N-1; j++) {
a = arr[N-N][N-(N-j)];
j++;
b = arr[N-N][N-(N-j)];
j--;
i++;
c = arr[N-(N-i)][N-(N-j)];
j++;
d = arr[N-(N-i)][N-(N-j)];
j--;
i--;
arr[i][j] = a + b + c + d;
}
}
for(int i = 0; i < (N-1); i++) {
for(int j = 0; j < (N-1); j++) {
cout << arr[i][j];
cout << " ";
}
cout << endl;
}
return arr;
}
|
Last edited on
Topic archived. No new replies allowed.