0

When I enter 2 values the third one is garbage..Don't Know why..

#include<iostream>
#include "Size_Decider.h"

using namespace std;

int main()
{
    int size=0,Encoded_Matrix_Column=1;
    cout<<"Enter The Number of Digits ";
    cin>>size;

    int *Digits= NULL;
    Digits = new int[size];

    Encoded_Matrix_Column = matrix_size_checker(size,Encoded_Matrix_Column);
    cout<<"It Has "<<Encoded_Matrix_Column<<" Columns"<<endl;
    cout<<"It Hass 3 Rows"<<endl;

    //Intilizing every point in 1D array with NULL
    for(int i=0;i<size;i++)
    {
        Digits[i] = NULL;
    }

    // Input fro the user to enter the Digits for Further Process 
    for(int i=0;i<size;i++)
    {
        cout<<"Enter The Digit "<<i+1<<"   ";
        cin>>Digits[i];
    }

    int **Encoded_Matrix = NULL,u=0;
    Encoded_Matrix = new int*[3];

    //Allocating memory to every point in 2D array
    for(int i=0;i * Encoded_Matrix_Column <size;i++)
    {
        Encoded_Matrix[i]= new int[Encoded_Matrix_Column];
    }


    //Intilizing every point in 2D array with NULL
    for(int i=0;i<Encoded_Matrix_Column;i++)
    {
        for(int j=0;j<3;j++)
        {
            Encoded_Matrix[i][j] = NULL;
        }
    }



    //Coping the User input into 2D array
    for(int i=0;i<Encoded_Matrix_Column;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(Digits[u]==NULL)
            {
                Encoded_Matrix[i][j] = 0 ;
            }
            else
            {
                Encoded_Matrix[i][j] = Digits[u];
            }
            u++;
        }
    }

    //Output of 2D Array
    for(int i=0;i<Encoded_Matrix_Column;i++)
    {
        for(int j=0;j<3;j++)
        {
            cout<<Encoded_Matrix[i][j]<<"  " ;
        }
        cout<<endl;
    }
    system("pause");
}
2
Contributors
1
Reply
6
Views
10 Months
Discussion Span
Last Post by AssertNull
0

When I enter 2 values the third one is garbage..

Not sure what you mean here. You're entering two values. What is the "third one" then? We cannot compile this since we do not have the file "Size_Decider.h".

Line 22 - you are initializing integers to NULL. NULL is defined as 0, but you should initialize your integers to 0, not NULL. NULL is used to initialize a POINTER to not point anywhere. The result is the same, but when you read code and you see NULL, you are expecting it to be an address, not an integer.

http://en.cppreference.com/w/cpp/types/NULL

Same with line 47.

Line 33:

Encoded_Matrix = new int*[3];

Line 36:

for(int i=0;i * Encoded_Matrix_Column <size;i++)

Shouldn't the middle term: i * Encoded_Matrix_Column <size bei < 3 in order to match line 33?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.