0% found this document useful (0 votes)
15 views18 pages

Chapter 5 - Array and String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views18 pages

Chapter 5 - Array and String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

LOGO

Nguyen Van Phuc

www.hcmute.edu.vn
LOGO

CONTENTS

1 One – dimensional arrays

2 String/Character array

3 Two-dimensional arrays
4 Exercises

www.hcmute.edu.vn
LOGO

1 One – dimensional arrays

 Declaration:
 Syntax
dataType arrayName[array_size];
 Example
int a[5];

www.hcmute.edu.vn
LOGO

1 One – dimensional arrays

 Element accessing:
 Format: arrayName [position number]
 Example: int a[5];
a[0] a[1] a[2] a[3] a[4]

www.hcmute.edu.vn
LOGO

1 One – dimensional arrays

 Initialization:
int a[5]={2,4,34,3,4};
or
int a[ ]={2,4,34,3,4};
a[0] a[1] a[2] a[3] a[4]

2 4 34 3 4

www.hcmute.edu.vn
LOGO

1 One – dimensional arrays

 Initialization:
int a[ 5 ] = { 5, 7 };

a[0] a[1] a[2] a[3] a[4]

5 7 0 0 0

www.hcmute.edu.vn
LOGO

2 String

 Declaration:
 Syntax
char arrayName[array_size];
 Example
char s[5];

www.hcmute.edu.vn
LOGO

2 String

 Initialization:
 char arrayName[ ] = “string content”;
 Example
char s[ ] = “UTE”;
a[0] a[1] a[2] a[3] a[4]

U T E \0

NULL character www.hcmute.edu.vn


LOGO

2 String

 Input/Output:
 Input: gets/gets_s function
 Output: puts function
Example:
char s[10];
gets(s);
puts(s);
www.hcmute.edu.vn
LOGO

2 String

 String handling function: <string.h>


Function Description Example

strlen() showing the length of a char a[10] = {“hello”};


string int k;
k = strlen(a); // k = 5
strcpy( ) copying one string into char a[10] = {“hello”};
another string char b[10];
strcpy(b, a); // copy string a into string b
strcmp() comparing two string char a[10] = {“hello”};
char b[10] = {“hi”};
int k;
k = strcmp(a,b); // k > 0
www.hcmute.edu.vn
LOGO

3 Two-dimensional arrays

 Declaration:
 Syntax
dataType arrayName [rowSize] [columnSize];
 Example
int a[2][3];

www.hcmute.edu.vn
LOGO

3 Two-dimensional arrays

 Initialization:
int a[2][3] = { {1, 2, 5} , {2, 3 , 1} };

1 2 5

2 3 1

www.hcmute.edu.vn
LOGO

3 Two-dimensional arrays

 Element accessing:
 Format
arrayName [ rowPosition][columnPosition]
 Example:
a[1][2] = 5;

www.hcmute.edu.vn
LOGO

4 Exercise
 Exercise 1:

Write a C program to allow user enter


(input) an array of 10 integer numbers then
print all.

www.hcmute.edu.vn
LOGO

4 Exercise
 Exercise 2:
Write a C program to allow user enter
(input) a array of 10 integer numbers
(negative value is not allowed):
a) print the maximum number
b) print the minimum of even numbers

www.hcmute.edu.vn
LOGO

4 Exercise
 Exercise 3:

Write a c program to allow user enter


(input) an array of 10 integer numbers. Print
all numbers in the form of increasing order.

www.hcmute.edu.vn
LOGO

4 Exercise
 Exercise 4:

Write a c program to read full name


a) print the given name
b) counting the number of words in the full
name.

www.hcmute.edu.vn
LOGO

4 Exercise
 Exercise 5:
Write a C program to read 2D array, the
array’s size is entered first (n row and m
column).
a) print the maximum element of array
b) print the maximum of elements on the
main diagonal
www.hcmute.edu.vn

You might also like