Programming Tutorials

(追記) (追記ここまで)

Sum of the elements of an array in C

By: Ignatius in C Tutorials on 2012年03月16日 [フレーム]

This simple C program computes the sum of all the elements of an array.

#include <stdio.h>
#define SIZE 12
 
int main()
{
 // declare and initialize the array named a with size SIZE
 int a[SIZE] = {1,3,5,4,7,2,99,16,45,67,89,45};
 // declare two normal variables
 int i, total = 0;
 // do the loop for the array...
 for(i = 0; i <= (SIZE-1); i++)
 {
 // display the array and its element...
 printf("\n a[%d]= %d", i, a[i]);
 // total up the array
 // total = total + a[i]
 total += a[i];
 }
 printf("\nThe sum of the array elements is %d\n", total);
 return 0;
}
 




(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /