User14229754
User14229754

Reputation: 85

Printing out 2-D array after passing it through a function

void shiftString(int ,char stringinput[]);
void createTable(char table[][26]);
int main()
{
   char array[26][26]={1};  //initialize array
   createTable(array);   //function which takes 2-D array and feeds values into it
   return 0;
 }
 void shiftString(int n,char stringinput[])    
 {
  //this function shifts the characters in the string left or right by n.left if n is
    negative and vice versa.I have to use this to shuffle the characters 'A' to 'Z'
    in each row.
 }
 void createTable(char table[][26])  //this function creates a 26x26 matrix 
 {
     int n=0;
     int count=0;
     for (int i=0;i<26;i++)    //loop for row of matrix
     {   
          char array1[26]=  {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','W','Y','Z'};  //initializing 1-D array
      shiftString(n,array1);  //moves back every character by n. 
      for (int j=0;j<26;j++)//this loop equals one row of 2-D array to 1-D array
      {
          table[i][j]=array1[count];
          count++;
      }

       n--;  //everytime moving back of character increases by 1
   }
   for (int a=0;a<26;a++) //these two loops print out the array
   {
       for (int b=0;b<26;b++) //26 is the size of rows and columns
       {
           cout<<table[a][b]<<" ";
       }  
       cout<<endl;
   }
   }

I am passing an array with unknown length into a function.The function puts 'A' to 'Z' characters into it.Now in this function,i am using another function made by myself(tested before and works perfectly fine).Now when I try to print the array it is something like this:

    M N O P Q R S T U V X W Y Z      ü ¥ R · ô ? ~ ·
    h a ¨ ¿    ¤ ^ ¨ ¿  Ì | · w ù p · w ù p ·
    o · A B C D E F G H I J K L M N O P Q R S T U V X W
    Y Z      ü ¥ R · ô ? ~ · h a ¨ ¿    ¤ ^ ¨ ¿
    Ì | · w ù p · w ù p ·   o · A B C D E F G H I J
    K L M N O P Q R S T U V X W Y Z      ü ¥ R · ô ?
    ~ · h a ¨ ¿    ¤ ^ ¨ ¿  Ì | · w ù p · w ù p ·
    o · A B C D E F G H I J K L M N O P Q R S T U V

what is the problem?Am i initializing the arrays correctly?I tried to print it out in the main code but same problem.

Upvotes: 1

Views: 173

Answers (1)

nneonneo
nneonneo

Reputation: 179452

You never reset count to zero, so your table[i][j]=array1[count]; is running way past the end of array1.

Upvotes: 4

Related Questions