Vous êtes sur la page 1sur 22

• CpE 1102L: Programming Logic and Design

Arrays and Strings

UNIT 6
elf © 2015
Topics

• Importance of arrays in computer


programming.
• Array declaration and definition
• One-dimensional Array
• Two-dimensional Array
• Multi-dimensional Array
• Strings
Objectives

• To learn the importance of arrays in computer


programming.
• To apply concepts of arrays using the C
language.
• To create simple programs in one, two or
multidimensional arrays.
• To make simple string programs using the
standard string functions.
Arrays
• An array is a collection of variables of the same type and
placed in memory contiguously .

• The set of data of data in an array is only given one name


and the indices are used to differentiate each of the data.

• The array is important to avoid redundancy of variable


declaration and for easy access of data that are placed
contiguously in memory.

• It is the job of the programmer to ensure that the array is


large enough to hold what the program will put in them.
One-Dimensional Array

• Syntax Declaration:
<data_type> array_name[size_of_array] = {optional
initialization data};

• Example: int num[5]={ 2, 4, 3, 10, 20};


Index 0 1 2 3 4
Note: Min Index =0
2 4 3 10 20 Max Index = size -1

Data num[0] num[1] num[2] … Important:

Address &num[0] &num[1] &num[2] … The name of the array is


the starting address of the
or num array.
One-Dimensional Array
Sample Program : Compute Sum of Array Inputs

1 /* Filename: SumArray.c
Program Description: Ask 5 integers and compute the sum.
Programmer: Pedro de la Cruz
Date Written: May 29,2005
*/ Sample Output:
2 #include<stdio.h> for(x=0; x<5; x++) 11
Enter a number: 3
3 int main(void) sum += num[x]; 12
4 { int num[5]; 13
Enter a number: 1
5 int x, sum=0; printf(“\nSum=%d”, sum); 14 Enter a number: 2
6 15 Enter a number: 5
7 for(x=0; x,5; x++) } 16 Enter a number: 2
8 { printf(“Enter a number: “);
Sum = 13
9 scanf(“%d”, &num[0]);
10 }
One-Dimensional Array

• Example
1) Make a program that will ask 10 integers and
determine how many of the inputs are odd or
even numbers.

2) Design the logic for a program that allows a user


to enter 12 numbers, then displays them in the
reverse order of entry.
One-Dimensional Array

• Seatwork
1) Make a program that will ask 10 integers and
determine how many numbers are positive,
negative or zero.

2) Modify the reverse-display program so that the


user can enter any amount of numbers up to 12
until a sentinel value is entered.
Strings

• In C, a string is a group of characters


terminated by a null character (‘\0’).
• It is necessary to declare character arrays to
be one character longer than the size of the
string.
h e l l o ‘\0’
• Example: char str[6]= “hello”;
• char str[6] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
Strings
Sample Program : Ask Characters and Make it a String

1 /* Filename: Function1.c
Program Description: Ask 4 characters, then make it a string.
Programmer: Pedro de la Cruz Sample Output:
Date Written: May 29,2005
Enter a character: a
*/
Enter a character: b
2 #include<stdio.h> str[x]= ‘\0’; 11 Enter a character: c
3 int main(void) 12 Enter a character: d
4 { char str[5], ch; printf(“\nString: %s”, str); 13 String: abcd

5 int x; } 14
6 for(x=0; x<5; x++) { 15
7 printf(“\nEnter a character:”); 16
8 ch=getche( ); 17
9 str[x]=ch; 18
10 } 19
Strings
Sample Program : Ask A String Using A Standard Function

1 /* Filename: Function1.c
Program Description: Ask a string using scanf or gets.
Programmer: Pedro de la Cruz
Sample Output:
Date Written: May 29,2005
*/

2 #include<stdio.h> Enter your name: May Ann


3 int main(void) Enter your surname: Cruz
4 { char str[80]; Cruz
5
6 printf(“\nEnter your name: “);
7 scanf(“%s”, &str[0]);
8 printf(“Enter your surname:”);
9 gets(str);
10 puts(str);
11 }
Common String Library Functions
• strcpy( dest, source) –copy the content of source string to dest string

• strncpy(dest, source, n) – copy at most n character from source to dest

Sample Program : String Copy

1 /* Filename: StringCopy.c
Program Description: Example of strcpy and strncpy.
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Output:
*/

2 #include<stdio.h> puts(s1); 11 I will pass


3 #include<string.h> puts(s2); 12 I will
4 int main(void) puts(s3); 13
hello
5 { char s1[80], s2[80]; } 14
6 char s3[6]=“hello”;
7
8 strcpy(s1, “I will pass”);
9 strcpy(s2,s3);
10 strncpy(s2,s1,6);
Common String Library Functions
• strcat( dest, source) –appends source string to destination string

• strncat(dest, source, n) – appends at most n characters of source


string to dest
Sample Program : String Concatenate
1 /* Filename: StringConcat.c
Program Description: Example of strcat and strncat
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Output:
*/

2 #include<stdio.h> strcpy(s1,”hello”); 9 hello friend


3 #include<string.h> strcpy(s2,” friend”); 10 hello
4 strcat(s1,s2); 11
5 int main(void) strncat(s2,s1,5) 12
6 { char s1[20], s2[20]; 13
7 puts(s1); 14
8 puts(s2); 15
} 16
Common String Library Functions
• strlen( string) – determines the length of the string excluding the null
terminator
Sample Program : String Length

1 /* Filename: StringLength.c
Program Description: Example of strlen
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/

2 #include<stdio.h> printf(“Enter a string: “); 9 Enter a string: pasar


3 #include<string.h> gets(str); 10 Length of String: 5
4 len=strlen(str); 11
5 int main(void) 12
6 { char str[20]; printf(“Length of String: %d”, len); 13
7 int len; 14
8 } 15
Common String Library Functions
Return Values:
• strcmp( s1, s2) – compares two strings 0 s1==s2
<0 s1<s2
>0 s1>s2
Sample Program : String Compare
1 /* Filename: StringCompare.c
Program Description: Example of strcmp
*/

2 #include<stdio.h> result = strcmp(s1,s2); 9 Sample Output:


3 #include<string.h> printf(“%d”, result); 10
4 int main(void) 11 0
5 { } 12
6 char s1[5]=“hello”;
7 char s2[5]=“hello”;
8 int result;
Common String Library Functions

• Seatwork
• Ask a string and print it backwards.

• Ask 5 strings and display the longest string.

• Ask 3 strings and arrange them in alphabetical


order.
Two-Dimensional Array
Syntax Declaration:
<data_type> array_name[row][column] = {optional initialization data};

Example: int num[3][4] = { 1, 2, 3, 4, Note:


5, 6, 7, 8, Min Col & Row =0
9, 3, 1, 0}; Max Row = Row – 1
Max Col = Column – 1
Size of array = col x row x size of data
Data = num[0][0]
Address = &num[0][0] Column Important:
or num[0] 0 1 2 3 When the name of the array is used
with only 1 index specifying the row, it
0 1 2 3 4
Row refers to the address of the first
1 5 6 7 8 element of the row.

2 9 3 1 0
Two-Dimensional Array
Sample Program : Fill a Two-dimensional Array

1 /* Filename: Function1.c
Program Description: Ask 6 integers and place it in a 2x3 array.
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/

Enter a number: 1
2 #include<stdio.h> for(row=0; row<3; row++) 11
Enter a number: 10
3 int main(void) { 12
Enter a number: 2
4 { int x[2][3]; for(col=0; col<2; col++) 13
Enter a number: 20
5 int row, col; { printf(“Enter a number: “); 14
Enter a number: 3
6 scanf(“%d”, &num[row][col]); 15
Enter a number: 30
7 } 16
8 } 17
9 } 18
10 19
Two-Dimensional Array
Sample Program : Array of Strings

1 /* Filename: StrArray.c
Program Description: Array of Strings
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/

2 #include<stdio.h> for(x=0; x<3; x++) 11


Enter a string: I
3 int main(void) { 12
Enter a string: love
4 { char s[3][20]; printf(“Enter a string: “); 13
Enter a string: programming
5 int x; gets(s[x]); 14
I
6 } 15
love
7 16
programming
8 for(x=0; x<3; x++) 17
9 puts(s[x]); 18
10 } 19
Multidimensional Array
• Multidimensional arrays can have three, four or
more dimensions.
• Example: 3-Dimensional Array
Syntax:
<data_type> array_name[plane][row][column]
int table[3][5][4] =
0 0 1 2 3 { { /*Plane 0*/
1 1 2 3 4 {0,1,2,3}, /*Row 0*/
Row
2 3 4 5 6 Plane {1,2,3,4}, /*Row 1*/
3 4 5 6 7 2 {3,4,5,6}, /*Row 2*/
4 5 6 7 8 1
0 {4,5,6,7}, /*Row 3*/
0 1 2 3 {5,6,7,8}, /*Row 4*/
Column }
}
Multidimensional Array
Sample Program : Multidimensional Array
1 /* Filename: MultiDim.c
Program Description: Multidimensional Array sample
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Output:
*/

2 #include<stdio.h>
3 main() 0 1 2 3
4 { int table[3][3][4]= { { /*Plane 0*/ {0,1,2,3}, {1,2,3,4}, {2,3,4,5} } }; 1 2 3 4
5 int plane, row, col; 2 3 4 5
6
7 for(plane=0;plane<1; plane++)
8 {
9 for(row=0; row<3; row++)
10 {
11 for(col=0; col<4; col++)
12 printf(“\t%d”, table[plane][row][col};
13 printf(“\n”);
14 }
15 }
16 }
References
Computer Science: A Structure Programming Approach in C ,
Behrouz A. Fourozan and Richard F. Gilberg

Using Turbo C, Borland Osborne

The C Programming Language, Kernighan and Ritchie

Applications Programming in C, Johnsonbaugh, et.al.

C Programming: The Accessible Guide to Professional


Programming, Steven Holzner

Problem Solving and Program Design in C, Jeri Hanly, et.al

Vous aimerez peut-être aussi