www.zhnq.net > 输出数组元素并求和

输出数组元素并求和

#include #include void Sum(int *p,int *sum);void main(){ void (*p)(int*,int*); int a[8]={0},sum=0; p=Sum; srand(time(0)); for(int i=0;i 评论0 0 0

遍历数组,累加到和值对应的变量上即可.代码如下:int sum(int *a, int l)//对数组a的前l个元素求和,并返回和值.{ int r = 0; int i; for(i=0;i<l;++i) r+=a[i]; return r; }

程序如下:#include <stdio.h> int a[4][4]={{1,2,3,4},{4,5,6,1},{7,8,9,1},{10,11,12,1}}; int ahang[4],alie[4]; void main() { int sum=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { sum+=a[i][j]; ahang[i]+=a[i][j]; alie[j]+=a[i][j]; } } printf("%d\n",sum); int k,o; int min,

#include< stdio.h> void main() { int a[n],sum; for(i=0;i<n;i++) {scanf("%d",&a[i]); sum+=a[i];} printf("%d\n",sum); }

#include "stdio.h"#include "conio.h" main() { int A[4][5],B[4][5],C[4][5],tmp[4][5]; int i,j,Max; for(i=0;i<4;i++) for(j=0;j<5;j++) {scanf("%d",&A[i][j]);/*输入A*/ scanf("%d",&B[i][j]);/*输入B*/ tmp[i][j]=A[i][j]+B[i][j];/*暂存A+B*/ } for(i=0;i<4;i++) for

#include<stdio.h>int main(){ int a[2][3]={{1,2,3},{4,5,6}}; int i,j,sum=0; for(i=0;i<2;i++) for(j=0;j<3;j++) sum+=a[i][j]; printf("sum=%d\n",sum); return 0;}

option explicit option base 1 dim a(10),sum%,m%,i% sum = 0 print “一维数组为”; for i = 1 to 10 a(i)=int(rnd*100)+1 print a(i) sum =sum +a(i) next i print "十个元素之和为:";sum print "十个元素的平均值为";sum/10

#include main() { int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j,sum=0; for(i=0;ifor(j=0;jsum+=a[i][j]; printf("the sum is %d.\n",sum); }

看你代码,你的要求是:求数组a[]中满足下标中百位+十位==个位的所有下标及所有数的和?如果是的话 int i,j,a[900]; float sum=0; for(i=100,j=0;i<1000;i++) { if(i/100+(i%100)/10==i%10) { printf("满足条件的数为%d\n",i); sum=sum+a[i]; } } printf("%d\n",sum); 这样的话无需改变原来的数组就可以了

网站地图

All rights reserved Powered by www.zhnq.net

copyright ©right 2010-2021。
www.zhnq.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com