Sunteți pe pagina 1din 16

Introduction (Arrays)

When a program to store the roll numbers of students of a class is to be


written., then we will have 2 options:-

Suppose , students = 100


Option 1:- declare 100 different variables and store each roll no in a
variable.
Option 2:- declare a single variable in such a way that, we can store
all the roll no’s in it.

The programmer’s choice will be option-2.


In order to declare a variable, to store many values in it, we have to use the
concept of “ARRAY” .
Arrays - A collection of same data type, 1D, 2D

An array is a collection of elements of the same type that are


referenced by a common name.

Compared to the basic data type (int, float & char) it is an


aggregate or derived data type.

Array size must be a constant value. All the elements of an array


occupy a set of contiguous memory locations.
Why need to use array type?

"We have a list of 1000 students' marks of an integer type. If using


the basic data type (int), we will declare something like the
following…"

int studMark0, studMark1, studMark2, ..., studMark999;

imagine how long we have to write the declaration part by using


normal variable declaration?
By using an array, we just declare like this,
ex: int studMark[100];

This will reserve 100 contiguous memory locations for storing the
students’ marks.
Graphically, this can be depicted as in the following figure.
Eg: if we declare variable, memory will be allotted randomly
int n1, n2, n3, n4, n5, n6, n7
Declaring an Array:- Like any other variable, array must be
declared before they are used.

Syntax to declare an array:


datatype arrayname[size];

Ex: int arr[10];


In single dimension array memory will be allotted contiguously

int n[10]; can hold 10 integer values.


Size of an Array:
For each integer value, 2 bytes of memory will be registered
i.e., 10*2=20 bytes of memory is registered.
Initialization of an array:- After an array is declared it must be
initialized, it will contain garbage value (any random value). An
array can be initialized at either compile time or at runtime.

Direct array initialization: It is same as ordinary variable


initialization

Syntax:
datatype array_name[size] = {list of values};

Example:
int marks[4] = {67, 82, 56, 77}; //fixed length//integer array
initialization
float area[2] = {23.4, 6.8}; //float array initialization
int marks[4] = {67, 82, 56, 77, 59}; //compile time error
int arr[] = {2, 3, 4}; variable lenth//compile time array
initialization.
Run time array initialization:
It is done using scanf() function. This approach is usually
used for initializing large array, or to initialize array with
user specified values.

Eg:
For(i=0;i<n-1;i++)
{
scanf(“%d”,&arr[i]); //run time array initialization.
}
Example: One dimensional array
main()
{
int sub[5], i, total=0;
printf(“enter 5 numbers\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&sub[i]);
total=total + sub[i];
}
for(i=0;i<5;i++)
{
printf(“%d\n”,sub[i]);
}
printf(“total is %d\n”,total);
}
Linear search

• which is used to find whether a given number is present in an array and if it is


present then at what location it occurs.
• It is very simple.
• We keep on comparing each element with the element to search until the desired
element is found or list ends.
Linear search
Linearsearch
• #include <stdio.h>
• main()
• {
• int array[20], key, i, n;
• printf("Enter the number of elements in array\n");
• scanf("%d",&n);

• printf("Enter %d integer(s)\n", n);


• for (i= 0; i < n; i++)
• scanf("%d", &array[i]);

• printf("Enter the number to search\n");


• scanf("%d", &key);
• for (i= 0; i < n; i++)
• {
• if (array[i] ==key) /*if required element found */
• {
• printf("%d is present at location %d.\n",key, i+1);
• break;
• }
• }
• if (i == n)
• printf("%d is not present in array.\n", key);
• }
Two dimensional arrays:- The simplest form of the
multidimensional array is the two dimensional array.

Syntax
int a[3][4];
This array can also be declared and initialized together. Such as

Ex:
int arr[][3] = {……….. {0,0,0}, {1,1,1} };

Run time initialization of two dimensional array:

scanf(“%d”,&arr[i][j]);
Example: Two dimensional array
main()
{
int b[2][3];
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}}

S-ar putea să vă placă și