Home > Software engineering >  My print function does not print correctly when passing a 2d array, please tell me how to print all
My print function does not print correctly when passing a 2d array, please tell me how to print all

Time:04-18

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

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.

// C   Program to read a square matrix
// and print the main diagonal elements
#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

// 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[size * size] = 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[size * size] << "   ";
        }
        cout << endl;
    }
    
        return;
    }

CodePudding user response:

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.

CodePudding user response:

This is the corrected code after Joseph Larson's answer!

If people can explain to me the syntax a little more I'd be thrilled and for those that this may help, please take a look! <3

// C   Program to read a square matrix
// and print the main diagonal elements
#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

// 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;
    }
  • Related