chapter 2
chapter 2
Collage of Informatics
Second year software engineering students
Course name: Fundamentals of Programming II
Course code :SEng2021
Chapter Two
Arrays and Structure
BY:Degaga .A
Topics we will cover in this chapter are:
Data types.
Homogeneous and heterogeneous data types
Declaring, accessing and accessing arrays
Declaring, accessing and accessing structure.
Difference b/n Arrays and Structure data types
String manipulation using arrays.
BY:Degaga .A
Data Types
It defines the kind of data a variable can hold and the operations that can be
performed on that data.
It determines how much space is needed to store the data and what kind of values
it can take.
C++ supports several types of data types, which can be broadly categorized into built-in
• Structures
It is the simplest form of an array and is used to store a linear collection of data.
Example:int numbers[5] = {1, 2, 3, 4, 5};
Declaration of one dimensional array
Declaration of Arrays:
• An array declaration is very similar to a variable declaration.
• First a type is given for the elements of the array, then an identifier
for the array and,within square brackets, the number of elements in
the array.
• The numberof elements must be an integer.
• general format of array declaration is :
data_type (array name) [ size];
in this case size is an integer which shows number of the elements.
example: int temp[5];
Initializing one dimensional array
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}; it indicates 3 rows and 4 column array.
Declaring of two dimensional array
structure definition
struct structname
tag
{ datatype1 variable1;
Membe
datatype2 variable2; rs
};
Example :
struct student
{
int id;
char name[15];
};
Method 3:
struct student
{
char id_num[5];
char name[10]; structure definition
char gender;
int age;
}
studno_1, studno_2; // structure variable
:method 4
struct student
{
char id_num[5];
char name[10]; structure definition
char gender;
int age;
};
struct student
{
int id;
char name[15];
};
student std1={“Gizachew", 2337};
you can assign fewer values than member
variables
If you try to assign more values than member
variables, you will get a compiler error
#include <iostream>
int main()
{
cout<<std1.a;
cout<<std1.x; accessing structure.
cout<<std1.y;
return 0;
}
struct student{
int id;
string name;
char gender;
int age;
} studno_1, studno_2;
int main()
{
studno_1.id=123;
studno_1.age= 9;
studno_1.gender='m'; another way of struct intial
studno_1.name="bona";
struct employee
{
int idd;
string name;
char grade;
};
emp.name="saamuhel";
}
Struct
{ //no tag
int year;
char grade;
string id;
}studno_1, studno_2;
int main()
{
studno_1.id="stud/123";
studno_1.grade='a'; structure intialization.
studno_1.year=2022;
Return 0;
}
Accessing Members
You can treat the members of a struct just like variables.
To access members of structure we use the name of the record( stru
variable), the name of the member and dot between record and
General syntax for accessing members of structure:
members.
Structure_variable.member_varia
ble;
struct{ //no tag
int year;
char grade;
string id;
}studno_1, studno_2;
accessing the values of member
studno_1.id
int main()
{ studno_1.grade
studno_1.id="stud/123"; studno_1.yea
studno_1.grade='a'; intialization
studno_1.year=2022;
Difference b/n Arrays and Structure data types
array Structure
Type of Data Stored Arrays store multiple elements of the same data type Structures store multiple elements that
can be of different data types
Memory Allocation: Arrays allocate memory in a contiguous block, and Structures allocate memory for each
each element of the array is placed one after the other member individually. The memory
in memory. The size of an array is fixed once declared. layout of the members can vary.
Structure is not allocated in
contiguous.
Accessing Elements Array elements are accessed using an index (zero- Structure members are accessed using
based) the dot operator (.)
Size The size of an array is fixed when it is declared. It The size of a structure depends on the
cannot be resized dynamically unless you use dynamic number and type of members.
memory allocation.
String manipulation
In many programming languages, strings are represented as an array of
characters, and by treating them as such, you can manipulate strings
element by element (i.e., character by character).
String manipulation using arrays refers to the process of performing operations
on strings. The task of string manipulation can be achieved using loop and c++
build in function
String manipulation in C++ can be performed using both C-style Character Arrays
and the more modern string class.
Each approach has its own set of functions and methods for handling strings,
allowing for a variety of operations: those operation are:
Accessing individual characters.
Modifying or replacing characters.
Reversing the string.
Concatenating strings.
Finding substrings.
Copying strings.
Changing the case of characters and more
In C++, string manipulation using arrays can be achieved by either manipulating using
#include <iostream>
using namespace std;
int main()
{
string str = "Hello";
char c = str[1]; // 'e‘
Cout<<c;
Return 0;
}
3.Reversing a String
You can reverse a string using the reverse() function from the
<algorithm> library.
#include <iostream>
#include <string>
#include <algorithm>
int main()
string hello = "Hello, World!";
reverse(hello.begin(), hello.end());
cout << "Reversed string: " << hello << endl;
return 0;}
4.String Length
To get the length of a string, use the length() or size()
function
#include <iostream>
#include <string>
using namespace
int main()
{ string hello = "Hello, World!";
cout << "Length of the string: " << hello.length() <<
endl;
return 0;}
5.Extracting a substring
A substring of a string can be extracted(copied) and assigned to another.
stringName.substr(int copy_start_position, int number_of_elements);
//sample code
#include<iostream.h>
using namespace std;
int main()
{
string s1="Gizachew";
S2=ac
string s2=s1.substr(3,2);
cout<<s2;
}
6.Erasing a substring
A substring can be deleted from a string.
stringName.erase(int erase_start_position, int number_of_elements);
//sample code
#include<iostream.h>
using namespace std;
int main(){
string s1="Gizachew"; Gizhew
s1.erase(3,2);
cout<<s1;
7.Replacing a substring
A substring can be deleted and replaced by another substring.
stringName.replace(int replace_start_position, int number_of_elements, string s);
//sample code
#include<iostream.h>
using namespace std;
int main(){
Gizachew
string s1="Gizxyhew";
s1.replace(3,2,”ac”);
cout<<s1;
}
String manipulation using C-style Character Arrays:
C-style strings are essentially arrays of characters terminated by a null character ('\0').
Example char str[]=“hello” which equivalent to char str[]={ ‘h’,’e’,’l’,’l’,’o’,’\0’ } ,in this case the
length of character is 6.
Using toupper() and tolower() functionwe can automatically handle the conversion.
#include <stdio.h>
#include <ctype.h> // For toupper() and tolower()
int main()
{ char myString[] = "Hello, World!"; //C-style array character.
for (int i = 0; myString[i] != '\0'; i++)
{
myString[i] = toupper(myString[i]);
}
for (int i = 0; myString[i] != '\0'; i++) {
myString[i] = tolower(myString[i]);
}
.Counting Vowels and Consonants using loop
}
4.String Length using loop
we can calculate the length of the string manually by iterating over the
character array until you reach the null terminator ('\0').
#include <iostream>
using namespace std;
int main()
{
char myString[] = "Hello, World!";
int length = 0; // Calculate length manually
while (myString[length] != '\0') // in this case null terminator not
counted.
So it returns 13.
{
length++; }
5.Finding Frequency of a Character
You can find the frequency of a particular character in a string.
#include <iostream>
using namespace std;
int main() {
char myString[] = "Hello, World!"; //char array
char target = 'l'; int count = 0; //target to find frequency of l.
for (int i = 0; myString[i] != '\0'; i++)
{ if (myString[i] == target) { count++; }
}
cout << "Frequency of '" << target << "' in the string: " << count
<< endl; return 0;}
Reading assignment
1.dynamic array
2.other string manipulation technique.
Tokenizing a String
Reversing a String using c-style character using loop
Counting vowel and consonant without using loop
Searching a substring using string class.
Uppercase and lowercase using string class
Calculate String Length for c-style array without using loop
Thank you
?