CHAPTER 9:
Array
BEE1222: COMPUTER PROGRAMMING
YAW- credit to HAA,RAK & AIH
Syllabus
9.1
Array Initialization
9.2
Passing Array to
Function
9.3
Multidimensional
Array
YAW- credit to HAA,RAK & AIH
Lesson Outcomes
1.
Understand the concept of array and able to
use it to minimize the complexity of the
program
2.
Understand the concept of multidimensional
array
YAW- credit to HAA,RAK & AIH
9.1 Array Initialization
Database
(store/sort/searc
h/tables of
value)
How
programm
er needs
to keep
track a
number of
UMP
students ?
??
Array : Easy for memory allocation and saving data
YAW- credit to HAA,RAK & AIH
Arrays
Collection of data elements which hold
multiple variables of the same data type.
An index is used to identify the elements of
the array.
In C, indexing starts at zero
Programmer can put in any type of data in
the defined array memory such as integer,
floating point, character etc.
YAW- credit to HAA,RAK & AIH
Using Arrays
Array data type can be used in all elements of
programming:
1. I/O Statements
2. Operation Statements
3. Selection Statement
4. Repetitive Statements
5. Functions
YAW- credit to HAA,RAK & AIH
Array Types
Single array a[n]
multidimensional array a[n][m]
String array char a[n]
YAW- credit to HAA,RAK & AIH
Example of memory allocation for 10-elements array
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];
YAW- credit to HAA,RAK & AIH
Array (Initialization)
ata_type variable_name [elements] = {value_list};
data_type a[n]={d0,d1,dn1};
int a[5]={0,1,2,3,4};example:a[4]=3
doublex[3]={5.5,4.4,3.3};
charvowel[5]={a,e,i,o,u};
charvowel[5]=aeiou;
10
int names[2];
names[0] = 101;
names[1] = 232;
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
9.2 Passing Array to Function
The array is passed
as an array of unspecified size (int array[])
OR
as a pointer (int *arrayPtr)
Changes to the array within the function
affect the original array
14
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
9.3 Multi Dimensioned Arrays
have two or more index values which specify the
element in the array.
Declaration & Calculation :
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
Multi Dimensioned Arrays
Value in memory location
a[n][0]
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
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
Example 6 :Passing Arrays to Functions
#include <stdio.h>
#include <conio.h>
void printArray(int array[][4]); /* declare function */
int main() {
int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
printArray(array);
getch ();
return 0;
}
void printArray(int array[][4]) { /* define function */
int i, j;
for(i=0 ; i<3 ; i++) {
for(j=0 ; j<4 ; j++) {
printf("%2d ", array[i][j]);
}
printf("\n");
}
19
YAW- credit to HAA,RAK & AIH
Character Data Type
Declaration & initialization of character variable
char identifier_name;
char identifier_name = char_literal;
Identifier_name = char_literal;
Accept single character of alphanumeric or
symbols (including blank).
20
YAW- credit to HAA,RAK & AIH
Character Input and Output
Character input
scanf( %c, &ch)
Note:
21
Character output
printf(%c, ch)
A space included before %c
type specifier
YAW- credit to HAA,RAK & AIH
Operations on Character Type
Operation
Copy ch1 = ch2
Logical operators
==/!=
<, <=, >=, >
Increment/decrement
Character manipulation
#include <ctype.h>
Example
22
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
String Input and Output
String Input
scanf(%ms, str);
gets(str);
24
String Output
printf(%-s,str);
puts(str);
Note:
Ampersand (&) not included
as prefix to string variable
identifier
Input Text longer than string
length will cause overflow of
string variable
Note:
Adding minus (-) sign to
type specifier (s) cause left
justification of string
String variable without null
character (\0) may cause
run-time error
YAW- credit to HAA,RAK & AIH
Operation with String
String library function
#include <string.h>,<stdlib.h>
String initialization or assignment statement
strcpy(str_var, str_text);
Checking length of string variable to avoid
overflow
strlen(str_var);
25
YAW- credit to HAA,RAK & AIH
Using Strings with Functions
Sting is array of character, therefore using string
data type with function is similar to using
numeric array with function discussed in earlier
topic.
26
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
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);
}
YAW- credit to HAA,RAK & AIH
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
printf("Enter string length and string = ");
scanf("%d", &str_siz);
string1 = (char*)calloc(str_siz,sizeof(char));
scanf("%s",string1);
printf("\nHow many numbers?\n");
scanf("%d",&num_nums);
array_of_nums = (int*)calloc(num_nums, sizeof(int));
array_of_nums[0] = 5;
for(i = 1; i <num_nums; ++i){
array_of_nums[i] = array_of_nums[i-1]*i;
}
return (0);
} 30
YAW- credit to HAA,RAK & AIH
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
YAW- credit to HAA,RAK & AIH
END OF CHAPTER 9
1.
2.
Q& A
Outcomes
Understand the concept of array and able to use it to
minimize the complexity of the program
3.
32
Understand the concept of multidimensional array
Reflection
YAW- credit to HAA,RAK & AIH