Chapter 5
Array, String and Function
May 18, 2017 EC
University of Gondar
Gondar , Ethiopia
By Amanuel D
Array
• An array is a collection of elements of the
same data type stored in contiguous
memory locations.
• It allows you to store multiple values under
one variable name.
• Each element is accessed by an index (starting
from 0).
Contiguous Memory
• "Contiguous memory" means that array
elements are stored side-by-side in memory,
without gaps.
Array Declaration
• 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:
data_type array_name[size];
Example:
int numbers[5]; // Declares an array of 5
integers
Array Initialization
• You can initialize an array when declaring:
Example:
int numbers[6] = {2, 4, 8, 12, 16, 18};
Or Let the compiler determine size:
int numbers [] = {2, 4, 8, 12, 16, 18}; // size = 6
Accessing Array
Elements
• You can access an array element by referring to
the index number inside square brackets [].
• NB: array index start from 0 for the first
element
Example:
int numbers[6] = {2, 4, 8, 12, 16, 18};
cout<<numbers[0]; // displays 2
cout<<numbers[1]; // displays 4
• To access last element the index will be size - 1
cout<<numbers[5]; // displays 18
Traverse Array
• Traversing means visiting each element one by
one.
• You can traverse through the array elements
with Loops.
Example:
int arr[6] = {2, 4, 8, 12, 16, 18};
for (int i = 0; i < 6; i++){
cout << arr[i] << " ";
}
Length of Array
• To get the size of an array, you can use
the sizeof() operator:
sizeof(arr) / sizeof(arr[0]);
Example:
int num[5] = {10, 20, 30, 40, 50};
int size = sizeof(num) / sizeof(num[0]);
cout<< size; // displays 5
Multidimensional
Arrays
• Two-Dimensional Array
• Create Multidimensional Array
data_type array_name [size1][size2];
Example:
int matrix[2][3]= { {1, 2, 3}, {4, 5, 6} };
Accessing elements:
cout << matrix[1][2]; // prints 6
String
• strings are sequences of characters that are
used to store words and text.
• C++ provides two main ways to handle
strings:
1. C-style strings (character arrays):
• These are arrays of characters
• Example:
char name[10] = "John";
cout << name; // Output: John
cout << name[1]; // Output: o
String
2. C++ string class (recommended):
• C++ standard library provides the string class in
the <string> header.
• Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = “John";
cout << "Name: " << name << endl;
return 0;
}
String
• To access length of the string use
nameOfString.length();
• To accept string as input
cin<<nameOfString;
• To accept string as input with spaces allowed
getline(cin, nameOfString);
• Concatenation
string1 + string2;
String
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName, lastName;
cout << "Enter your full name: ";
getline(cin, firstName); // reads with spaces
cout << "Length: " << firstName.length() << endl;
cout << "First letter: " << firstName[0] << endl;
return 0;
}
Function
• A function is a block of code that performs a specific task.
• A function is a block of code which only runs when it is
called.
• It allows modular programming and avoids repetition.
• Benefits:
• Code reuse
• Simplifies large programs
• Easy to test and debug
• Example
void greet() {
cout << "Hello from function!" <<endl;
}
Function Syntax
• General Syntax:
returnType functioName(parameters) {
// body of the function
}
returnType: The data type returned
functionName: Identifier
parameters: (optional) input values
body: Code to execute
• NB: Functions must be called to perform their task
Types of Functions
• Built-in functions – like sqrt(), pow(), abs() etc.
• User-defined functions – created by you to
perform specific tasks.
Example: user defined function to calculate the sum
of two integers.
int add(int x, int y){
int sum= x + y;
return sum;
}
void Function (No return
type)
• If the function does not return something, then the
return type should be void.
• Example: This function performs an action but does
not return any value.
#include <iostream>
using namespace std;
void greetUser() {
cout << "Hello, User!" << endl;
}
int main() {
greetUser();
return 0;
}
Function with Return
Value
• If the function returns something you have to
specify return type.
#include <iostream>
using namespace std;
int add(int a, int b) { // Return type is int
return a + b; // and we return integer since return
type is int
}
int main() {
int result = add(5, 3);
std::cout << "Sum: " << result << std::endl;
return 0;
}
Function with No
Parameters
• We can create a function with no parameters by
using empty brackets like ().
returnType functionName(){ // function with no
parameters ().
// body of the function
}
#include <iostream>
using namespace std;
void welcome() {
cout << "Welcome to C++ Functions!" << endl;
}
int main() {
welcome(); // function call
return 0;
Function with Parameters
#include <iostream>
using namespace std;
void greet (string name) {
cout << "Hello, " << name << "!" << endl;
}
int main() {
greet("Abebe ");
return 0;
}