C++ 根据用户输入排序并打印二维数组
我已经创建了一个2d数组程序,我正试图根据用户的选择打印出数组 这是数组C++ 根据用户输入排序并打印二维数组,c++,arrays,C++,Arrays,我已经创建了一个2d数组程序,我正试图根据用户的选择打印出数组 这是数组 1 2 3 1a b c 2d e f 3g h i 如果用户输入1b3,则会显示 adgbehcfi 如果输入c21,它将显示 cfibehadg 现在我已经创建了数组,但是我一直在研究如何根据用户输入打印出数组的顺序,请帮助我。多谢各位 在花了一天的时间之后,下面是我的代码 #include <iostream>; using namespace std; int main() { strin
1 2 3
1a b c
2d e f
3g h i
如果用户输入1b3,则会显示
adgbehcfi
如果输入c21,它将显示
cfibehadg
现在我已经创建了数组,但是我一直在研究如何根据用户输入打印出数组的顺序,请帮助我。多谢各位
在花了一天的时间之后,下面是我的代码
#include <iostream>;
using namespace std;
int main()
{
string alphabate;
string array[3][3];
string a="abcdefghi";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
array[i][j] = a[j+ (i * 3)];
}
}
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout << array[i][j];
}
cout << endl;
}
cout << "Enter some alphabate:";
cin >>alphabate;
return 0;
}
#包括;
使用名称空间std;
int main()
{
字符串字母;
字符串数组[3][3];
字符串a=“abcdefghi”;
对于(int i=0;i据我所知,您希望仅基于用户输入的列号/索引打印数组元素。这就是我如何实现的。希望这有帮助:)
#包括
#包括
使用名称空间std;
字符串数组[3][3];
//函数,用于打印数组的列c
void打印数组(int c){
对于(int i=0;我可以详细说明1b1和C21)它们指的是第一列是1,第二列是b,因为b是字母顺序的第二列,后跟第三列。我犯了一个错误,是1b3“我一直在学习如何读取数组”看来你已经知道cin了,那有什么问题吗?@Happy In 1b3,1表示第1列,b表示第2列,3表示第3列。如果我错了,请纠正我。@Sumeet Singh是的,你是对的。谢谢你的帮助,但是我花了一些时间试图解决另外两件事。我在问题中更新了它们。再次感谢你。我可以ld更新我的问题,所以我将在这里发布。第一件事是我如何使我的程序更智能,比如使cin>>alphabate更符合逻辑,比如接受1234,拒绝不符合顺序的输入,比如1(缺少2)34.另一件事是如何让我的程序接受10个字的数组。我注意到如果我输入helloworld,密码文本会丢失2d数组中的字母d。再次感谢你的帮助。谢谢你嗨,我忘了添加,我确实将数组设置为数组[5][5]并最终为unix环境中缺少的字段获取垃圾值。
#include <iostream>
#include <string>
using namespace std;
string array[3][3];
//function that prints the column c of the array
void printArray(int c) {
for(int i=0; i<3; i++) {
cout << array[i][c];
}
}
int main() {
string alphabate;
string a="abcdefghi";
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
array[i][j] = a[j+ (i * 3)];
}
}
cout << "Enter some alphabate:";
cin >> alphabate;
//checking the input parameters
for (int j=0; j<3; j++) {
if (alphabate[j] == '1' || alphabate[j] == 'a') {
printArray(0);
}
else if (alphabate[j] == '2' || alphabate[j] == 'b') {
printArray(1);
}
else if (alphabate[j] == '3' || alphabate[j] == 'c') {
printArray(2);
}
else {
cout << "Error";
}
}
return 0;
}