add to array c code example

Example 1: sum of array in c

//made by Kashish Vaid
#include <conio.h>
 
 
int main()
{
    int a[1000],i,n,sum=0;
   
    printf("Enter size of the array : ");
    scanf("%d",&n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
 
    
    for(i=0; i<n; i++)
    {
         
        sum+=a[i];
    }
     printf("sum of array is : %d",sum);
 
    return 0;
}
//made by kashish vaid

Example 2: c assign array

model : 
type arrayName[arraySize] = {value1, value2, ..., valuearraySize};

example : 

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

for multi dimensional arrays :
type arrayName[size1][size2] = {{value11, value12, ..., value1size2}, {value21, value22, ..., value2size2}, ..., {valuesize11, valuesize12, ..., valuesize1size2}};

example : 
int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

Tags:

C Example