Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why do i get random characters? (c++)

+1 vote
asked Oct 26 by Franco ACTIS (130 points)
hello, i'm trying to make a function that prints an entire 2d array in c++ i cant find what's wrong with the code and the program compiles successfully but when i run it i get random characters when it starts to print in a new row.

this is my code:

#include <iostream>
using namespace std;
int NewPrintColumn,NewPrintRow = 0;

void PrintEntireArray(char array[4][4],int MaxColumn,int MaxRow,int NewPrintColumn = 0,int NewPrintRow = 0)
{
    if (NewPrintRow == MaxRow)
    {
        NewPrintRow == 0;
    }
    if (NewPrintColumn-1 == MaxColumn)
    {
        cout << "\n";
        NewPrintColumn == 0;
        NewPrintRow++;
    }
    NewPrintColumn++;
    cout << "\t" << array[NewPrintColumn-1][NewPrintRow];
    //
    if (MaxColumn != NewPrintColumn-1 and MaxRow != NewPrintRow)
    {
        PrintEntireArray(array,MaxColumn,MaxRow,NewPrintColumn,NewPrintRow);
    }
}

int main()
{
    char hitmapp2[4][4]{
    {'x','x','x','x'},
    {'x','x','x','x'},
    {'x','x','x','x'},
    {'x','x','x','x'}};

    PrintEntireArray(hitmapp2,4,4);

    return 0;
}

2 Answers

0 votes
answered Oct 29 by Anon (180 points)

You tried to print an out of bounds array index and that's why it's a random character. Also some of your syntax is wrong. "and" should be "&&", "==" is comparing "=" is assign a new value. Also using recursive like this is a waste of system resources. Instead try this:

#include <iostream>

void PrintEntireArray(char array[4][4],int MaxColumn,int MaxRow)
{
    for(int r = 0; r < MaxRow; r++){   //This iterate through the rows
        for(int c = 0; c < MaxColumn; c++){   //Haha C++, also this iterate through the columns
            std::cout << array[r][c] << "\t";
        }
        std::cout << "\n";
    }
}

int main()
{
    char hitmapp2[4][4] = {
        {'1', '2', '3', '4'},
        {'5', '6', '7', '8'},
        {'9', 'a', 'b', 'c'},
        {'d', 'e', 'f', 'g'}
    };

    PrintEntireArray(hitmapp2,4,4);

    return 0;
}

Output:

1 2 3 4

5 6 7 8

9 a b c

d e f g
0 votes
answered Oct 30 by Peter Minarik (63,140 points)

You have multiple problems.

  1. NewPrintColumn == 0; is not an assignment, but a value comparison, you should use a single equal sign for the assignment.
  2. Your recursive function has the wrong logic. You need to increment the column and when the columns reach the maximum, you need to increment the row. Do this, as long as you haven't printed even the last row.

Your code fixed:

#include <iostream>

void PrintEntireArray(char array[4][4], int maxColumn, int maxRow, int column = 0, int row = 0)
{
    std::cout << "\t" << array[column++][row];
    if (column == maxColumn)
    {
        std::cout << std::endl;
        column = 0;
        row++;
    }
    if (row < maxRow)
    {
        PrintEntireArray(array, maxColumn, maxRow, column, row);
    }
}

int main()
{
    char hitmapp2[4][4]
    {
        {'x','x','x','x'},
        {'x','x','x','x'},
        {'x','x','x','x'},
        {'x','x','x','x'}
    };

    PrintEntireArray(hitmapp2, 4, 4);

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...