0% found this document useful (0 votes)
7 views

Arrays: Arrays Strings. Array Arrays Define Type Name Brackets Number Arr Int Allocated Space Indexes

Uploaded by

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

Arrays: Arrays Strings. Array Arrays Define Type Name Brackets Number Arr Int Allocated Space Indexes

Uploaded by

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

Arrays

Greetings, dear students, I hope you are on the good mood after writing quiz. I will check your writings as
quickly as I can and I will upload points on sis. If after viewing your points, you think that you deserved more
then what I have written, or less even, you are free to contact me via e-mail. Now let’s start explaining new
materials.
Today’s new materials are Arrays and Strings. These 2 data types of CPP are used to save big data in our
programs and gives us ability to work on them and get results. Let’s start with an array.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each
value. To declare an array, firstly, we must define the variable type, then specify the name of the array,
followed by brackets with the number of elements it should store in them. Now, it is time to look at examples:
1. Int arr[3]; <- with this, we created single variable called arr, which is of type int and has 3 allocated
space for storing different values in it.
2. Int arr[3]={1,2,3}; <- with assignment operator and braces, we can also save values in array.
It must be noted that, if we want to access the stored information, we must use indexes. Indexes are being
used as our house addresses in real life. The best example of it would be hotels. Imagine that array variable is 1
floor of hotel, each allocated space in array is room and we can use these rooms as we see fit: saving
information, changing value or leaving it empty for future use.
If we want to use 1 specific allocated space, we must use it’s index like this: arr[1]=4; you must remember that
indexes in C++ programming always starts from 0. So, 0 indicates first allocated space, 1 second space, 2 third
and so on. If you have an array of size n, that the last allocated space will be on index n-1.
When programmers work with arrays, they always use loops, otherwise it would be almost impossible to do
job properly. Using loops is simple, for example. We want to fill array with some values, we can do following:
1. Int arr[9];
2. For (int i=0;i<9;i++){
3. Arr[i]=1; // you can assign any value here, but you must understand that iterator variable works as an
index here. With this, we are going though array allocated spaces and assigning them values one by
one
4. }
5. For (int i=0;i<9;i++){
6. Cout << arr[i] << “ “;
7. }
After you execute this code, you must see the result: 1 1 1 1 1 1 1 1 1.
Array in C++ has 1 built-in function, in other words, ability which may come in handy sometimes. This
function is size of. Let’s take a block of code and analyze it:
1. int myNumbers[5] = {10, 20, 30, 40, 50};
2. cout << “the size of array is ” <<sizeof(myNumbers) <<endl;
3. cout << “the size of 1 element is “ << sizeof(myNumbers[0]) <<endl;
4. int getArrayLength = sizeof(myNumbers) / sizeof(myNumbers[0]);
5. cout << getArrayLength;
as you can understand, function sizeof(), requires 1 input. It may be the array variable name, or
member of the array. If it is first one, then we get result, which is the size of allocated space in memory
in bytes, and if it is the second one, then we will get size of chosen member in bytes. The size of int
data type variable is 4 bytes, and the whole size of the array we created is 20 bytes. From this, we can
deduce the number of members in array on the 4th line.
Previously, we talked about the arrays and 1 floor of hotels. Did u know that we can also create array
with multiple floors? These kind of arrays are called multi-dimensional arrays. For instance, imagine
that that you are working with an excel document. As you know, in excel, each cell has its column and
row indicator and if we want to use 1 cell, we must reference them with these indicators. Same thing
happens with multi-dimensional arrays.
1. Int two_dimensional_array[2][3];
2. For(int i=0; i<2; i++){
3. For (int j=0; j<3; j++){
4. two_dimensional_array[i][j] = 3*i+j;
5. }
6. }
7. Cout << two_dimensional_array[1][2] <<endl;
These was what you needed to know about arrays and now it is time to move to our next data type,
which is call Strings.

Strings
Strings are data types, which are used for storing text and characters. If you remember, on our first
lecture we wrote: cout << “Hello, World!” << endl; <- this “Hello, World” is a string. We can save this
text in string type variable. Let’s test it out:
1. string greeting = “Hello, World!”;
2. cout << greeting << endl;
if we want to use string data type and all the benefits it has, we must write on the 2 nd line:
#include <string>
When we have 2 or more string and we want to use them together, we must use concatenation
methods. We can either use + sign or string built-in function called “append”.
1. string firstName = “Luka”;
2. string lastName = “Rakviashvili”;
3. string fullName1 = firstName + “ “ + lastName;
4. string fullName2 = firstName.append(lastName);
5. cout << fullName1 << endl;
6. cout << fullName2 << endl;
you must also remember that if you create string data types and save “10” and “20”, after addition with
+ sign, you will get “1020” and NOT “30”;
String built-in functions
size() and length():
1. string fullName = “Luka Rakviashvili”;
2. cout << fullName.length() << endl << fullName.size() << endl;
this block of code must print 17 twice. As you would guess, both of these are used to get the
size/length of the string type variable, and both return the same result. It is totally up to you which one
to use.
Accessing string characters:
String in C++ are treated as an array of chars. Because of this we have a 2 way of accessing these
characters. Regular way, like we access int type data in arrays of with string built-in function:
1. string name = “Luka”;
2. cout << “The First Letter in name is: “ << name[0] <<endl;
3. cout << “The Last Letter in name is: “ << name.at(name.length()-1) <<endl;
special characters in strings
as you can already see, in C++ we use special characters for different purposes, for example:
 double quotes are used for writing string data
 single quotes are used for writing char data
 \n is being used for new line
 \t is being used for tabs
If we want to use these symbols in our string type data, then we must follow protocol and use
backslash escape character. For example:
1. string comments = “Please, do not use \”, \’ or \\ in input text”;
2. cout << comments << endl;
here are the built-in function of strings, which are imported from a library of <string>:
empty(), substr(), find(), rfind(), replace(), insert(), erase(), compare().
1. string name = "Luka";
2. string surname = "Rakviashvili";
3. cout << "Checking if variable name is empty: " << name.empty() <<endl;
4. cout << "substring of 5 characters from 2nd index is: "<< surname.substr(2,5) <<endl;
5. cout << "first finding of character I is on index: " << surname.find('i') <<endl;
6. cout << "last finding of character I is on index: " << surname.rfind('i') <<endl;
7. cout << name.replace(0,4,"Toko") <<endl;
8. name.insert(2,"rni").replace(6,1,"e");
9. cout << name <<endl;
10. name.erase(2);
11. cout << name <<endl;
12. name += "ko";
13. cout << name.compare("Toko") << " " << name.compare("Tornike") <<endl;

You might also like