0% found this document useful (0 votes)
7 views22 pages

CPP CH#4

Uploaded by

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

CPP CH#4

Uploaded by

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

SALALE UNIVERSITY

COLLEGE OF ENGINEERING AND TECHNOLOGY


(Pre – Engineering)

Computer Programming
(ECEg 1013)

Course Instructor:
Haylemaryam G. (M.Sc. in ECEg)

By: Haylemaryam G. (MSc. in ECEg) 1


CHAPTER IV
Arrays and String Manipulation
➢ Array Definition, Array Referencing
➢ One dimensional and multidimensional arrays
➢ Strings: Definition, accessing Strings
➢ Arrays and functions: Passing array as argument to a function

By: Haylemaryam G. (MSc. in ECEg) 2


Array Basics
➢ An array is used to process a collection of data all of which is of the same
type.
➢ A collection of identical data objects, which are stored in consecutive
memory locations under a common heading or a variable name.
➢ In other words, an array is a group or a table of values referred to by the
same name.
➢ The individual values in array are called elements.
➢ Array is a data structure that represents a collection of the same types of
data.
➢ An array is a collection of data elements that are of the same type (e.g., a
collection of integers, collection of characters…).
By: Haylemaryam G. (MSc. in ECEg) 3
➢ In C++, an array consisting of five variables of type int can be declared as
follows:
int score[5];
➢ This declaration is like declaring the following five variables to all be of type
int:
score[0], score[1], score[2], score[3], score[4]
➢ These individual variables that together make up the array are referred to in
a variety of different ways. We will call them indexed variables, though they
are also sometimes called subscripted variables or elements of the array.
➢ The number in square brackets is called an index or a subscript. In C++,
indexes are numbered starting with 0, not starting with 1 or any other
number except 0.
By: Haylemaryam G. (MSc. in ECEg) 4
Array Visualization

Specifies an array of We are creating a


variables of type int new array object

int primes[10]; // An array of 10 integers

The array object is of


The name type int and has index values
of the array ten elements

primes[0] primes[1] primes[2] primes[9]


By: Haylemaryam G. (MSc. in ECEg) 5


Array Declaration
➢ The array must be declared before one uses in like other variables. In the
array declaration one must define.
1. The type of the array (i.e. integer, floating point, char etc).
2. Name of the array,
3. The total number of memory locations to be allocated or the maximum
value of each subscript. i.e. the number of elements in the array.

By: Haylemaryam G. (MSc. in ECEg) 6


One Dimensional Array
➢ Array declarations use square brackets.
Syntax:
data_type name [array_size];
➢ For example:
int prices [10];
char names [20];
Array Indexes
➢ Every compartment in an array is assigned an integer reference. This
number is called the index of the compartment.
➢ Important: In c++ (and most other languages), the index starts from 0 and
ends at N-1, where N is the size of the array.
By: Haylemaryam G. (MSc. in ECEg) 7
Accessing Array Elements
➢ To access an item in an array, type the name of the array followed by the
item’s index in square brackets.
➢ For example, the expression:
names[0]
will return the first element in the names array.

Another way to construct arrays


➢ You can also specify all of the items in an array at its creation.
➢ Use curly brackets to surround the array’s data and separate the values with
commas: Example:
int powers [5] = {0, 1, 10, 100,200};

By: Haylemaryam G. (MSc. in ECEg) 8


Example 1:
#include<iostream> Output
using namespace std;
int main()
{
int fibs [10];
fibs[0] = 1;
fibs[1] = 1;
for(int i=2; i<10; i++)
{
fibs[i] = fibs[i-2] + fibs[i-1];
}
for(int i=0; i<10; i++)
{
cout<<fibs[i]<<endl;
}
return 0;
}
By: Haylemaryam G. (MSc. in ECEg) 9
Example 2: //Reads in five scores and shows how much each score differs from the highest score.
#include<iostream> Output
using namespace std;
int main()
{
int i, score[5], max;
cout<<"Enter 5 scores:\n"; cin>>score[0];
max=score[0];
for (i=1; i<5; i++)
{
cin>>score[i];
if (score[i]>max)
max=score[i];
}
cout<<"The highest score is "<<max<<endl<<"The scores and
their\n"<<"differences from the highest are:\n";
for(i= 0; i<5; i++)
cout<<score[i]<<" off by "<<(max-score[i])<<endl;
return 0;
}
By: Haylemaryam G. (MSc. in ECEg) 10
Example 3: What is the value of c after the following code segment?
#include<iostream> Output
using namespace std;
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[3] = {11, 12, 13};
int c[5];
for (int j=0; j<3; j++)
{
c[j] = a[j] + b[j];
cout<<c[j]<<" ";
}
return 0;
}

By: Haylemaryam G. (MSc. in ECEg) 11


Example 4: //Program to convert the decimal number into binary number.
#include<iostream>
using namespace std;
int main()
{
int a[50], i=0, n, r; cout<<"Enter any number ="; cin>>n;
while(n>=1)
{
r=n%2; n=n-r; n=n/2; a[i]=r;
i++;
} Output
i--;
while(i>=0)
{
cout<<a[i];
i--;
}
return 0;
}
By: Haylemaryam G. (MSc. in ECEg) 12
Two Dimensional Array
➢ The arrays we've used so far can be thought of as a single row of values.
➢ A 2-dimensional array can be thought of as a grid (or matrix) of values.
➢ Each element of the 2-D array is accessed by providing two indexes: a row
index and a column index.
➢ (A 2-D array is actually just an array of arrays). Example:
Syntax: Two Dimensional Array
data_type name [array_size][array_size];

Syntax: Multi-Dimensional Array


datatype name [array_size][array_size]...[Last_array_size];

By: Haylemaryam G. (MSc. in ECEg) 13


➢ 2-Dimensional Arrays can define one array for multiple sets of data:
• Like a table in a spreadsheet
• Use two size declarators in definition:
➢ We declare a 2D array two sets of square brackets:
Example 1:

double heights [20][55];

➢ This 2D array has 20 rows and 55 columns


Example 2:
value at row index 2, column index 0
is 3.

By: Haylemaryam G. (MSc. in ECEg) 14


Example 3:
const int rows=4, cols=3;
int exams[rows][cols];

➢ Use two subscripts to access element:


exams[2][2]=86;

By: Haylemaryam G. (MSc. in ECEg) 15


➢ Two-dimensional arrays are initialized row-by-row:

const int rows=2, cols=2;


int exams[rows][cols] = {{84, 78}, {92, 97}};

➢ Can omit inner { }, some initial values in a row-array elements without initial
values will be set to 0 or NULL.

By: Haylemaryam G. (MSc. in ECEg) 16


Example 1:
#include <iostream>
using namespace std;
int main()
{
const int NUM_ROWS = 5; const int NUM_COLS = 5;
int total = 0;
int numbers[NUM_ROWS][NUM_COLS] = {{2, 7, 9, 6, 4}, {6, 1, 8,
9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, 2, 7, 4, 1}};
for (int row = 0; row<NUM_ROWS; row++)
{
for (int col=0; col<NUM_COLS; col++)
total += numbers[row][col];
}
cout<<"The total is "<<total<<endl;
return 0; Output
}

By: Haylemaryam G. (MSc. in ECEg) 17


Arrays with Three or More Dimensions (Multi-Dimensional)

➢ Can define arrays with any number of dimensions:

short rectSolid[2][3][5];
double timeGrid[3][4][3][4];

➢ When used as parameter, specify all but 1st dimension in prototype,


heading:

void getRectSolid(short [][3][5]);

By: Haylemaryam G. (MSc. in ECEg) 18


Example:
Output
#include<iostream>
using namespace std;
int score [] = {16,2,13,1,12};
int n, result=0;
int main ()
{
for (n=0; n<5; n++)
{
result += score[n];
}
cout<<result;
return 0;
}

By: Haylemaryam G. (MSc. in ECEg) 19


Arrays and functions: Passing array as argument to a function
➢ We must do when declaring the function is to specify in the argument the
base type for the array that it contains, an identifier and a pair of void
brackets [].
➢ We pass both array and its size to a function array are passed as reference
variable without (&).
Example:
double read(int a[], int size)
{
// any manipulation statement on array
}
➢ When we call function with array parameter we simply pass array name
and size.
read(a, size);
By: Haylemaryam G. (MSc. in ECEg) 20
Example:
#include<iostream>
using namespace std;
void printArray(const int [][3]);
int main()
{
int array1[2][3]={{1,2,3},{4,5,6}}, array2[2][3]={{1,2,3},{4,5}},
array3[2][3]={{1,2},{4}};
cout<<"\nValues in array1 by row are: "<<endl;
printArray(array1); cout<<"\nValues in array2 by row are:"<<endl;
printArray(array2); cout<<"\nValues in array3 by row are:"<<endl;
printArray(array3);
return 0;
}
void printArray(const int a[][3])
{
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
} Output
By: Haylemaryam G. (MSc. in ECEg) 21
Any Questions?
END !

By: Haylemaryam G. (MSc. in ECEg) 22

You might also like