Upload
isse-essi
View
12
Download
0
Tags:
Embed Size (px)
DESCRIPTION
array
Citation preview
YAW- credit to HAA,RAK & AIH1
CHAPTER 9: Array
BEE1222: COMPUTER PROGRAMMING
YAW- credit to HAA,RAK & AIH2
Syllabus
• Array Initialization9.1
• Passing Array to Function9.2
• Multidimensional Array9.3
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
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
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.
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
YAW- credit to HAA,RAK & AIH7
Array Types Single array a[n] multidimensional array a[n][m] String array – char a[n]
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
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)
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;
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);}
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;}
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;}
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
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 */}
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
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
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;
}
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");
}
}
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).
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)
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()
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
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
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);
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.
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};
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);}
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);}
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
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
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