Similar presentations:
Introduction to programming - Lecture 5: Arrays
1. Introduction to programming – Lecture 5: Arrays
INTRODUCTION TO PROGRAMMING –LECTURE 5: ARRAYS
Aigerim Aibatbek - Senior Lecturer
aigerim.aibatbek@astanait.edu.kz
2. Variable Review
VARIABLE REVIEWWe learned that if you need to store 2 items, you ask for 2 drawers.
3. Variable Review
VARIABLE REVIEWWell, let's say you need to store more values, like 5, 10, or even 100!
int x;
int myNum;
int birthYear;
int count; int totalArea;
int
y;
int sum;
int z;
Can you imagine creating 100 variables? That's just crazy!
4. arrays
ARRAYSEnter Arrays!
Arrays are like a bunch of memory cells located contiguously, i.e., located right next to each
other in memory.
5. arrays
ARRAYS• Let’s say you want to store 5 values.
• You give them a single name, like 'myArray',
and ask the computer for 5 memory slots.
• The computer allocates you 5 contiguous
drawers, and each drawer gets its own
address.
• However, it only tells you the address of the
first drawer in the sequence.
6. Array Declaration
ARRAY DECLARATIONtype var_name[size];
• Array is a data structure of related data items, i.e. it is a set of variables of the same type
• Array is a static entity, i.e. same size throughout program.
• Arrays must be explicitly declared providing a constant size.
* Constants are initialized in declaration and never changed
7. Accessing Elements in An Array
ACCESSING ELEMENTS IN AN ARRAY• In an array, elements refer to the individual values
that you store within array.
• To access a specific element in the array, you need to
refer to its index.
• The index represents the position of an element within
the array.
• Array indices start from 0.
• For an array with n elements, the last element is
located at index n-1.
int myArray[5];
10
8
54 -4
6
8. Array Initialization
ARRAY INITIALIZATIONtype var_name[size];
var_name[index] = value;
• By default, local variables store “garbage” values, whereas global variables are initialized to
zero
• One can initialize an array during its declaration by providing a list of initializers
int arr[5] = {2, 5, 1, 8, 13};
int arr[ ] = {2, 5, 1, 8, 13};
• In case an initializer list is empty all elements are initialized to 0
int arr[5] = {75, 25};
int arr[5] = {};
9. Array & Loops
ARRAY & LOOPSSince an access to a particular element is gained using
an index, one can use a control variable as index of an
array
10. Null terminated String (char Array)
NULL TERMINATED STRING (CHAR ARRAY)11. Review of Chars
REVIEW OF CHARSThe char data type is used to store a single
character. The character must be surrounded
by single quotes, like 'A' or ‘c’ or ‘&’ etc.
Alternatively, you can use ASCII values to
display certain characters:
12. Null terminated String (char Array)
NULL TERMINATED STRING (CHAR ARRAY)• Using a character array can be simplified using C style string
• The C style string is usual char array that ends with null terminator (i.e. ‘\0’, 0, null)
• One can use input and output streams to work with
entire character array
• Null terminator stops processing further data
software