利用动态分配内存方法,编写一个能计算N位整数相加的程序,N<=100。用c语言编写。拜托高手指教

来源:百度知道 编辑:UC知道 时间:2024/05/26 04:41:51

一楼的朋友,楼主要的是动态分配内存。。。当然是要数组方法的了。
如果只是循环累加就没意思了。
----------------------------------
//VC++6.0下调试通过
#include <stdio.h>
#include <malloc.h>

signed long sum(signed long array[], int n)
{
signed long result=0;
for (int i=0; i<n; i++)
{
result+=array[i];
}
return result;
}

void main()
{
printf("楼主,你想输入多个数: ");
int n=0;
scanf("%d",&n);
signed long* arr=(signed long*)malloc(sizeof(signed long)*n);
for (int i=0; i<n; i++)
{
printf("请输入第%d个数: ",i+1);
scanf("%d",&arr[i]);
}
printf("累计总和: %d\n",sum(arr,n));
}

#include<stdio.h>
main()
{
long i;
long sum=0,s,gs;
printf("shu ru ji suan de ge shu.\n"); //输入你要计算的整数的个数
scanf("%ld",&gs);

for(s=0;s<gs;s++)
{printf("input %d ge s