Basu

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Welcome

By: Basavaraj K Parapur


Bsc 1 sem
122Scs0430
Arrays in C
01 Introduction

c ontents
02 Properties

03 Advantage and
Disadavntages

04 Declaration And
Initilization of array

05 Programs
01
Introduction:
An array is defined as the collection of similar type of data
items stored at contiguous memory locations. Arrays are the
derived data type in C programming language which can
store the primitive type of data such as int, char, double,
float, etc. It also has the capability to store the collection of
derived data types, such as pointers, structure, etc. The array
is the simplest data structure where each data element can be
randomly accessed by using its index number.
02 Properties of an Array:

The array contains the following properties. 


• Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes. 
• Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location. 
• Elements of the array can be randomly accessed since we
can calculate the address of each element of the array with
the given base address and the size of the data element.
03
Advantages of
Array:

1) Code Optimization: Less code to the access the data.


2) Ease of traversing: By using the for loop, we can retrieve
the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need
a few lines of code only.
4) Random Access: We can access any element randomly
using the array.
04 Disadvantages of an Array:

1) Fixed Size: Whatever size, we define at the time of


declaration of the array, we can't exceed the limit.
05
Declaration of an Array :

We can declare an array in the c language


in the following way.
data_type array_name[array_size];
Now, let us see the example to declare the
array.
int marks[5];
Here, int is the data_type, marks are the
array_name, and 5 is the array_size.
06 Initilization of an Array:
initialization of array in c language:
The simplest way to initialize an array is
by using the index of each element.
We can initialize each element of the array
by using the index.
Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
07
Examples:
C array example
#include<stdio.h>
int main()
{
int i=0;
int marks[5];//declaration of array Output
marks[0]=80;//initialization of array 80
marks[1]=60; 60
marks[2]=70; 70
marks[3]=85; 85
marks[4]=75; //traversal of array 75
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Programes:
08 Program to take 5 values from the user and store them in an array
#include <stdio.h>
int main()
{ Output
int values[5]; Enter 5 integers:
printf("Enter 5 integers: "); 1
for(int i = 0; i < 5; ++i) -3
{ 34
scanf("%d", &values[i]); 0
} 3
printf("Displaying integers: "); Displaying integers: 1
for(int i = 0; i < 5; ++i) -3
{ 34
printf("%d\n", values[i]); 0
} 3
return 0;
}
Program to find the average of n numbers using
09 arrays
#include <stdio.h>
int main()
{ Output
int marks[10], i, n, sum = 0, average; Enter n: 5
printf("Enter number of elements: "); Enter number1: 45
scanf("%d", &n); Enter number2: 35
for(i=0; i<n; ++i) Enter number3: 38
{ Enter number4: 31
printf("Enter number%d: ",i+1); Enter number5: 49
scanf("%d", &marks[i]); Average = 39
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
THANK YOU!

You might also like