0

code can be run/compiled here https://onlinegdb.com/9yhzLeVu3

this code will only print value 4 for all the values if tested on a 2x2 matrix

[1][2]

[3][4]

I believe its with the cout statement, I am fairly convinced it is saving the values to the matrix but, am not seeing it print correctly.

If could please tell me what I am not seeing? this syntax is very new to me, this program functions as functional programming but, is difficult once I start converting it to 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://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;
    }

1 Answers1

1

What do you think these lines are going to do:

        matrix[size * size] = edgeYesOrNo;                     
        cout << matrix[size * size] << "   ";

I find it far more likely you need rowIndex * size + colIndex in both places.

I'll explain the difference. You've already allocated your array when you did this:

int *matrix = new int[size * size];

This is not a 2D array. It's a single dimension array. But that's okay. If size is four, size * size is 16, so you allocated space for 16 integers.

 matrix[rowIndex * size + colIndex]

This is how I use it in both my places. What this means is you'll store them kind of like this:

[ 0][ 1][ 2][ 3]
[ 4][ 5][ 6][ 7]
[ 8][ 9][10][11]
[12][13][14][15]

That is, row 0, col 0 is thus in the 0'th index. row 1, col 1, is in the row * size + col location, or 5.

Does that make sense now?

C++ doesn't actually have 2D arrays. You can simulate it. This is one way to do so.

What you were doing was using (for size == 4) the 16th spot each time, which is actually one slot past the end. You were stepping on who knows what.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • Hi! Thank you for asking! My understanding is it creates the 2dimensional matrix dynamically like matrix[rowIndex][colIndex] thus a 2x2 matrix cout << matrix[size * size] I thought would print it accordingly! I will try your response and see what happens! – Robert Petersen Apr 18 '22 at 03:47
  • I am wondering though what this does in the previous sense it was to my understanding some sort of odd pointer notation the * an in this sense does that read the same way? Trying it now all the same! :D – Robert Petersen Apr 18 '22 at 03:51
  • Omgosh Thank you so much! This fixed it! This syntax is still really confusing to me so if you have more input I'd be happy to read it! Otherwise have a great evening :X thank you! – Robert Petersen Apr 18 '22 at 03:54
  • I think what is confusing to me about this still is I tried matrix[rowIndex * colIndex] an that printed no values so I am wondering what is the + sign in this case --- is it an interpolation or? :O so much nuance, sorry if too many questions / comments :X – Robert Petersen Apr 18 '22 at 04:01
  • 1
    I'll edit my answer. – Joseph Larson Apr 18 '22 at 21:49
  • It does make sense! the syntax of matrix[rowIndex * size + colIndex] I think I understand - I see it now as a mathematical syntax in this way rather than a pointer as I'd thought! this is interesting! It contradicts my original understanding of array[][] declaration an the idea of simulation gives determination that a declaration of array[] might be sufficient due to the usage of for loops to implement this simulation! Unless I am applying this second point of information/abstraction incorrectly then yes, I believe this makes sense is very clear! – Robert Petersen Apr 18 '22 at 23:03
  • Sometimes these things like a vector can be an array or a queue can be a stack - though I stare directly at them I don't see them quite right.. Like I don't want to admit it to myself but, I may believe the naming convention sometimes forgetting the logic behind such convention. Thank you very much! I see the syntax as math expression correlating to your example :) – Robert Petersen Apr 18 '22 at 23:06
  • I've updated this code to reflect this change and the implementation for doing so! Thank you very much again! – Robert Petersen Apr 18 '22 at 23:20