0% found this document useful (0 votes)
8 views

CA2 Notes Array For Students1

Uploaded by

22u211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CA2 Notes Array For Students1

Uploaded by

22u211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ARRAY

What is an Array?

C Array is a collection of variables belongings to the same data type. You can store group of
data of same data type in an array.

 Array might be belonging to any of the data types


 Array size must be a constant value
 Always, Contiguous (adjacent) memory locations are used to store array elements in
memory
 It is a best practice to initialize an array to zero or null while declaring, if value not
assigned

Types of Array:
There are 2 types of C arrays. They are,

1. One dimensional array


2. Multi dimensional array
 Two dimensional array
 Three dimensional array
upto n dimensional array can be created

Declaration of One Dimensional Array


Integer array for Numbers
int a[10];
Character array for Strings
char b[10];
ONE DIMENSIONAL ARRAY IN C:

Syntax Example

Array declaration syntax: Integer array:


data_type arr_name [arr_size]; int age[5];

Array initialization syntax: int age[5]={10,11,12,13, 14};


data_type arr_name [arr_size]={value1, value2, value3, …}; (or)
int age[0]={10}
int age[1]={20}

Array accessing syntax: To Access the 0th index age[0];


arr_name[index];
To Access the 2nd index age[2];

Character array:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;

To Access the 0th index


str[0];

To Access the 1st index


str[1];

Dynamic initialization of array Example:


Syntax
To get three numbers as input
To get integer input from user
from user
scanf(“format specifier”, &array_name[counter]);
for (i=0; i<3; i++)
{
scanf("%d", &number[i]);
}
Example 1:
Write a C program using one dimensional array to initialize array elements and display the
elements with its index position.
Program:
int main()
{
int i;
int arr[5] = {10,20,30,40,50}; // Initializing and declaring array in C
//To initialize all array elements to 0, use int arr[5]={0};
for (i=0;i<5;i++)
{
// To access each index of the array
printf("value of arr[%d] is %d \n", i, arr[i]);
}
return 0;
}
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

Example 2:

Write a C program to get five numbers from user. Compute and display the addition of even
numbers and product of odd numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,m=1,i,num[5];
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter number[%d]:",i+1);
scanf("%d",&num[i]);
}
printf("\n------------------------");
for(i=0;i<5;i++)
{
if(num[i]%2==0)
{
printf("\n Even Number :%d",num[i]);
a=a+num[i];
}
else
{
printf("\n Odd Number :%d",num[i]);
m=m*num[i];
}
}
printf("\n--------------------");
printf("\n Addition of Even Number:%d",a);
printf("\n Product of Odd Number :%d",m);
printf("\n--------------------");
getch();
}
void main()
{
int sum=0,i,num;
clrscr();
printf("Enter 5 numbers : ");
for(i=0; i<5; i++)
{
scanf("%d",&num);
sum=sum+num;
}
printf("Sum of all numbers = %d",sum);
getch();
}

You might also like