this code is supposed to be a person walking to right in a 4x4 array filled with '*'. for example: if the person walks to the right R ,row zero and walks one step it would look like this:
O * * *
* * * *
* * * *
* * * *
or row one, walks two steps
* * * *
* O * *
* * * *
* * * *
row three, walks four steps
* * * *
* * * *
* * * *
* * * O
这是原始代码:
#include <stdio.h>
int main ()
{
int i,k, c, f;
char z;
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}};
printf("Walking to the right\n");
z=getchar();
// modified array goes here []
if(z=='R')
{
printf("Row\n");
scanf("%d",&c);
printf("Steps");
scanf("%d",&f);
a[c][f]='O';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return ;
}
我想让R,行和步骤的值使用函数传递给int main,其中只打印上面的数组。
我尝试了一些事情,但我真的不知道从哪里开始。 :/
#include <stdio.h>
void print(char);
int main ()
{
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}, {'*','*','*','*'}};
printf("From the right\n");
z=getchar();
// modified array goes here []
void print(char z){
int i,k;
int c, f;
if(z=='R')
{
printf("Row\n");
scanf("%d",&c);
printf("Steps");
scanf("%d",&f);
a[c][f]='O';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return ;
}
#include <stdio.h>
int main ()
{
int i,k,x,c,f;
char z;
char a[4][4] = { {'*','*','*','*'}, {'*','*','*','*'},{'*','*','*','*'}, {'*','*','*','*'}};
printf("From the right\n");
z=getchar();
printf("Row\n");
scanf("%d",&c);
if(z=='R')
{
printf("Steps");
scanf("%d",&f);
a[c][f]='G';
for ( i = 0; i < 4; i++ ) {
for ( k = 0; k < 4; k++ ) {
printf("%c ", a[i][k] );
}
printf("\n");
}
}
return 0;
}
我会感激任何建议。感谢。
答案 0 :(得分:0)
不清楚但是看看这个:
void getCommand(char &z) {
printf("Walking to the right\n");
z = getchar();
}
void getRowAndSteps(char z, int &c, int &f) {
if (z == 'R')
{
printf("Row\n");
scanf("%d", &c);
printf("Steps");
scanf("%d", &f);
}//else...
}
void print(char a[][4]) {
for (size_t i = 0; i < 4; i++) {
for (size_t k = 0; k < 4; k++) {
printf("%c ", a[i][k]);
}
printf("\n");
}
}
int main()
{
int c, f;
char z;
char a[4][4] = { { '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' } };
getCommand(z);
getRowAndSteps(z, c, f);
a[c][f] = 'O';
//maybe some cycle here for each step ??
print(a);
return 0;
}
或:
int main(int argc, char * argv[])
{
if (argc != 4) {
printf("Usage: .exe side row steps");
return 0;
}
int c = atoi(argv[2]), f = atoi(argv[3]);
char z = argv[1][0];
char a[4][4] = { { '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' },{ '*','*','*','*' } };
a[c][f] = 'O';
//maybe some cycle here for each step ??
print(a);
return 0;
}
未经测试,希望它能帮到你