meljun cortes c++_chap5
TRANSCRIPT
-
8/10/2019 MELJUN CORTES C++_Chap5
1/44
C Programming Language
Chap. 5
Exploring Arrays and Strings
MELJUN CORTES MBA MPA BSCS
-
8/10/2019 MELJUN CORTES C++_Chap5
2/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 2
Objectives
Well learn Declare one-dimensional arrays
Use strings
Create multidimensional arrays Initialize arrays
Build arrays of strings
-
8/10/2019 MELJUN CORTES C++_Chap5
3/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 3
1. Declare one-dimensional arrays
Arrays ? A list of variables that are all of the same type and are
accessed through a common name.
Form a convenient way to handle groups of related data.
The general form of one-dimensional Array
type: data type
var_name: array name
size: the number of elements
type var_name[size];
-
8/10/2019 MELJUN CORTES C++_Chap5
4/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 4
Declaration of Array type var_name[size];
ex) int grade[5];
grade[0], grade[1], . . . , grade[4]
Index : 0 ~ Size 1
var_name[index]corresponds to a variable.
index range of array[n] : ( 0 ~ n-1 )
Often mistake for ( 1 ~ n )
It can have an fatal effect on the program.
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
5/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 5
Declaration of Array
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
6/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 6
Usually using array for ( i = 0 ; i < N ; i++ )
a[i] = 0 ; /* clear a */
for ( i = 0 ; i < N ; i++ )
scanf("%d", &a[i]) ;/* reads data into a */
for ( i = 0 ; i < N ; i++ )sum += a[i] ; /* sums the elements of a */
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
7/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 7
1. Declare one-dimensional arrays
The scores array
int scores[9];
-
8/10/2019 MELJUN CORTES C++_Chap5
8/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 8
1. Declare one-dimensional arrays
#include
int main(void) {
int sqrs[10];
int i;
for(i=1; i
-
8/10/2019 MELJUN CORTES C++_Chap5
9/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 9
1. Declare one-dimensional arrays
Using array When you want to use scanf() to input a numeric value into
an array element, simply put the & in front of the array
name.
Ex) This call to scanf() reads an integer into count[9]
In C, you may not assign one entire array to another
Ex) This fragment is incorrect
scanf("%d", &count[9]) ;
char a1[10], a2[10] ;
: :
a2 = a1 ; /* this is wrong */
for(t=0; t
-
8/10/2019 MELJUN CORTES C++_Chap5
10/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 10
Example 1.
Arrays are very useful when lists of information need to be
managed.
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
11/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 11
Example 2.
This program loads a1with the numbers 1 through 10 and then
copies them into a2.
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
12/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 12
Example 3.
The user first enters the message, which is stored in a character
array. When the user presses ENTER, the entire message is then
encoded by adding 1 to letter.
1. Declare one-dimensional arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
13/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 13
2. Initialize Arrays
You can give the elements of arrays initial values.
Integer array
int i[5] = { 1, 4, 9, 16, 25 } ;
Character array char a[3] = { 'A', 'B', 'C' } ;
char i[5] = "Herb" ;
type array-name[size] = {value-list};
value-list: a comma-separated list of constants that are
type compatible with the base type of the array.
-
8/10/2019 MELJUN CORTES C++_Chap5
14/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 14
2. Initialize Arrays
You can give the elements of arrays initial values.
-
8/10/2019 MELJUN CORTES C++_Chap5
15/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 15
2. Initialize Arrays
/* Squares array */
#include
#define ARY_SIZE 5
void main () {
int i;int sqrAry[ARY_SIZE];
for (i = 0; i < ARY_SIZE; i++)
sqrAry[i] = i * i;
printf("Element\tSquare\n");
printf("=======\t======\n");
for (i = 0; i < ARY_SIZE; i++)
printf("%5d\t%4d\n", i, sqrAry[i]);
}
Result
Element Square
======= ======
0 01 1
2 4
3 9
4 16
-
8/10/2019 MELJUN CORTES C++_Chap5
16/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 16
2. Initialize Arrays
Exercises
Write a program that reads ten numbers entered by the user
and reports if any of them match.
====== Mapping Checking Program ======
Enter ten numbers : 1 2 3 4 5 6 7 8 9 10
Enter a mapping number : 5
5 is in your array[4].
Enter a mapping number : 12
12 is not in your array.
Enter a mapping number : 0
===== Good Bye ~~!! =====
-
8/10/2019 MELJUN CORTES C++_Chap5
17/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 17
3. Use String
String The most common use of the one-dimensional array in C is
the string.
A string is defined as a null-terminated character array.
The size of array must have one byte larger than the largest
string.
String constant
A series of characters is surrounded by double quotation
mark()
This is a string, Hello, xyz 123
End of a string mark : null character
\0ASCII value is 0.
-
8/10/2019 MELJUN CORTES C++_Chap5
18/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 18
3. Use String
Storing string and characters
-
8/10/2019 MELJUN CORTES C++_Chap5
19/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 19
3. Use String
String and character array
-
8/10/2019 MELJUN CORTES C++_Chap5
20/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 20
3. Use String
String and characters
-
8/10/2019 MELJUN CORTES C++_Chap5
21/44MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 21
3. Use String
String variables C language doesnt have string type.
String variables use char array.
char str[11];
char str[11] = Good Day;
char month[] = January;
char *pStr = Good Day!;
char str[10] = {G,o,o,d,,D,a,y,!,\0};
-
8/10/2019 MELJUN CORTES C++_Chap5
22/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 22
3. Use String
Cs standard library functions for string gets(str) : reads characters until you press ENTER.
strcpy(to, from) : is used copy the contents frominto to.
strcat(to, from) : concatenates a copy offromto toand
terminates fromwith a null.
strcmp(s1, s2) : lexicographically compares two strings and
returns an integer based on the outcome
- s1 is less than s2 less than 0
- s1 is equal to s2 0
- s1 is greater than s2 greater than 0
strlen(str) : returns the length of the string pointed to by str
-
8/10/2019 MELJUN CORTES C++_Chap5
23/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 23
3. Use String
String input
#include
int main(void) {
char str[80];
int i;
printf("Enter a string (less than 80 chars) : \n");
gets(str); /* input the string */
for(i=0; str[i]; i++)
printf("%c", str[i]); /* output the string */
return 0;
}
-
8/10/2019 MELJUN CORTES C++_Chap5
24/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 24
3. Use String
String input
#include
int main(void) {
char str[80];
int i;
printf("Enter a string (less than 80 chars) : \n");
gets(str); /* input the string */
printf(str); /* output the string */
return 0;
}
-
8/10/2019 MELJUN CORTES C++_Chap5
25/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 25
3. Use String
String input/output function
Using gets() and puts()
gets() reads characters until you press ENTER.
scanf() reads characters until you press blank, space or
ENTER.
puts(message); printf(%s, message);
gets(message); scanf(%s, message);
-
8/10/2019 MELJUN CORTES C++_Chap5
26/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 26
3. Use String
String input function
-
8/10/2019 MELJUN CORTES C++_Chap5
27/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 27
3. Use String
String output function
-
8/10/2019 MELJUN CORTES C++_Chap5
28/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 28
3. Use String
String output function
#include
#define MAXCHARS 81
void main(void) {char message[MAXCHARS]; /* enough storage for a complete line */
printf("Enter a string:\n");
gets(message);
printf("The string just entered is:\n");puts(message);
}
Enter a string:
This is a test input of a string.
The string just entered is:
This is a test input of a string.
3 S i
-
8/10/2019 MELJUN CORTES C++_Chap5
29/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 29
3. Use String
String manipulation functionString manipulation library :
String Length : strlen()function
Returns the length, in characters, of a string
str(parameters)
strlen(str) : 8char str[12];
int strlen(str)
3 U S i
-
8/10/2019 MELJUN CORTES C++_Chap5
30/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 30
3. Use String
String manipulation functionString Copy : strcpy()function
Copys the contents of fromto to.
strcpy(to, from) ;
char str[80];
strcpy(str, hello);
printf(str);
3 U S i
-
8/10/2019 MELJUN CORTES C++_Chap5
31/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 31
3. Use String
String Copy
3 U S i
-
8/10/2019 MELJUN CORTES C++_Chap5
32/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 32
3. Use String
String manipulation functionString Concatenation : strcat()function
Adds the contents of fromto the contents of to.
strcat(to, from) ;
char str[80];
strcpy(str, hello);
strcat(str, there);printf(str)
;
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
33/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 33
3. Use String
String Concatenation
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
34/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 34
3. Use String
String Compare : strcmp()function
Returns the result of comparison two strings.
strcmp(s1, s2) ;
string1 string2 Size Results Returns
ABC123
ABC123
ABC123
ABC123
ABC123
ABC
ABC123
ABC123
ABC456
ABC456
ABC
ABC
ABC123
123ABC
8
3
4
3
4
3
-1
equal
equal
String1string2
equal
equal
0
0
< 0
0
> 0
0
0
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
35/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 35
3. Use String
Return : 0
Return : < 0
Return : > 0
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
36/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 36
Example String manipulation function
3. Use String
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
37/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 37
Example String manipulation function
3. Use String
3 U St i
-
8/10/2019 MELJUN CORTES C++_Chap5
38/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 38
3. Use String
Exercises
Write a program that repeatedly inputs string.
Each time a string is input, concatenate it with a second string
called bigstr.
Add newlines to the end of each string.
If the user types quit, stop inputting and display bigstr(which will
contain a record of all strings input).Also stop if bigstr will be overrun by the next concatenation.
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
39/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 39
4. Multidimensional Arrays
Multidimensional Array
You can create arrays of two or more dimensions``
ex) 10 X 12 two-dimensional integer array
Two-dimensional array
5 X 4 two-dimensional array
int count[10][12] ;
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
40/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 40
4. Multidimensional Arrays
Two-dimensional Array
This program loads a 4X5
array with the products of
the indices, then displays
the array in row, column
format.
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
41/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 41
4. Multidimensional Arrays
Initialization of Multidimensional Array
int table[5][4] = {0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23,
30, 31, 32, 33, 40, 41, 42, 43};
int table[5][4] = {{0, 1, 2, 3}, {10, 11, 12, 13},
{20, 21, 22, 23}, {30, 31, 32, 33},{40, 41, 42, 43}
};
int table[ ][4] = {{0, 1, 2, 3}, {10, 11, 12, 13},
{20, 21, 22, 23}, {30, 31, 32, 33},
{40, 41, 42, 43}
};
int table[5][4] = {0}; All zero
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
42/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 42
4. Multidimensional Arrays
Three-Dimensional Array
Add the size of the additional dimension.
ex) 5 X 4 X 3 three-dimensional array
float values[5][4][3] ;
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
43/44
MELJUN CORTES MBA MPA BSCS
School of Computer I nformation 43
Example
4. Multidimensional Arrays
4 Multidimensional Arrays
-
8/10/2019 MELJUN CORTES C++_Chap5
44/44
4. Multidimensional Arrays
Exercises
Write a program that defines a 3X3X3 three-dimensional array,
and load it with the numbers 1 to 27.
And display the sum of its elements.