Lecture22 25
Lecture22 25
(CS-102)
• Arrays
• Definition and Basics of Arrays
• Example
Arrays in C++
Declare Variables -What We Have So for
• Declare 10 integers and print on screen.
• Read 100 numbers from user and find their sum.
void main {
int var1, var2, var3, var4, var5, var6, var7, var8, var9,var10;
• In Standard C++ the array is not the only way to group elements of
the same type. (vector is another way)
C++ Arrays
• Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
• To declare an array, define the variable type, specify the name of the
array followed by square brackets and specify the number of
elements it should store:
string cars[4];
• We have now declared a variable that holds an array of four strings.
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
Output:
Volvo
Change an Array Element
• To change the value of a specific element, refer to the index number:
• Example
• cars[0] = "Opel";
• Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}
Output: Opel
Loop Through
You can loop through thean Array
array elements with the for loop.
The follow
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << cars[i] << "\n";
}
return 0;
}
ing example outputs all elements in the cars array:
Output
Volvo
BMW
Ford
Mazda
The following example outputs the index of each element together with its value:
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
return 0;
}
Output
0: Volvo
1: BMW
2: Ford
3: Mazda
Omit Array Size
• You don't have to specify the size of the array. But if you don't, it will
only be as big as the elements that are inserted into it:
• string cars[] = {"Volvo", "BMW", "Ford"}; // size of array is always 3
• This is completely fine. However, the problem arise if you want extra
space for future elements. Then you have to overwrite the existing
values:
• string cars[] = {"Volvo", "BMW", "Ford"};
string cars[] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
• If you specify the size however, the array will reserve the extra space:
• string cars[5] = {"Volvo", "BMW", "Ford"}; // size of array is 5, even
though it's only three elements inside it
• Now you can add a fourth and fifth element without overwriting the
others:
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[5] = {"Volvo", "BMW", "Ford"};
cars[3] = "Mazda";
cars[4] = "Tesla";
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
Volvo
BMW
Ford
Mazda
Tesla
Omit Elements on Declaration
int main() {
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
cars[4] = "Tesla";
for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
Output:
Volvo
BMW
Ford
Mazda
Tesla
Arrays- Introduction
• Arrays are fixed-size collections consist of the data items of the same
type.
• Array has same properties as for variable in C++ (type, name, size)
int arrayVar1[5];
arrayVar1[0]=10;
arrayVar1[1]=20;
arrayVar1[2]=30;
arrayVar1[3]=40;
arrayVar1[4]=50;
return 0;
}
Array – Example Array subscripting operator
int arrayVar1[5];
Type
int, float, double, char
Size/dimension
Name Must be unsigned integer value
Same rules as for variables
Arrays – Memory View
Name
memory
arrayVar1[0] 10
arrayVar1[1] 20
arrayVar1[2] 30 arrayVar
arrayVar1[3] 40 1
arrayVar1[4] 50
Index
Start from zero to size-1
Declaration of Arrays Datatype ArryName [Int_expression_size]
• Array must be declared before its first use (as for variables)
int arrayVar1 [ 50 ] ; char myChar[20]
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
• If too many a syntax error is produced
27
Limitations of array
• We must know in advance that how many elements are to be stored in
array.
• Array is static structure. It means that array is of fixed size. The memory
which is allocated to array can not be increased or reduced.
B
A 1 D
C
B
2
Homework
Problem#1
Problem#2:
Write a program that take 10 integer from user and find the maximum
and minimum using arrays;.