### how do i connect my array functions?

so this is my goal im trying to create 3 matrix functions, but i am also trying to create another function that shows the numbers of each matrix.
when I run my code it does what i want but it only outputs one grid (matrix1 im guessing) and the its a mess of numbers.
im trying to achieve inputting all the numbers for each matrix and then i want the program to show me the results of each matrix.here is my code so far:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````using namespace std; int matrix1(); int matrix2(); int matrix3(); void result(); int main() { matrix1(); matrix2(); matrix3(); result(); system("pause"); return 0; } int matrix1() { int row,column; int arr[3][3]; cout << endl; cout << "matrix1" << endl; for (row = 0; row < 3; row++) for (column = 0; column < 3; column++) { std::cout << "ENTER NUMBER " << (column + 1) + 3 * (row) << " : "; std::cin >> arr[row][column]; } return 0; } int matrix2() { int row,column; int arr[3][3]; cout << endl; cout << "matrix2" << endl; for (row = 0; row < 3; row++) for (column = 0; column < 3; column++) { std::cout << "ENTER NUMBER " << (column + 1) + 3 * (row) << " : "; std::cin >> arr[row][column]; } return 0; } int matrix3() { int row,column; int arr[3][3]; cout << endl; cout << "matrix3" << endl; for (row = 0; row < 3; row++) for (column = 0; column < 3; column++) { std::cout << "ENTER NUMBER " << (column + 1) + 3 * (row) << " : "; std::cin >> arr[row][column]; } return 0; } void result() { int row, column; int arr[3][3]; for (row = 0; row < 3; row++) { for (column = 0; column < 3; column++) { std::cout << arr[row][column] << " "; } std::cout << std::endl; } std::cout << "\n\nand here you have a matrix\n"; }``````
http://www.cplusplus.com/doc/tutorial/arrays/
Chapter: "Arrays as parameters"
You are forgetting that each of `matrix()` functions creates a temporary array, which exist only as long as the function is running. Eg. in line 22 you define `int arr[3][3];`, you fill it with data but in line 32 the scope of arr ends, and the data are no longer stored in memory. Function `result()` cannot access them. It creates another temporary array, which happens to have the same name, but is in an entirely different part of memory, and may be filled with garbage data.

You may consider using global array variables, or learn how to pass an array to functions.
Last edited on
what im trying to learn is how to pass arrays to functions. i read the tutorial on arrays as parameters but im not understanding it completely so then i studied the article on pointers and that confused me more. in class my prof. probably mentioned pointers but we never fully discussed it. so here is what i did different:

im only writing the void function

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````void result (arr1[][3][3]); int main { //remember i am only passing the input //of the arrays to the function. // i have already created 3 separate arrays inside my main. result (arr, 3); //this is how im trying to call the function //but it gives me an error. //it says that the argument is incompatible with //parameter it also says there is a syntax error with "]" //rest of my code } void result(); { int row,column;//im guessing i dont need to declare these //but because i havent passed the array to the function from main //i just left it for now int arr[3][3];//" for (row = 0; row < 3; row++) { for (column = 0; column < 3; column++) { std::cout << arr[row][column] << " "; } } } std::cout << std::endl; std::cout << "\n\nand here you have a matrix\n"; }``````
Last edited on
maybe an example will help

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include using namespace std; void fillArray(int myFoo[][10], int row, int col); void printArray(int myFoo[][10], int row, int col); int main() { const int row = 5; const int col = 10; int foo[row][col]; fillArray(foo, row, col); printArray(foo, row, col); cin.ignore(); return 0; } //fill array void fillArray(int myFoo[][10], int row, int col) { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { myFoo[i][j] = 0; } } }//end of fillArray //print array void printArray(int myFoo[][10], int row, int col) { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { cout << myFoo[i][j]; } cout << endl; } }//end of printArray ``````
the problem im having is getting the input from the user for each matrix (matrix1, matrix 2 and matrix 3) and then calling a function that shows the input numbers for each matrix. example:

matrix1
enter num 1 in row 1 //user input
enter num 2 in row 1 //user input
enter num 3 in row 1//user input

enter num 1 in row 2//user input
enter num 2 in row 2//user input
enter num 3 in row 2//user input

enter num 1 in row 3//user input
enter num 2 in row 3//user input
enter num 3 in row 3//user input

matrix2
enter num 1 in row 1//user input
enter num 2 in row 1//user input
enter num 3 in row 1//user input

enter num 1 in row 2//user input
enter num 2 in row 2//user input
enter num 3 in row 2//user input

enter num 1 in row 3//user input
enter num 2 in row 3//user input
enter num 3 in row 3//user input

.....etc

matrix 1

11 3 45
18 15 44
12 15 10

matrix2

14 15 88
99 98 74
16 54 1
im confused because the numbers i want to
show are random numbers input by the user
and then i want to print out the numbers
i dont know how to associate the input numbers by the users
of an array to a function that shows the results
at the end of the program

Last edited on
OK, so i think im making progress but still not sure. For the first part i finally achieved what i was trying to do. where the program asks you for the numbers (input) for matrix 1 and then asks you for the input on matrix 2. although there are no errors in the written code when i execute it i get the error when i try to print out both matrices im still not grasping how to associate the input numbers with the final result. here is what i have so far:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include "stdafx.h" #include using namespace std; void printarray1(int arrays[][3]) { int row = 0; int column; cout << endl; cout << "Matrix 1\n" << endl; for (row = 1; row < 4; row++) for (column = 1; column < 4; column++) { while(row < 4) { int column = 1; while(column< 4) { cout<<"enter number "<> arrays[row][column]; column++; } cout<<"\n"; row++; } } } void printarray2(int arrays[][3]) { int row = 0; int column; cout << endl; cout << "Matrix 2\n" << endl; for (row = 1; row < 4; row++) for (column = 1; column < 4; column++) { while(row < 4) { int column = 1; while(column< 4) { cout<<"enter number "<> arrays[row][column]; column++; } cout<<"\n"; row++; } } } void printarrayfinal(int arrays[][3]) { int row,column, matrix; for (matrix = 1; matrix<2; matrix++) { cout << "Matrix " << matrix <
Last edited on
Topic archived. No new replies allowed.