0

The following is the code, i have tried al the other styles mentioned in the comments one by one but none worked to remove the garbage value, Although "jugaad" works but it's not performance wise good. Couldn't figure out what's wrong !

#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
//    Other styles
//    int data[n+1][m+1] = {0,0};
//    int data[n+1][m+1] = {0};
//    int data[n+1][m+1] = {{0,0}};
int data[n][m] = {0};
//    Jugaad start:
for(int i=0;i<m;i++){
    data[0][i] = 0;
}
// Jugaad end
cout<<"\n";
    for(int x=0;x<n;x++){
        for(int y=0;y<m;y++){
            cout<<data[x][y]<<"\t";
        }
        cout<<"\n";
    }
cout<<"\n";
return 0;
}    

Screenshot of code and output

2

1 Answer 1

0
for(int i=0;i<n;i++)
{
    for(int j=0;j<n;j++)
      {
         data[i][j] = 0;
      }
 ``}

This will set all values of your array to zero

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but its not good performance-wise when its about competitive progrraming, and why : int arr[n][m] = {0}; not working ?
@PrashantRawat int arr[n][m] = {0}; is not valid C++. The size of an array must be known at compile time. Therefore there are no rules for this in C++. If your compiler supports it, you should check the documentation of your compiler. We can't help without knowing the compiler and compiler version.
The following article will be helpful for you : stackoverflow.com/questions/16064820/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.