programing chap 3
programing chap 3
Computer programming 2
Cont’d
Computer programming 4
Array declaration
► In the array declaration one must define:
♣The type of the array (i.e. integer, floating point, char
etc.)
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,};
#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
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