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

programing chap 3

Chapter three covers arrays and strings in C++, explaining the definition, properties, declaration, and initialization of arrays, as well as common errors. It also introduces multidimensional arrays and provides examples of string declaration and initialization. Additionally, the chapter includes assignments related to arrays and strings to reinforce learning.

Uploaded by

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

programing chap 3

Chapter three covers arrays and strings in C++, explaining the definition, properties, declaration, and initialization of arrays, as well as common errors. It also introduces multidimensional arrays and provides examples of string declaration and initialization. Additionally, the chapter includes assignments related to arrays and strings to reinforce learning.

Uploaded by

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

Chapter three

Arrays and Strings


What is An Array?
♣A collection of identical data objects, which are
stored in consecutive memory locations under a
common heading or a variable where each
element can be accessed by a name and an index.
♣ an array is a group or a table of values referred
by the same name.
♣ The individual values in array are called
elements.

Computer programming 2
Cont’d

♣Set of values of the same type, which have a


single name followed by an index
♣ In C++, square brackets appear around the
index right after the name.
♣A block of memory representing a collection of
many simple data variables stored in a separate
array element, and the computer stores all the
elements of an array consecutively in memory.
Computer programming 3
Properties of arrays

♣Arrays in C++ are zero-bounded; that is the index


of the first element in the array is 0 and the last
element is N-1, where N is the size of the array.
♣It is illegal to refer to an element outside of the
array bounds, and your program will crash or
have unexpected results, depending on the
compiler.
♣Array can only hold values of one type.

Computer programming 4
Array declaration
► In the array declaration one must define:
♣The type of the array (i.e. integer, floating point, char
etc.)

♣Name of the array,


♣Number of subscripts in the array
The total number of memory locations to be allocated or
the maximum value of each subscript. So the general
syntax for the declaration is:DataTypename
arrayname [array size];
Example int Age[5];
Computer programming 5
Initializing Arrays

♣A simple variable can be initialized when


it is declared.
♣When array is initialized the values for
the various indexed variables are
enclosed in braces and separated with
commas.

Example: int Age[3]={12,6,8};


Computer programming 6
♣The number of constants in the initialize list
must be less than or equal to size of the array.
♣The other way to initialize is passing a value
for each indexed array

int Age[3];

Age[0]=12;

Age[1]=6;

Age[2]=8;
Computer programming 7
♣An array can be initialized to be all zeros by
using an empty initialization list.

Example
 float salary [] = {0, 0, 0, 0, 0, 0,};

 float salary [6] = {0, 0, 0};

 float salary [6] = {0, 0, 0, 0, 0, 0};

If an array is not initialized it will contain


garbage values.
Computer programming 8
Example

#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<array[2]<<endl;
cout<<array[3]<<endl;
cout<<array[4]<<endl;
return 0;
}
Computer programming 9
//The above code can be easily written as follows:-
#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
for(int i=0;i<=4;i++)
cout<<array[i]<<endl;
return 0;
}
Computer programming 10
// accept series of numbers according to the /user input and display it .
#include<iostream>
using namespace std;
int main()
{
int n,num[100];
cout<<"enter the number of array you want\n";
cin>>n;
for (int i=0;i<=n-1;i++)
{
cout<<"enter your "<< i <<" array ";
cin>>num[i];
}
cout<<"the contents of the array are\n";
for (int j=0;j<=n-1;j++)
cout<<"the "<<j <<"array is " <<num[j]<<endl;
return 0;
}

Computer programming 11
Common Errors on Array
♣The most common programming errors
made when using array is attempting to
reference a non-exist array index.
♣ Array indexes get out of rang most
commonly at the first or last iteration of a
loop that process the array.
♣Remember the array always starts on its
first iteration that is 0. 12
Computer programming
Multidimensional Array
♣ A 1D array is a simple data structure that stores a collection of
similar type data in a contiguous block of memory while the 2D
array is a type of array that stores multiple data elements of the
same type in matrix or table like format with a number of rows
and columns. Thus, this is the main difference between 1D and

2D array. .
♣ Thus a two dimensional array will require two pair of
brackets (two independent subscripts) as
Example: int x [] [];
 A three dimensional array will require three pairs of square
brackets and so on 13
Computer programming
Cont,d
♣The general format of multidimensional
array with n dimension is:
Data _ type array _name
[expression_1] [expression_2] ---
[expression_n];

Computer programming 14
Cont,d
♣2D array or multi-dimensional
array stores data in a format consisting
of rows and columns.

Computer programming 14
Cont,d

Example
Float x[10][10]; // total of 100 elements
with two dimensions
int age[3[[6]; // age has 18 elements

int x [2][2] ={1,2,3,4};// two column and


two rows
int x [2][3]={1,2,3,4,5,6};
Computer programming 15
Cont,d

Computer programming 15
Cont,d
// a program to accept input cout<<"enter the "<<i<<" row and
and display its//content enter "<<j<<" column ";
cin>>nums[i][j];
#include<iostream.h>
}
}
int main() cout<<"the output is \n";
{ for (i=0;i<2;i++)
int i,j; {
cout<<" row "<<i<<"\t";
for (j=0;j<2;j++)
int nums[2][2]; {
cout<<nums[i][j]<<"\t";
for (i=0;i<2;i++) }
cout<<endl;
{
cout<<"the "<<i<<" row \n"; }
for (j=0;j<2;j++) return 0;
{ }

Computer programming 16
Strings of Characters:
What are Strings?
♣strings of characters that allow us to
represent successive characters, like
words, sentences, names, texts, etc

Computer programming 17
Declaring and Initialization of
Strings
◊ char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\
0' };
◊ char mystring [] = "Hello";
♣In both cases the Array or string of
characters mystring is declared with a
size of 6 characters (elements of type char
): the 5 characters that compose Hello
plus a final null character ( '\0' ) which
specifies the end of the string and that, in
the second case, when using double
quotes ( " ) it is automatically appended.
Computer programming 18
Cont,d
♣ The following expressions within a code are not valid for
arrays
mystring="Hello";
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
Example:
#include <iostream.h>
#include <string.h>
int main ()
{
char name [20]="Abebe";
cout << name[4];
return 0;
} 19
Computer programming
Assignment one
♣Define strlen function (i.e write the function body of
strlen)
♣Define the strcmp function and the strncmp function
♣Define strcpy function and the strncpy function
♣Define strcat function and the strncat function
♣Write a program to store the ages of six of your friends
in a single array. Store each of the six ages using the
assignment operator. print the ages on the screen
♣Write a C++ program that accepts 10 integers from
the user and finally displays the smallest value and the
largest value.
♣Write a program that accepts ten different integers
from the user and display these numbers after sorting
them in increasing order.
Computer programming 19
Cont,d
♣ Write a C++ program that has two functions to Binary and to
Decimal. The program should display a menu prompting the
user to enter his choice. If the user selects to Binary, then the
function should accept a number in base ten and displays the
equivalent binary representation. The reverse should be done
if the user selects to Decimal.
♣ Develop a C++ program that accepts a word from the user
and then checks whether the word is palindrome or not. (NB
a word is palindrome if it is readable from left to right as well
as right to left).
♣ Write a C++ program that accepts a word from the user and
then displays the word after reversing it.
♣ Develop a C++ program that accepts the name of a person
and then counts how many vowels the person’s name have.
♣ Modify the question in Q14 in such a way that it should
replace vowel characters with * in the person name.
Computer programming 19
Cont,d
♣ Write a program in C++ which read a three digit number and generate all the
possible permutation of numbers using the above digits. For example n = 123 then
the permutations are – 123, 213, 312, 132, 231, 321
♣ Write a program which read a set of lines until you enter #.
♣ Write a program which read two matrixes and then print a matrix which is addition
of these two matrixes.
♣ Write a program which reads two matrix and multiply them if possible
♣ Write a program which reads a 3 x 2 matrix and then calculates the sum of each row
and store that in a one dimension array.
♣ Write a program to store six of your friend’s ages in a single array. Assign the ages
in a random order. print the ages, from low to high, on-screen
♣ Modify the program on Q8 to print the ages in descending order.
♣ Write a C++ program that calculates the letter grades of 20 students. The program
should accept the mid result and the final result from the students. Use the
appropriate validity control mechanism to prevent wrong inputs.
♣ Write a program to store six of your friend’s ages in a single array. Assign the ages
in a random order. print the ages, from low to high, on-screen
♣ Modify the program on Q8 to print the ages in descending order.
♣ Write a C++ program that calculates the letter grades of 20 students. The program
should accept the mid result and the final result from the students. Use the
appropriate validity control mechanism to prevent wrong inputs.
Computer programming 19

You might also like