C Programming Language
MELJUN CORTES
Objectives
Well learn
Declare one-dimensional arrays
Use strings
Create multidimensional arrays
Initialize arrays
Build arrays of strings
Declaration of Array
type var_name[size];
ex) int grade[5];
grade[0], grade[1], . . . , grade[4]
Index : 0 ~ Size 1
a[i] = 0 ;
/* clear a */
scanf("%d", &count[9]) ;
In C, you may not assign one entire array to another
Ex) This fragment is incorrect
10
11
12
2. Initialize Arrays
Integer array
int i[5] = { 1, 4, 9, 16, 25 } ;
Character array
char a[3] = { 'A', 'B', 'C' } ;
char i[5] = "Herb" ;
13
2. Initialize Arrays
You can give the elements of arrays initial values.
14
2. Initialize Arrays
/* Squares array */
#include <stdio.h>
#define ARY_SIZE 5
void main () {
int i;
int sqrAry[ARY_SIZE];
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
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
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
17
3. Use String
Storing string and characters
18
3. Use String
String and character array
19
3. Use String
String and characters
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};
21
3. Use String
Cs standard library functions for string
gets(str)
strcpy(to, from)
strcat(to, from)
strcmp(s1, s2)
less than 0
- s1 is equal to s2
strlen(str)
22
3. Use String
String input
#include <stdio.h>
int main(void) {
char str[80];
int i;
return 0;
}
MELJUN CORTES, MBA,MPA,BSCS
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);
return 0;
}
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.
25
3. Use String
String input function
26
3. Use String
String output function
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);
}
28
3. Use String
String manipulation function
String manipulation library : <string.h>
str (parameters)
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);
30
3. Use String
String Copy
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);
32
3. Use String
String Concatenation
33
3. Use String
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
34
3. Use String
Return : 0
Return : < 0
Return : > 0
35
3. Use String
Example String manipulation function
36
3. Use String
Example String manipulation function
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.
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
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
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},
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] ;
42
4. Multidimensional Arrays
Example
43
4. Multidimensional Arrays
Exercises
Write a program that defines a 3X3X3 three-dimensional array,
and load it with the numbers 1 to 27.
44