Bitlabactivity 2
Bitlabactivity 2
Chapter : ARRAY
w[0] w[1] w[2] w[3] w[4] w[5] w[6] w[7] w[8] w[9]
70.3 48.4 52.67 56.1 54.12 50.78 63.26 68.65 74.37 73.61
Introduction(Cont..)
4
Example 1 :
Declaration:
Memory allocation :
exam_score1 exam_score2
exam_score3 exam_score4
exam_score5
before
Using control structure
While, do..while and for loop
int exam_score[5];
for (i=0;i<5;i++)
scanf(“%d”,&exam_score[i]);
Introduction(Cont..)
6 By using an array..
Declaration :
int exam_score[5];
array index /
Memory allocation : subscript
array name
exam_score[0]
exam_score[1]
exam_score[2]
exam_score[3]
exam_score[4]
Terms :
1. Array
a collection of data items of the same name & data type.
2. Array name
name of array is an identifier.
3. Array element
maximum number that can be stored in array / size of array
4. Array subscript/Index
a value/expression enclosed in [ ] brackets after the array
name, specifying which array element to access. Must
start with 0.
Declaring Array
8 Whendeclaring arrays, we have to specify :
1. Array data type
2. Array Name
3. Number of elements
Format :
Example :
int exam_score[28];
Example:
int b[100], x[27];
char car[23], lorry[45];
float duck[3], ant[9];
Array can be initialized at :
10
1. Compile-Time Initialization of Arrays (while declaring)
Example :
Example :
int exam_score[4] = {88,85}; --> the 2nd & 3rd element are 0
Format:
arrayname[ position number ]
//output for the fragment code :
Example : //output element for index 0 = 4.05
89 element for index 1 = 3.45
element for index 2 = 3.21
(i) exam_score[0] = 89; element for index 3 = 6.55
element for index 4 = 10.23
printf( "%d", exam_score[ 0 ] );
index → 0 1 2 3 4
Mark [0] Mark[1] Mark[2] Mark[3] Mark [4] Mark [5] Mark [6] Mark [7]
16.0 12.0 6.0 8.0 2.5 12.0 14.0 10.5
Let x = 1 and y = 2, write the new value, if any for array value after execute
the following statements. (Assume that each of the operations is connected).
▪ value[x + 3] = value[5] ;
▪ value[5] = value[2 *x] + y;
▪ value[y] = value[4] + value[2 /y] ;
▪ value[0] = x * value[3];
value
Let x = 1 and y = 2, write the new value, if any for array value after execute
the following statements. (Assume that each of the operations is connected).
▪ value[x + 3] = value[5] ;
▪ value[5] = value[2 *x] + y;
▪ value[y] = value[4] + value[2 /y] ;
▪ value[0] = x * value[3];
value 7 6 16 7 10 10
exam_score[0][0] 70
exam_score[0][1] 80
exam_score[0][2] 90
exam_score[0][3]
72
exam_score[1][0]
82
exam_score[1][1]
exam_score[1][2] 92
exam_score[1][3] 71
exam_score[2][0] 81
exam_score[2][1] 91
exam_score[2][2] 67
exam_score[2][3] 78
98
Two Dimensional Array Initialization
22
2-D array can be initialized at :
1. compile time (while declaring)
2. run time
Example :
The best way : nest the data in braces to show the exact nature of the array.
Example :
Example:
exam_score[0][2] = 99;
exam_score[1][4] = exam_score[0][3] + 10;
Two Dimensional Array Referencing
26
(cont..)
Example Program //Program Output
[0][0] = 71
#include<stdio.h> [0][1] = 81
void main( ) { [0][2] = 91
int [0][3] = 73
exam_score[3][4]={{70,80,90,72}, [1][0] = 83
{82,92,71,81},{91,67,78,98}}; [1][1] = 93
for(int row=0; row<3; row++) [1][2] = 72
{ [1][3] = 82
for(int column=0; column<4; [2][0] = 92
column++){ [2][1] = 68
exam_score[row][column]= [2][2] = 79
exam_score[row][column]+1; [2][3] = 99
printf("[%d][%d] = %d\n",
row,column,exam_score[row][
column]);
}
}
}
Exercise
27
int marks[3][5]={80, 90, 96, 73, 65, 67, 90, 68, 92, 84, 70, 55, 95, 78,
100};
Example :
char college[15];
character array college can store a string from 0 to 14.
Example :
char college[11]=“FSKTM UTHM”;
F S K T M U T H M \0
String Initialization
30 can initialize string variables at compile-time.
(a) a string
char college[11]={‘K’,‘U’,‘i’,‘T’,‘T’,‘H’,‘O’, ‘ ’,‘B’,‘P’,‘\0’};
use printf and %s and function fprintf for file-oriented output of string
variables.
equivalent to
Array name is address of array, so ‘&’ sign is not needed for scanf.
scanf( "%s", string2 );
Example Program
/* Treating character arrays as strings using
//Program Output
scanf*/
#include <stdio.h>
Enter a string: Good
int main() { Luck
char string1[20], string2[] = "string literal";
int i;
string1 is: Good
printf("Enter a string: "); string2 is: string literal
scanf( "%s", string1 );
printf("string1 is: %s\nstring2 is: string1 with spaces
%s\n",string1,string2);
printf("string1 with spaces between
between characters is:
characters is:\n"); Good
for ( i = 0; string1[ i ] != '\0'; i++ )
printf( "%c ", string1[ i ] );
printf( "\n" );
return 0;
}
String Referencing (cont..)
34
2. Assign the character value 'Z' to the fourth element of 8. Use a printf statement to print out the third element
the letters array of an integer array called totals
3. Use a for loop to total the contents of an integer array 9. Use a printf statement to print out the contents of the
called numbers which has five elements. Store the character array called words
result in an integer called total.