传递二维数组时,我的打印函数无法正确打印,请告诉我如何正确打印其所有值。这是我的语法吗?
My print function does not print correctly when passing a 2d array, please tell me how to print all its values correctly. Is it my syntax?
代码可以是run/compiled这里https://onlinegdb.com/9yhzLeVu3
如果在 2x2 矩阵上测试,此代码只会为所有值打印值 4
[1][2]
[3][4]
我相信它与 cout 语句有关,我相当相信它会将值保存到矩阵中,但是我没有看到它正确打印。
如果可以请告诉我我没有看到什么?这种语法对我来说很新,这个程序作为函数式编程运行,但是一旦我开始将它转换为 methods/functions.
就很困难
#include <iostream>
using namespace std;
// Create a matrix based graph representation.
// It will need to support the following operations.
// Ask the user how many points there are. DONE
// Ask the user to label those points, ie "ABC", "XYZ", "C12"... DONE
// Define the matrix as a square matrix (2 dimensional array) based on the number of points, also keep an array of the labels. DONE
// Repeatedly ask the user to define edges between two points. Add these edges to the matrix. DONE
// Have a list method that will list out all of the edges in the graph.
// REFERENCE
// https://www.geeksforgeeks.org/how-to-access-elements-of-a-square-matrix/
// https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/
// https://www.cplusplus.com/reference/utility/make_pair/
// https://www.tutorialspoint.com/cplusplus-program-to-implement-adjacency-matrix
//
// https://www.techiedelight.com/pass-2d-array-function-parameter-cpp/
// https://www.tutorialspoint.com/Passing-two-dimensional-array-to-a-Cplusplus-function
//
// one possible implementation would be make pair to track the label for edge cout statement for user input
// Code is formatted to be best read with labels the size of 3 this is hard coded per implementation requirements.
int main()
{
// PROTOTYPE
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size);
int size ;
cout << "How many points would you like this to be. Points meaning -size- of matrix: ";
cin >> size;
// example 2x2 matrix
// A B
// A [][]
// B [][]
// determine size, and modulo to create matrix form
string label; // labeling convention for determining assignments
string labelArray[size]; // label containing array to track for printing and labeling purposes
int edgeYesOrNo;
int counter = 0;
cout << "Will now ask to label the points of graph matrix.\n";
int *matrix = new int[size * size]; // this is how to define a 2d array
int rowIndex; // these are to access individual elements
int columnIndex; // ^^
for(int i=0; i<size; i++)
{
cout << "Enter label: "; // enter the label here to insure that there is no redundancy
cin >> label;
labelArray[i] = label;
}
// Get the square matrix
cout << "Enter 1 for edge 0 for no edge" << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << "Is there an edge to: " << labelArray[counter] << " and " << labelArray[columnIndex] << ": ";
cin >> edgeYesOrNo;
matrix[rowIndex * size + columnIndex] = edgeYesOrNo;
}
counter++;
}
printMatrix(labelArray, matrix, rowIndex, columnIndex, size);
delete[] matrix;
return 0;
}
// Display the matrix
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size)
{
cout << "The matrix is\n" << endl;
cout << " ";
for(int i=0; i<size; i++)
{
cout << labelArray[i] << " "; // To print the labels so its understandable
}
cout << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
cout << labelArray[rowIndex] << " ";
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << matrix[rowIndex * size + columnIndex] << " ";
}
cout << endl;
}
return;
}
你认为这些行会做什么:
matrix[size * size] = edgeYesOrNo;
cout << matrix[size * size] << " ";
我发现您更有可能在这两个地方都需要 rowIndex * size + colIndex
。
我来解释一下区别。当你这样做时你已经分配了你的数组:
int *matrix = new int[size * size];
这不是二维数组。它是一维数组。但没关系。如果大小为四,size * size
为 16,因此您为 16 个整数分配了 space。
matrix[rowIndex * size + colIndex]
这就是我在两个地方使用它的方式。这意味着您将像这样存储它们:
[ 0][ 1][ 2][ 3]
[ 4][ 5][ 6][ 7]
[ 8][ 9][10][11]
[12][13][14][15]
也就是说,第 0 行,第 0 列因此在第 0 个索引中。第 1 行第 1 列位于 row * size + col
位置,或 5.
现在明白了吗?
C++ 实际上没有二维数组。你可以模拟它。这是一种方法。
你所做的是每次都使用(对于大小 == 4)第 16 个位置,这实际上是最后一个位置。谁知道你在踩什么。
代码可以是run/compiled这里https://onlinegdb.com/9yhzLeVu3
如果在 2x2 矩阵上测试,此代码只会为所有值打印值 4
[1][2]
[3][4]
我相信它与 cout 语句有关,我相当相信它会将值保存到矩阵中,但是我没有看到它正确打印。
如果可以请告诉我我没有看到什么?这种语法对我来说很新,这个程序作为函数式编程运行,但是一旦我开始将它转换为 methods/functions.
就很困难#include <iostream>
using namespace std;
// Create a matrix based graph representation.
// It will need to support the following operations.
// Ask the user how many points there are. DONE
// Ask the user to label those points, ie "ABC", "XYZ", "C12"... DONE
// Define the matrix as a square matrix (2 dimensional array) based on the number of points, also keep an array of the labels. DONE
// Repeatedly ask the user to define edges between two points. Add these edges to the matrix. DONE
// Have a list method that will list out all of the edges in the graph.
// REFERENCE
// https://www.geeksforgeeks.org/how-to-access-elements-of-a-square-matrix/
// https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/
// https://www.cplusplus.com/reference/utility/make_pair/
// https://www.tutorialspoint.com/cplusplus-program-to-implement-adjacency-matrix
//
// https://www.techiedelight.com/pass-2d-array-function-parameter-cpp/
// https://www.tutorialspoint.com/Passing-two-dimensional-array-to-a-Cplusplus-function
//
// one possible implementation would be make pair to track the label for edge cout statement for user input
// Code is formatted to be best read with labels the size of 3 this is hard coded per implementation requirements.
int main()
{
// PROTOTYPE
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size);
int size ;
cout << "How many points would you like this to be. Points meaning -size- of matrix: ";
cin >> size;
// example 2x2 matrix
// A B
// A [][]
// B [][]
// determine size, and modulo to create matrix form
string label; // labeling convention for determining assignments
string labelArray[size]; // label containing array to track for printing and labeling purposes
int edgeYesOrNo;
int counter = 0;
cout << "Will now ask to label the points of graph matrix.\n";
int *matrix = new int[size * size]; // this is how to define a 2d array
int rowIndex; // these are to access individual elements
int columnIndex; // ^^
for(int i=0; i<size; i++)
{
cout << "Enter label: "; // enter the label here to insure that there is no redundancy
cin >> label;
labelArray[i] = label;
}
// Get the square matrix
cout << "Enter 1 for edge 0 for no edge" << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << "Is there an edge to: " << labelArray[counter] << " and " << labelArray[columnIndex] << ": ";
cin >> edgeYesOrNo;
matrix[rowIndex * size + columnIndex] = edgeYesOrNo;
}
counter++;
}
printMatrix(labelArray, matrix, rowIndex, columnIndex, size);
delete[] matrix;
return 0;
}
// Display the matrix
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size)
{
cout << "The matrix is\n" << endl;
cout << " ";
for(int i=0; i<size; i++)
{
cout << labelArray[i] << " "; // To print the labels so its understandable
}
cout << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
cout << labelArray[rowIndex] << " ";
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << matrix[rowIndex * size + columnIndex] << " ";
}
cout << endl;
}
return;
}
你认为这些行会做什么:
matrix[size * size] = edgeYesOrNo;
cout << matrix[size * size] << " ";
我发现您更有可能在这两个地方都需要 rowIndex * size + colIndex
。
我来解释一下区别。当你这样做时你已经分配了你的数组:
int *matrix = new int[size * size];
这不是二维数组。它是一维数组。但没关系。如果大小为四,size * size
为 16,因此您为 16 个整数分配了 space。
matrix[rowIndex * size + colIndex]
这就是我在两个地方使用它的方式。这意味着您将像这样存储它们:
[ 0][ 1][ 2][ 3]
[ 4][ 5][ 6][ 7]
[ 8][ 9][10][11]
[12][13][14][15]
也就是说,第 0 行,第 0 列因此在第 0 个索引中。第 1 行第 1 列位于 row * size + col
位置,或 5.
现在明白了吗?
C++ 实际上没有二维数组。你可以模拟它。这是一种方法。
你所做的是每次都使用(对于大小 == 4)第 16 个位置,这实际上是最后一个位置。谁知道你在踩什么。