Вы находитесь на странице: 1из 44

Exploring Arrays and Strings

C Programming Language

MELJUN CORTES

Objectives
Well learn
Declare one-dimensional arrays

Use strings
Create multidimensional arrays
Initialize arrays
Build arrays of strings

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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 var_name[size];
type : data type
var_name : array name
size : the number of elements

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Declare one-dimensional arrays

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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Declare one-dimensional arrays


Declaration of Array

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Declare one-dimensional arrays

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] ;

MELJUN CORTES, MBA,MPA,BSCS

/* sums the elements of a */

School of Computer Information

1. Declare one-dimensional arrays

The scores array


int scores[9];

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Declare one-dimensional arrays


Using array
This program loads the sqrs array with the squares of the
numbers 1 through 10 and then displays them.
#include <stdio.h>
int main(void) {
int sqrs[10];
int i;
for(i=1; i<11; i++) sqrs[i-1] = i*i;
for(i=0; i<10; i++) printf("%d ", sqrs[i]);
return 0;
}
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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]

scanf("%d", &count[9]) ;
In C, you may not assign one entire array to another
Ex) This fragment is incorrect

char a1[10], a2[10] ;


:
:
a2 = a1 ; /* this is wrong */

MELJUN CORTES, MBA,MPA,BSCS

for(t=0; t<SIZE; t++)


a2[t] = a1[t];

School of Computer Information

1. Declare one-dimensional arrays


Example 1.
Arrays are very useful when lists of information need to be
managed.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

10

1. Declare one-dimensional arrays


Example 2.
This program loads a1 with the numbers 1 through 10 and then
copies them into a2.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

11

1. Declare one-dimensional arrays


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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

12

2. Initialize Arrays

You can give the elements of arrays initial values.


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.

Integer array
int i[5] = { 1, 4, 9, 16, 25 } ;

Character array
char a[3] = { 'A', 'B', 'C' } ;
char i[5] = "Herb" ;

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

13

2. Initialize Arrays
You can give the elements of arrays initial values.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

14

2. Initialize Arrays
/* Squares array */
#include <stdio.h>
#define ARY_SIZE 5
void main () {
int i;
int sqrAry[ARY_SIZE];

for (i = 0; i < ARY_SIZE; i++)


sqrAry[i] = i * i;

Result
Element Square
======= ======
0
0
1
1
2
4
3
9
4
16

printf("Element\tSquare\n");
printf("=======\t======\n");
for (i = 0; i < ARY_SIZE; i++)
printf("%5d\t%4d\n", i, sqrAry[i]);
}
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

15

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 ~~!! =====
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

16

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


\0 ASCII value is 0.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

17

3. Use String
Storing string and characters

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

18

3. Use String
String and character array

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

19

3. Use String
String and characters

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

20

3. Use String
String variables
C language doesnt have string type.
String variables use char array.

char
char
char
char
char

str[11];
str[11] = Good Day;
month[] = January;
*pStr = Good Day!;
str[10] = {G,o,o,d, ,D,a,y,!,\0};

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

21

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 from into to.

strcat(to, from)

: concatenates a copy of from to to and


terminates from with 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

- s1 is greater than s2 greater than 0

strlen(str)

: returns the length of the string pointed to by str

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

22

3. Use String
String input
#include <stdio.h>
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;
}
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

23

3. Use String
String input
#include <stdio.h>
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;
}

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

24

3. Use String
String input/output function
Using gets() and puts()
puts(message); printf(%s, message);
gets(message); scanf(%s, message);
gets() reads characters until you press ENTER.
scanf() reads characters until you press blank, space or
ENTER.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

25

3. Use String
String input function

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

26

3. Use String
String output function

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

27

3. Use String
String output function
#include <stdio.h>
#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");
Enter a string:
puts(message);
}

MELJUN CORTES, MBA,MPA,BSCS

This is a test input of a string.


The string just entered is:
This is a test input of a string.
School of Computer Information

28

3. Use String
String manipulation function
String manipulation library : <string.h>
str (parameters)

String Length : strlen() function


int strlen(str)

Returns the length, in characters, of a string


strlen(str) : 8
char str[12];
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

29

3. Use String
String manipulation function
String Copy : strcpy() function
strcpy(to, from) ;
Copys the contents of from to to.
char str[80];
strcpy(str, hello);
printf(str);

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

30

3. Use String
String Copy

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

31

3. Use String
String manipulation function
String Concatenation : strcat() function
strcat(to, from) ;
Adds the contents of from to the contents of to.
char str[80];
strcpy(str, hello);
strcat(str, there);
printf(str);

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

32

3. Use String
String Concatenation

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

33

3. Use String

String Compare : strcmp() function


strcmp(s1, s2) ;
Returns the result of comparison two strings.
string1

string2

Size

Results

Returns

ABC123

ABC123

equal

ABC123

ABC456

equal

ABC123

ABC456

String1<string2

<0

ABC123

ABC

equal

ABC123

ABC

String1>string2

>0

ABC

ABC123

equal

ABC123

123ABC

-1

equal

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

34

3. Use String
Return : 0

Return : < 0

Return : > 0

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

35

3. Use String
Example String manipulation function

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

36

3. Use String
Example String manipulation function

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

37

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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

38

4. Multidimensional Arrays
Multidimensional Array
You can create arrays of two or more dimensions``
ex) 10 X 12 two-dimensional integer array
int count[10][12] ;

Two-dimensional array
5 X 4 two-dimensional array

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

39

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
1
2
3

0
2
4
6

0
3
6
9

0
4
8
12

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

40

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};
MELJUN CORTES, MBA,MPA,BSCS

All zero
School of Computer Information

41

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] ;

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

42

4. Multidimensional Arrays
Example

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

43

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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

44