代碼可以在這里運行/編譯https://onlinegdb.com/9yhzLeVu3
如果在 2x2 矩陣上進行測驗,此代碼只會為所有值列印值 4
[1][2]
[3][4]
我相信它與 cout 宣告,我相當確信它正在將值保存到矩陣中,但是,我沒有看到它正確列印。
如果可以請告訴我我沒有看到什么?這種語法對我來說很新,這個程式的功能是函式式編程,但是一旦我開始將它轉換為方法/函式就很難了。
#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://stackoverflow.com/questions/2828648/how-to-pass-a-multidimensional-array-to-a-function-in-c-and-c
// https://www.techiedelight.com/pass-2d-array-function-parameter-cpp/
// https://www.tutorialspoint.com/Passing-two-dimensional-array-to-a-Cplusplus-function
// https://stackoverflow.com/questions/71907069/my-print-function-does-not-print-correctly-when-passing-a-2d-array-please-tell/71907317#71907317
// 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;
}
uj5u.com熱心網友回復:
你認為這些行會做什么:
matrix[size * size] = edgeYesOrNo;
cout << matrix[size * size] << " ";
我發現你rowIndex * size colIndex在這兩個地方都需要它。
我將解釋其中的區別。執行此操作時,您已經分配了陣列:
int *matrix = new int[size * size];
這不是二維陣列。它是一維陣列。但沒關系。如果 size 為 4,size * size則為 16,因此您為 16 個整數分配了空間。
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 實際上沒有二維陣列。你可以模擬它。這是這樣做的一種方法。
您所做的是每次使用(對于 size == 4)第 16 個位置,這實際上是最后一個位置。你踩著誰知道什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462788.html
