Chapter 9 Array
Chapter 9 Array
Array
Syllabus
9.1
Array Initialization
9.2
Passing Array to
Function
9.3
Multidimensional
Array
YAW- credit to HAA,RAK & AIH
Lesson Outcomes
1.
2.
Database
(store/sort/searc
h/tables of
value)
How
programm
er needs
to keep
track a
number of
UMP
students ?
??
Arrays
Using Arrays
Array Types
Single array a[n]
multidimensional array a[n][m]
String array char a[n]
Name of array
a[0]
a[1]
a[2]
a[3]
1
2
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
a[10]
8
8
9
YAW- credit to HAA,RAK & AIH
Value in memory
location
Array (Declaration)
Syntax:
Data_type variable_name [elements];
char ch [10];
int a [5];
int b[3], c[7];
Array (Initialization)
10
int names[2];
names[0] = 101;
names[1] = 232;
For
holding
strings
Example 1
#include<stdio.h>
intmain()
{
inta[6]={1,2,223,
56,889,90},i;
for(i=0;i<=5;i++)
{
printf("%d\t",a[i]);
}
return(0);
}
11
Example 2
#include<stdio.h>
intmain()
{
intn[10],i;
for(i=0;i<10;i++)
{
n[i]=0;//initializearray
}
printf("%s%13s\n","Element","Value");
for(i=0;i<10;i++)//printarray
{
printf("%7d%13d\n",i,n[i]);
}
return0;
}
12
Example 3
#include <stdio.h>
#include <conio.h>
#define SIZE 10
int main ()
{
int i, n[SIZE];
printf ("Insert 10 value of n =");
for (i=0;i<=9;i=i+1)
{
scanf ("%d", &n[i]);
}
printf ("Your entered values are :\n");
for (i=0;i<=9;i=i+1) //print array
{
printf("\tn[%d]=%d\n", i ,n[i]);
}
getch ();
return 0;
}
13
14
Example 4
#include <stdio.h>
#include <conio.h>
int addNumbers(int fiveNumbers[]); /* declare function */
int main()
{
int array[5];
int i;
printf("Enter 5 integers separated by spaces: ");
for(i=0 ; i<=5 ; i++)
{
scanf("%d", &array[i]);
}
printf("\nTheir sum is: %d\n", addNumbers(array));
getch ();
return 0;
}
int addNumbers(int fiveNumbers[]) /* define function */
{
int sum = 0;
int i;
for(i=0 ; i<=5 ; i++)
{
sum+=fiveNumbers[i];
/* work out the total */
}
return sum;
/* return the total */
}
15
int
m1[10][10];
int
m2[2][2] = { {0,1}, {2,3} };
sum = m1[i][j] + m2[k][l];
16
ro
w
lum
o
C n
YAW- credit to HAA,RAK & AIH
a[n][1]
a[0][m]
a[1][m]
30
33
a[2][m]
a[3][m]
a[n][2]
a[n][2]
n = 0,1,2,3
m = 0,1,2,3
17
Example 5
#include <stdio.h>
#include <conio.h>
int main()
{
int m[4][3] = { {10,5,-3}, {9, 0, 0}, {32,20,1},
{0,0,8} };
int row, column, sum;
sum = 0;
for( row = 0; row < 4; row++ )
for( column = 0; column < 3; column++ )
sum = sum + m[row][column];
printf("The total is %d\n", sum );
getch ();
return 0;
}
18
19
20
Note:
21
Character output
printf(%c, ch)
Operation
Copy ch1 = ch2
Logical operators
==/!=
<, <=, >=, >
Increment/decrement
Character manipulation
#include <ctype.h>
Example
22
isalpha()
islower()
isupper()
isdigit()
isspace()
tolower()
toupper()
String
Character array (array of char data type)
Declaration & initialization of string variable
char
str[str_len];
char str[] =
strings_value;
string
will add null character \0 to mark end of
Note:
string causing number of character stored
always one less than str_len
23
24
String Output
printf(%-s,str);
puts(str);
Note:
Note:
strcpy(str_var, str_text);
Checking length of string variable to avoid
overflow
strlen(str_var);
25
26
Array of String
Two-dimensional array of character type
Declaration
char str_ary[row][col];
char str_ary[row][col] = {str1,
str2, , str_row};
27
Example 7
#include <stdio.h>
#include <string.h>
int main ( )
{
char *name;
strcpy(name,"Siti");
printf("My Name is %s\n",name);
strlen("Siti");
return(0);
}
Compile result:
My Name is
Siti
28
Example 8
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main ( )
{
char string1[20];
char string2[ ]="String literal";
int i;
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 is: %s\nString2 is: %s\n"
"String1 with spaces between characters is: \n",
string1,string2);
for (i=0;string1[i]!= '\0';i++)
{
printf("%c", string1[i]);
}
printf("\n");
29
return (0);
}
Example 9
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
Int main ( )
{
char *string1;
int *array_of_nums;
int str_siz, num_nums, i;
Compile result:
Enter string length and string = 9
alyan
How many numbers
4
Compile result:
Enter string length and string = 9
alyan
How many numbers
4
Illustration:
String1
\0 \0 \0 \0
Array_of_nums
5
5
10
30
31
END OF CHAPTER 9
1.
2.
Q& A
Outcomes
3.
32
Reflection