Description
数组的输入与输出。编写程序,通过函数给数组输入若干不小于0的整数,用负数作为输入结束标志,通过另一函数输出该数组中的数据。
#include <stdio.h>
#define M 100 /* M表示元素个数不超过100 */
void array_out(int *, int); /* 函数原型,输出元素个数 */
int array_in(int *); /* 函数原型,输入元素 */
int main( )
{
int s[M], k ; /* 定义数组s,元素个数不超过100 */
k = array_in( s ); /* 调用array_in函数给数组s 赋值 */
array_out (s, k); /* 调用array_out函数输出数组s 的各元素值 */
return 0;
}
//请编写array_in 和 array_out函数,只提交你编写的函数,注意输出的格式,5个换一行,每个输出占用4个字符
Input
输入任意数目的数字(数字保证小于等于100),以负数结束输入。
Output
输出数组的内容。
Sample Input
3 2 1 5 4 8 -1
Sample Output
3 2 1 5 4
8
HINT
#include <stdio.h>
#define M 100 /* M表示元素个数不超过100 */
void array_out(int *, int); /* 函数原型,输出元素个数 */
int array_in(int *); /* 函数原型,输入元素 */
int main( )
{
int s[M], k ; /* 定义数组s,元素个数不超过100 */
k = array_in( s ); /* 调用array_in函数给数组s 赋值 */
array_out (s, k); /* 调用array_out函数输出数组s 的各元素值 */
return 0;
}
int array_in(int *a) //函数给数组s赋值,并统计元素个数
{
int i=0,x;
scanf("%d",&x);
while(x>=0) //输入数组s各元素值,负数为结束标志
{
*(a+i)=x; //将输入的值送入数组s各个元素中
i++;
scanf("%d",&x);
}
return i; //返回数组s元素的个数
}
void array_out(int *a,int n) //函数输出数组s各元素的值
{
int i;
for(i=0;i<n;i++)
printf(((i+1)%5==0)?"%4d\n":"%4d",*(a+i));
printf("\n");
}