Lecture No
Lecture No
No
(10)
PAL
* Introduction
Explain about array
* Declaring Arrays
the * Examples Using
2 Hours
arrays Arrays
* Multiple-
structure Subscripted Arrays
Arrays
•Arrays
– Group of consecutive memory locations
– Same name and type
–Generic declaration:
typename variablename[size]
–For example
int a[10];
– Defines an array of int with subscripts
ranging from 0 to 9
– There are 10*sizeof(int) bytes of memory
reserved for this array.
Position number of
the element
within array a
ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح. 5
Arrays
•Array elements are like normal variables
– You can use a[0]=10; x=a[2]; a[3]=a[2]; etc.
– You can use print("%d", a[9]);
– You can use scanf("%d", &a[3]);
•for(i=0;i<10;i++)
– printf(“%d”, a[i]);
ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح. 8
Using Arrays Example
Write a program to input values into an
array and display them
#include <stdio.h>
int main()
{
int main() { int arr[5],i;
for(i=0;i<5;i++)
{ printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}
ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح. 9
Using Arrays Example
printf(“the array elements are: \n”);
for(i=0;i<5;i++)
{printf(“%d\t”,arr[i]);}
return 0;
}
10
Initializing Arrays
77 99 44 55 22 88 11 0 66 33
Found 66
77 99 44 22 88 11 0 66 33
•In memory:
Multiple-Subscripted
Arrays
•For processing 2-d array,we use two
nested for loops.The outer for loop
corresponds to the row and the inner
ccorresponds to the column
•For Example
int a[4][5];