c++ - The values are getting stored in "arr" correctly but garbage values are getting stored in "str". I do not understand why -


the values getting stored in "arr" correctly garbage values getting stored in "str". not understand why. checked many times , seems right me.

#include <iostream> using namespace std;  int main() {     int test,n,arr[50],str[20][20],i,j,k;     t = 0;     j = 0;     cin>>test; //test cases     while(test>0){         cin>>n; //number of elements in test case          for(i=0;i<n;i++)             cin>>arr[i]; //array of elements         for(i=0;i<n;i++){             cout<<"arr = "<<arr[i]<<"\n";             str[i][j] = arr[i]; // storing arr in str         }         cout<<"\n";         str[i+1][j] = 0;         j++;         test--;     }     for(i=0;i<j;i++){         for(k=0;str[i][k]!=0;k++)             cout<<<<str[i][k];  // printing str         cout<<"\n";     }     return 0; } 

there lot of mistakes main 1 indexing of 2d array

a 2 dimensional array arr [row] [column] , have been using arr [column] [row]

 str[i][j] = arr[i]; 

see u doing storing number @ every row

maybe image clear things out

enter image description here

#include <iostream> using std :: cin ; using std :: cout ;  using std :: endl ;  int main() {     int test,n,arr[50],str[20][20],i,j,k;     j = 0;     cin>>test; //test cases     int temp = test ;     while(test>0)     {         cin>>n; //number of elements in test case           for(i=0;i<n;i++)             cin>>arr[i]; //array of elements          for(i=0;i<n;i++)         {             cout<<"arr = "<<arr[i]<<"\n";             str[j][i] = arr[i]; // storing arr in str         }          str[j][i] = '\0';         cout<<"\n";          j++;         test--;     }      ( int = 0 ; < temp ; i++ )     {         j=0 ;     while ( str[i][j] != '\0')        {            cout<<str[i][j];  // printing str             j++ ;         }         cout<<"\n";     }     cin.ignore(5) ;     return 0; } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -