chapter 9 array
DESCRIPTION
arrayTRANSCRIPT
![Page 1: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/1.jpg)
YAW- credit to HAA,RAK & AIH1
CHAPTER 9: Array
BEE1222: COMPUTER PROGRAMMING
![Page 2: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/2.jpg)
YAW- credit to HAA,RAK & AIH2
Syllabus
• Array Initialization9.1
• Passing Array to Function9.2
• Multidimensional Array9.3
![Page 3: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/3.jpg)
YAW- credit to HAA,RAK & AIH3
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
![Page 4: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/4.jpg)
YAW- credit to HAA,RAK & AIH4
How programmer needs to keep track a
number of UMP
students ???
Array : Easy for memory allocation and saving data
Database(store/sort/
search/tables of value)
9.1 Array Initialization
![Page 5: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/5.jpg)
YAW- credit to HAA,RAK & AIH5
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.
![Page 6: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/6.jpg)
YAW- credit to HAA,RAK & AIH6
Using Arrays Array data type can be used in all elements of
programming:1. I/O Statements2. Operation Statements3. Selection Statement4. Repetitive Statements5. Functions
![Page 7: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/7.jpg)
YAW- credit to HAA,RAK & AIH7
Array Types Single array a[n] multidimensional array a[n][m] String array – char a[n]
![Page 8: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/8.jpg)
YAW- credit to HAA,RAK & AIH8
Name of array a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
a[9]
a[10]
0
1
2
3
4
5
6
7
8
9
Value in memory location
Example of memory allocation for 10-elements array
![Page 9: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/9.jpg)
YAW- credit to HAA,RAK & AIH9
Syntax:
Data_type variable_name [elements];
char ch [10];
int a [5];
int b[3], c[7];
Array (Declaration)
![Page 10: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/10.jpg)
YAW- credit to HAA,RAK & AIH10
Array (Initialization)
data_type a[n] = {d0,d1……,dn-1};
int a[5] = {0,1,2,3,4}; example: a[4] = 3
double x[3]= {5.5,4.4,3.3};
char vowel [5]={‘a’,‘e’,‘i’,‘o’,‘u’}; char vowel [5]=“aeiou”;
Data_type variable_name [elements] = {value_list};
For holding strings
int names[2];names[0] = 101;names[1] = 232;
![Page 11: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/11.jpg)
YAW- credit to HAA,RAK & AIH11
Example 1
#include <stdio.h>
int main ( ){ int a[6] = {1, 2, 223, 56, 889, 90}, i; for(i=0;i<=5;i++) { printf ("%d\t",a[i]); }
return(0);}
![Page 12: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/12.jpg)
YAW- credit to HAA,RAK & AIH12
Example 2 #include <stdio.h>
int main (){
int n[10],i;
for (i=0;i<10;i++){n[i]=0; //initialize array}printf ("%s%13s\n", "Element","Value");
for (i=0;i<10;i++) //print array{printf("%7d %13d\n", i ,n[i]);}
return 0;}
![Page 13: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/13.jpg)
YAW- credit to HAA,RAK & AIH13
Example 3#include <stdio.h>#include <conio.h>#define SIZE 10int 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;}
![Page 14: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/14.jpg)
YAW- credit to HAA,RAK & AIH14
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
![Page 15: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/15.jpg)
YAW- credit to HAA,RAK & AIH15
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 */}
![Page 16: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/16.jpg)
YAW- credit to HAA,RAK & AIH16
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];
row Colum
n
![Page 17: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/17.jpg)
YAW- credit to HAA,RAK & AIH17
Multi Dimensioned ArraysValue in memory location
a[0][m]
a[1][m]
a[2][m]
a[3][m]
A
33
0
22
30
5
7
7 7
8
9
4
4
4
4
a[n][0] a[n][1] a[n][2] a[n][2]
n = 0,1,2,3
m = 0,1,2,3
![Page 18: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/18.jpg)
YAW- credit to HAA,RAK & AIH18
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;
}
![Page 19: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/19.jpg)
YAW- credit to HAA,RAK & AIH19
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");
}
}
![Page 20: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/20.jpg)
YAW- credit to HAA,RAK & AIH20
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 orsymbols (including blank).
![Page 21: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/21.jpg)
YAW- credit to HAA,RAK & AIH21
Character Input and Output
Character input
scanf(“ %c”, &ch)
Note: A space included before %c
type specifier
Character output
printf(“%c”, ch)
![Page 22: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/22.jpg)
YAW- credit to HAA,RAK & AIH22
Operations on Character Type
Operation
Copy ch1 = ch2
Logical operators ==/!=
<, <=, >=, >
Increment/decrement
Character manipulation
#include <ctype.h>
Example isalpha()
islower()
isupper()
isdigit()
isspace()
tolower()
toupper()
![Page 23: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/23.jpg)
YAW- credit to HAA,RAK & AIH23
String
Character array (array of char data type)
Declaration & initialization of string variable char str[str_len];
char str[] =
“strings_value”;
Note: string will add null character \0 to mark end ofstring causing number of character storedalways one less than str_len
![Page 24: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/24.jpg)
YAW- credit to HAA,RAK & AIH24
String Input and Output
String Input
scanf(“%ms”, str);
gets(str);
Note: Ampersand (&) not included
as prefix to string variableidentifier
Input Text longer than stringlength will cause overflow ofstring variable
String Output
printf(“%-s”,str);
puts(str);
Note: Adding minus (-) sign to
type specifier (s) cause leftjustification of string
String variable without nullcharacter (\0) may causerun-time error
![Page 25: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/25.jpg)
YAW- credit to HAA,RAK & AIH25
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 avoidoverflow
strlen(str_var);
![Page 26: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/26.jpg)
YAW- credit to HAA,RAK & AIH26
Using Strings with Functions
Sting is array of character, therefore using stringdata type with function is similar to using
numeric array with function discussed in earliertopic.
![Page 27: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/27.jpg)
YAW- credit to HAA,RAK & AIH27
Array of String
Two-dimensional array of character type
Declaration char str_ary[row][col];
char str_ary[row][col] = {“str1”,“str2”, …, str_row};
![Page 28: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/28.jpg)
YAW- credit to HAA,RAK & AIH28
Example 7
Compile result:My Name is Siti
#include <stdio.h>#include <string.h>
int main ( ){char *name;strcpy(name,"Siti");printf("My Name is %s\n",name);strlen("Siti");return(0);}
![Page 29: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/29.jpg)
YAW- credit to HAA,RAK & AIH29
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");
return (0);}
![Page 30: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/30.jpg)
YAW- credit to HAA,RAK & AIH30
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;
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);}
Compile result:Enter string length and string = 9 alyanHow many numbers4
![Page 31: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/31.jpg)
YAW- credit to HAA,RAK & AIH31
Illustration:
Compile result:Enter string length and string = 9 alyanHow many numbers4
String1
Array_of_nums
5
5
10
30
a l y a n \0 \0 \0 \0
![Page 32: Chapter 9 Array](https://reader034.vdocuments.net/reader034/viewer/2022052701/55cf91ed550346f57b91de5e/html5/thumbnails/32.jpg)
YAW- credit to HAA,RAK & AIH32
END OF CHAPTER 91. Q& A2. Outcomes
Understand the concept of array and able to use it to
minimize the complexity of the program
Understand the concept of multidimensional array
3. Reflection