若这些数组都用全局变量的话。那就简单了
如下:#include<stdio.h>
#include<stdlib.h>
void addarray();
int num0[5],num1[5],num2[5];
int i;
int main()
{
printf("请输入第一个数组5个数字...\n");
for(i=0;i<5;i++)
{
printf("请输入第%d个数字:",i+1);
scanf("%d",&num0[i]);
}
printf("\n");
printf("=====================================\n");
printf("\n");
printf("请输入第二个数组5个数字...\n");
for(i=0;i<5;i++)
{
printf("请输入第%d个数字:",i+1);
scanf("%d",&num1[i]);
}
printf("\n");
printf("=====================================\n");
printf("\n");
printf("新数组为:\n");
addarray();
for(i=0;i<5;i++)
printf("%d ",num2[i]);
system("pause");
return 0;
}
void addarray()
{
for(int i=0;i<5;i++)
num2[i]=num0[i]+num1[i];
}
若这些数组不用全局变量的话,那么函数应该这么写#include<stdio.h>
#include<stdlib.h>
int main()
{
int* addarray(int num0[5],int num1[5]);
int num0[5],num1[5];
int *p_num2,i;
printf("请输入第一个数组5个数字...\n");
for(i=0;i<5;i++)
{
printf("请输入第%d个数字:",i+1);
scanf("%d",&num0[i]);
}
printf("\n");
printf("=====================================\n");
printf("\n");
printf("请输入第二个数组5个数字...\n");
for(i=0;i<5;i++)
{
printf("请输入第%d个数字:",i+1);
scanf("%d",&num1[i]);
}
printf("\n");
printf("=====================================\n");
printf("\n");
printf("新数组为:\n");
p_num2=addarray(num0,num1);
for(i=0;i<5;i++)
printf("%d ",*(p_num2+i));
system("pause");
return 0;
}
int* addarray(int num0[5],int num1[5])
{
static int num2[5];
for(int i=0;i<5;i++)
num2[i]=num0[i]+num1[i];
return num2;
}
|