Chapter 05
Chapter 05
Samilen
1
Array
• is a series of elements of the same type placed
in contiguous memory locations
• referenced by adding an index to a unique
identifier.
• in arrays the First index is always 0
• Example, an array to contain 5 integer values
of type int called billy could be represented
4
Initializing Arrays
• In both cases, local and global, when we declare
an array, we have the possibility to assign initial
values to each one of its elements by enclosing the
values in braces { }.
5
• The amount of values between braces { }
must not be larger than the number of
elements that we declare for the array
between square brackets [ ].
– Value b/n {} !>than elements b/n []
• C++ allows the possibility of leaving the
square brackets empty [ ].
int billy [] = { 16, 2, 77, 40, 12071 };
billy[2] = 75;
• Example, to pass the value of the third element of billy
to a variable called a, we could write:
a = billy[2];
7
• Notice that the third element of billy is specified
billy[2], since the first one is billy[0], the second
one is billy[1], and therefore, the third one is
billy[2].
• accessing out-of-range elements do not cause
compilation errors but can cause Runtime errors.
• the two uses that bracket [ ] have related to
arrays:
– They perform two different tasks:
• to specify the size of arrays when they are declared;
• to specify indices for concrete array elements
8
Example
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;
9
Example
11
Reading a String from the keyboard
cout << “Here is your string: “; Solution: use another c++ library
cout << str; function, gets()
return 0; } 12
Some C++ Library Functions for Strings
13
Copying of string Length of string
strcpy (to_string, from_string ) strlen (str) returns the length of the string
pointed to by str,
i.e., the number of characters excluding the
#include <iostream.h> null terminator.
int main() #include <iostream.h>
{ int main()
char a[10]; {
strcpy(a, “hello”); char str[80];
cout << a; cout << “Enter a string: “;
return 0; gets(str);
} cout << “Length is: “ << strlen(str);
Return 0; }
14
String concatenation
Strcat(string_1,string_2);
The strcat() function appends s2 to the end of s1. String s2 is
unchanged.
#include<iostream.h>.
int main() output
{ hello there
char s1[21], s2[11]; there
strcpy(s1, “hello”);
strcpy(s2, “ there”);
strcat(s1, s2);
cout << s1 << endl;
cout << s2 << endl;
return 0;}
15
Two Dimensional Arrays
• is a list of one dimensional arrays.
• Syntax:
Type var_name [index] [index];
18
• Multidimensional arrays are just an abstraction
for programmers, since we can obtain the same
results with a simple array just by putting a
factor between its indices:
#define WIDTH 5#
#define WIDTH 5 define HEIGHT 3
#define HEIGHT 3 int jimmy [HEIGHT * WIDTH];
int jimmy [HEIGHT][WIDTH]; int n,m;
int n,m; int main (){
int main (){ for (n=0;n<HEIGHT;n++)
for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++)
for (m=0;m<WIDTH;m++) {
{ jimmy[n][m]=(n+1)*(m+1); jimmy[n*WIDTH+m]=(n+1)*(m+1
} return 0;} );
} return 0;}
20
Structure
Introduction
• C/C++ arrays allow you to define variables that combine
several data items of the same kind, but structure is
another user defined data type which allows you to
combine data items of different kinds.
• Structures are used to represent a record, suppose you
want to keep track of your books in a library. You might
want to track the following attributes about each book
• Title
• Author
• Subject
• Book ID
• Structure is a collection of variables of different data types
under a single name.
Cont..
• For example: You want to store some information about a
person: his/her name, citizenship number and salary. You
can easily create different variables name, citNo, salary to
store these information separately.
• However, in the future, you would want to store
information about multiple persons. Now, you'd need to
create different variables for each information per
person: name1, citNo1, salary1, name2, citNo2, salary2
• You can easily visualize how big and messy the code
would look.
• Also, since no relation between the variables (information)
would exist, it's going to be a daunting task.
Cont..
• A better approach will be to have a collection of
all related information under a single name Person,
and use it for every person.
• Now, the code looks much cleaner, readable and
efficient as well.
• This collection of all related information under a
single name Person is a structure.
How to declare a structure in C++ programming?
29
How to access members of a structure?
cout << "Name: " << people[1].name << ", Age: " << people[1].age << endl;
34
Nested structure
• nested structure is a structure that is defined inside another
structure. This is also known as a "nested struct" or a "struct
within a struct".
• To define a nested structure in C++, you can simply declare a
structure inside another structure's definition. Here is an
example:
struct outer {
int a;
struct inner {
int b;
int c;
} i;
};
35
Cont..
• In this example, the outer structure contains an integer
a, as well as an inner structure i that has two integer
members, b and c.
• To access the members of the nested structure, you can
use the dot operator twice. For example, to assign a
value to b, you can do:
outer o;
o.i.b = 10;
• In this example, we create an instance of the outer
structure called o, and then we set the value of o.i.b to
10.
• Nested structures are useful when you need to group
related data together, and you want to keep the code
organized and easy to read.
36
Structure pointer
• Structure pointer in C++" refers to the use of
pointers in C++ programming language to access
and manipulate data stored in structures.
• A pointer is a variable that stores the memory
address of another variable.
• It allows us to indirectly access and manipulate the
value stored in a particular memory location.
• In C++, we can declare a pointer to a structure
using the "->" operator, which lets us access the
members of the structure through the pointer.
37
Cont..
• Here's an example of how to declare and use a structure pointer in C++:
#include <iostream>
using namespace std;
// declare a structure
struct Person {
string name;
int age;
};
int main() {
// declare a structure variable
Person p1 = {“Lenjissa", 30};
// declare a pointer to the structure
Person *ptr = &p1;
// access the members of the structure using the pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
return 0;
}
38
Cont..
• In this example, we first declare a structure named
Person that contains two members: name and age.
• Then, we declare a structure variable p1 and
initialize it with the values “Lenjissa" and 30.
• Next, we declare a pointer ptr to the Person
structure and initialize it with the address of p1
using the & operator.
• Finally, we use the -> operator to access the
members of the structure through the pointer and
print their values to the console.
39
Structure to pointer
• we can declare an array of structures to store
multiple instances of the same data type. We can
also use pointers to access and manipulate the data
stored in the array.
• Here's an example of how to convert a structure
array to a pointer in C++:
40
Cont..
#include <iostream>
using namespace std;
// declare a structure
struct Person {
string name;
int age;
};
int main() {
// declare an array of structures
Person people[3] = {{“Jaalu", 30}, {“Meti", 25}, {“Beka", 40}};
// declare a pointer to the first element of the array
Person *ptr = &people[0];
// access the members of the structures using the pointer
for (int i = 0; i < 3; i++) {
cout << "Name: " << (ptr+i)->name << endl;
cout << "Age: " << (ptr+i)->age << endl;
}
return 0;
}
41
Cont..
• In this example, we first declare an array of Person
structures named people with three elements. Each
element contains a name and age member.
• Next, we declare a pointer ptr to the first element of
the people array using the & operator.
• Finally, we use a loop to iterate through each element
of the array and print the values of their members to
the console using the -> operator.
• We use (ptr+i) to access the next element of the array.
Or
• We can also increment the pointer ptr by one in each
iteration to access the next element of the array. Look
the following example
42
Cont..
#include <iostream>
using namespace std;
// declare a structure
struct Person {
string name;
int age;
};
int main() {
// declare an array of structures
Person people[3] = {{"Jalu", 30}, {“Meti", 25}, {“Beka", 40}};
// declare a pointer to the first element of the array
Person *ptr = &people[0];
// access the members of the structures using the pointer
for (int i = 0; i < 3; i++) {
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
ptr++;
}
return 0;
} 43
C++ Structure and Function