我正在尝试创建一个带有5个输入整数的程序,并在其位置打印出星号。我必须创建第二个函数来传递变量以将其打印出来,但只能传递1个值。这是我感到困惑的地方,因为我们还没有超过阵列,我认为这可能是一种可能的方法来完成它?有人指出我应该朝这个方向前进吗?
目前到目前为止:
int main()
{
//Variables
int counter = 1;
int chart = 0;
//I/O section for user input
printf ("Please enter number of asterisks for 5 graphs, must be less than or equal to 50\n");
for (counter; counter <= 5; counter++)
{
do
{
printf("How long is chart %d? ", counter) ;
scanf("%d", &chart) ;
}while (chart > 50) ;
}
}
在我知道它必须传递给另一个函数之前,我最初有类似下面的内容:
int main()
{
//Variables
int chart1 ;
int chart2 ;
int chart3 ;
int chart4 ;
int chart5 ;
//I/O section for user input
printf ("How long is chart 1? ");
scanf("%d", &chart1 );
if (chart1 <= 50)
{
printf("\nPlease enter a number less than or equal to 50") ;
}
else
{
printf ("How long is chart 2? ");
scanf("%d", &chart2 );
printf ("How long is chart 3? ");
scanf("%d", &chart3 );
printf ("How long is chart 4? ");
scanf("%d", &chart4 );
printf ("How long is chart 5? ");
scanf("%d", &chart5 );
}
//Chart 1 Print, loop, and counter
do
{
printf ("1. %d|", chart1);
for (chart1; chart1 >0; chart1--)
{
printf("*");
}
} while(chart1 != 0);
//Chart2 Print, loop, and counter
....
//Chart 3 Print, loop, and counter
..
//Chart 4 Print, loop, and counter
..
//Chart 5 print, loop, and counter
..
}
答案 0 :(得分:1)
以下是如何将整数数组传递给函数:
#include <stdio.h>
void print_array(int arr[]);
int main()
{
int my_array[] = { 1, 2, 3, 4, 5 };
print_array(my_array);
return 0;
}
void print_array(int arr[])
{
int i;
for (i = 0; i < 5; ++i)
printf("%d ", arr[i]);
printf("\n");
}
答案 1 :(得分:1)
可能这可能就是你要找的东西:
. /home/user/.bashrc
你的方法定义可以使用参数显示星号的for循环(不完全是这样的):
for (counter = 0; counter < 5; counter++) //notice changes here as well
{
printf("How long is chart %d? ", counter) ;
scanf("%d", &chart) ;
changeToAsterisk(chart);
}