Topic 6 - Arrays
Topic 6 - Arrays
PROGRAMMING 1
LECTURE: 04 – ARRAYS AND STRINGS
2
Learning Objectives:
After completing this chapter, you will be able to:
• learn about arrays
• explore how to declare and manipulate data into arrays
• understand the meaning of “array index out of bounds”
• become familiar with the restrictions on array processing
• learn about C-strings
• examine the use of string functions to process C-strings
• discover how to input data into—and output data from—a C-string
• learn about parallel arrays
Data Types 3
• The statement
int num[5];
declares an array num of 5 components of the
type int
• The components are num[0], num[1],
num[2], num[3], and num[4]
6
Accessing Array Components 7
The statement
int list[10] = {0};
declares list to be an array of 10 components
and initializes all components to zero
The statement
int list[10] = {8, 5, 12};
declares list to be an array of 10 components,
initializes list[0] to 8, list[1] to 5, list[2] to 1
2 and all other components are initialized to 0
Partial Initialization (continued)
21
• The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
• The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
− The first two components are initialized to 4 and 7
respectively
− All other components are initialized to 0
Restrictions on Array Processing
22
• For example:
int studentId[50];
char courseGrade[50];
THANK YOU…