Beruflich Dokumente
Kultur Dokumente
Topics Covered
CPU 1021
1020
RAM
4
I/O Devices
3
2
1
0
1024
1023
1024
1022
1023 1st Run
1021
1022
1020
1021
1020 4
3
4
2
3
1
2
0
1
0
Ghulam Ishaq Khan Institute of Science and Technology, Topi
Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing
5 ?
2
1
0
The variable value and v are two local variables with different scope.
If we print the addresses of both variables, they are different.
The variable value and v are aliases and if we print the addresses of
both variables, they are same.
Arrays in C++
Arrays in C++
• An array allows you to store and work with multiple values of the
same data type.
• int hours[6]; //6 locations each of 4 bytes
Memory Addressing
Arrays, Pointers, Strings
1022
n 100
1021
1020
Array Addressing
int attendance[10];
1023
4 function1(…) function2(…)
3
2
1
0
Take the starting address (base address) and the
total size (10) and do the required computation.
Memory Addressing
Pointer Variables
1023
1022
1021
1020
x 25 4
3
2
1
0
“Hello !”
“My Registration Number is 2017000”
• Strings
• Arrays of characters
• All strings end with null ('\0')
• Examples:
char string1[] = "hello";
char string1[] = { 'h', 'e', 'l', 'l', 'o',
'\0’ };
• Subscripting is the same as for a normal array
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
Sorting Arrays
• Sorting data
• Important computing application
• Virtually every organization must sort some data
• Massive amounts must be sorted
• Bubble sort (sinking sort)
• Several passes through the array
• Successive pairs of elements are compared
• If increasing order (or identical), no change
• If decreasing order, elements exchanged
• Repeat these steps for every element
Sorting Arrays
• Example:
• Original: 3 4 2 6 7
• Pass 1: 3 2 4 6 7
• Pass 2: 2 3 4 6 7
• Small elements "bubble" to the top
References
Dietal and Dietal : How to Program C++
3rd Edition