通过函数传递 2-D 数组后打印出来
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;
}
}
我正在将一个未知长度的数组传递到一个函数中。该函数将"A"到"Z"字符放入其中。现在在这个函数中,我正在使用我自己制作的另一个函数(之前测试过并且工作得很好)。现在,当我尝试打印数组时,它是这样的:
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
问题出在哪里?我是否正确初始化了数组?我试图在主代码中打印出来,但同样的问题。
您永远不会将count
重置为零,因此您的table[i][j]=array1[count];
运行时间远远超过array1
的末尾。
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 在一定长度后从数组中打印时缺少整数
- 数组元素打印的递归方法
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 打印矢量数组中的所有元素
- 打印问题.我在数组中打印值时遇到一些问题
- 数组类 阵列的打印输出
- 动态分配的聊天数组打印缺失的数据和空
- 如何打印 2D 字符数组C++
- 如何修改代码以打印出数组 v2
- 在C++中初始化和打印 2D 数组
- 将浮点数转换为无符号字符数组并打印出来
- C++ 打印包含数据结构的数组
- 如何让字符数组在一行而不是多行中打印出来?
- 打印字符数组地址的正确方法
- 按字母顺序打印存储在字符数组中的信息
- 使用递归反转和打印出数组的内容
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 如何在运行时在对象数组中动态追加新对象C++并打印它们
- 加载有空指针的字符数组的打印值