This document discusses arrays in C programming. It defines an array as a collection of the same type of data elements stored in contiguous memory locations that are accessed via an index. It provides the syntax for declaring a 1-dimensional array, which specifies the type, array name, and number of elements. An example declares and initializes an integer array of size 5. The document also shows examples of summing the elements of a hardcoded and user-input array using indexing and loops.
Array Declaration Syntax: <type><arrayName>[<array_size>] Ex. int a[6]; The array elements are all values of the type <type>. The size of the array is indicated by <array_size>, the number of elements in the array. <array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions.
4.
Array Declaration(continued) // arrayof 6 int a[6]; -- -- ---- ---- 4 530 21 0 1 2 3 4 5 a Collection of 6 variable 1 dimensional Array Datatype Array name Array index start from 0 Array index
5.
Sum 1,2,3,4 #include<stdio.h> int main() { inta,b,c,d; a=1 ; b=2 ; c=3 ; d=4 ; int sum=a+b+c+d; printf(“%d”,sum); return 0; } Output 10 #include<stdio.h> int main() { int num[4]; num[0]=1 ; num[1]=2 ; num[2]=3 ; num[3]=4 ; int sum=num[0]+num[1]+num[2]+num[3]; printf(“%d”,sum); return 0; } Output 10 Sum 1,2,3,4 Arraynormal Array
6.
#include<stdio.h> int main() { int num[4]; scanf(“%d%d%d%d”,&num[0],&num[1] ,&num[2],&num[3]); int sum=num[0]+num[1]+num[2]+num[3]; printf(“%d”,sum); return 0; } Output Sum from user input #include<stdio.h> int main() { int num[4],sum=0,i; for(i=0; i<4; i++) { scanf(“%d”,&num[ i ] ); } for(i=0; i<4; i++) { sum= sum + num [ i ] ; } printf(“%d”,sum); return 0; } Output Sum from user input
8.
ASHJADUL AMIN ID=171-004-041 Section=A1 Department ofTextile Engineering Primeasia University, Banani, Dhaka. MD. MAZHARUL ISLAM ID=171-009-041 Section=A1 Department of Textile Engineering Primeasia University, Banani , Dhaka. Submitted By