0% found this document useful (0 votes)
6 views30 pages

Lecture No

Uploaded by

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

Lecture No

Uploaded by

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

Lecture

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]

» typename is any type


» variablename is any legal variable name
» size is a number the compiler can figure
out
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 3
Arrays

–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.

‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 4


Arrays
Name of array (Note that
all elements of this array
have the same name, a)

–Format: a[0] -45


6
• arrayname[ position number ]
a[1]
a[2] 0

• First element at position 0 a[3] 72


a[4] 1543
a[5] -89
a[6] 0

–n element array named c: a[7]


a[8]
62
-3
•a[ 0 ], a[ 1 ]...a[ n – 1 ] a[9] 1

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]);

–Perform operations in subscript. If x


equals 3
• a[ 5 - 2 ] == a[ 3 ] == a[ x ]
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 6
Declaring Arrays

•Declaring multiple arrays of same type


–Format similar to regular variables
–Example:
int b[ 100 ], x[ 27 ];
float myArray[ 3284 ],z[20];

‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 7


Using Arrays

•Array consist of many values, so we need


loop to perform any operations on it.
•for(i=0;i<10;i++)
– Scanf(“%d”,&a[i]);

•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

•Initialization of arrays can be done by a


comma separated list following its definition.
– For example:
int array [4] = { 100, 200, 300, 400 };

This is equivalent to:


int array [4];
array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = ‫النجاح‬
400; ‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص‬. 11
Initializing Arrays

– You can also let the compiler figure out the


array size for you:
– If size omitted, initializers determine it
int array[] = { 100, 200, 300, 400};

–If not enough initializers, rightmost


elements become 0
int n[ 5 ] = { 0 }

‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 12


Initializing Arrays
–If the number of initializers is more than
the size given in brackets then the
compiler will show an error.
int arr[5]={1,2,3,4,5,6,7,8};//error
–we cannot copy all the elements of an
array to another array by simply assigning
it to the other array like,
int a[5] ={1,2,3,4,5},b[5];
b=a;//not valid
(note:-here we will have to copy all
the elements of array one by one,
using for loop.)
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 13
Example
Program that input temperatures of
week days and then compute the average
float temper[7],sum;
int day;
for(day=0;day < 7;day++)
{ printf(“Enter temperature for day
%d”,day+1);
scanf(“%f”,&temper[day]);
sum =sum +temper[day];}
printf(“Average =%f”,sum/7);
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 14
Example
#include <stdio.h>
int main() {
float expenses[12]={10.3, 9, 7.5, 4.3, 10.5, 7.5,
7.5, 8, 9.9, 10.2, 11.5, 7.8};
int count,month;
float total;
for (month=0, total=0.0; month < 12; month++)
{
total =total + expenses[month];
}
Example
for (count=0; count < 12; count++)
printf ("Month %d = %.2f\n", count+1,
expenses[count]);

printf("Total = %.2f, Average = %.2f\n", total,


total/12);
return 0;
}
Using Constants

–useful to define arrays using constants


#define MONTHS 12
int array [MONTHS];

–However, in ANSI C, you cannot


int n;
scanf(“%d”, &n);
int array[n];

‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 17


Example
#include <stdio.h>
#define SIZE 10
int main()
{
int i,j,n[SIZE]={19,3,15,7,11,9,13,5,17,1};
printf( "%s%13s%17s\n","Element", "Value",
"Histogram" );
for(i=0;i<=SIZE-1;i++ ) {
printf( "%7d %13d", i, n[ i ]);
for(j=1;j<=n[i];j++) /* print one bar */
printf( "%c", '*' );
printf( "\n" );
}
return 0;
}
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 18
‫‪Program Output‬‬

‫‪ .‬ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬ ‫‪19‬‬


Example
Program that demonstratesarrays
int arr[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
int searchKey; // key of item to search for
//------------------------------------------------
arr[0] = 77; // insert 10 items arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬. 20
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10;// now 10 items in array
//------------------------------------------------
for(j=0; j<nElems; j++) // display items
printf( "%d ",arr[j]);
printf("");
//------------------------------------------------
searchKey = 66; // find item with key 66
for(j=0; j<nElems; j++)
if(arr[j] == searchKey) // found item?
break;// yes, exit before end
if(j == nElems) // at the end?
printf("\nCan't find %d",searchKey); //yes
else ‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬.
printf("\nFound %d", searchKey); // no
//------------------------------------------------
searchKey = 55; // delete item with key 55
for(j=0; j<nElems; j++) // look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems; k++)/*move higher ones
down*/
arr[k] = arr[k+1];
nElems--; // decrement size
printf("\n");
//--------------------------------------
for(j=0; j<nElems; j++)//display items
printf("%d ", arr[j]);
printf("");
‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬.
‫‪Output‬‬

‫‪77 99 44 55 22 88 11 0 66 33‬‬
‫‪Found 66‬‬
‫‪77 99 44 22 88 11 0 66 33‬‬

‫‪ .‬ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬


Multidimensional
Arrays
• Arrays in C can have virtually as many
dimensions as you want.

• Definition is accomplished by adding


additional subscripts when it is defined.
Multidimensional
Arrays
• We can say 2-d array is a collection of 1-
D array placed one below the other.
– Data-type array name[row][column];
– Total no. of elements in 2-D array is
calculated as row*column
• For example:
– int a [4] [3] ;
• defines a two dimensional array
• a is an array of int[3];
Multidimensional Array
Example
• int a[2][3];
• For example:-
20 2 7
8 3 15
Positions of 2-D array elements in an array
are as below a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]

•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];

‫ ما الفشل إال هزيمة مؤقتة تصنع لك فرص النجاح‬.


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
corresponds to the column
•For Example
int a[4][5];
•for reading values:-
for(i=0;i<4;i++)
for(j=0;j<5;j++)
scanf(“%d”,&a[i][j]);
Multiple-Subscripted
Arrays
•for displaying value:-
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
printf(“%d \t”,a[i][j]);
printf(“\n”);
}
Initializing
Multidimensional Arrays
• The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} ,
{10, 11, 12} };
• Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
};
– is equivalent to
a[0][0] = 1; a[0][1] = 2; a[0][2] = 3;
a[1][0] = 4; a[1][1] = 5; a[1][2] = 6;
a[2][0] = 7; a[2][1] = 8; a[2][2] = 9;

You might also like