
































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Prepara tus exámenes
Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Prepara tus exámenes con los documentos que comparten otros estudiantes como tú en Docsity
Encuentra los documentos específicos para los exámenes de tu universidad
Estudia con lecciones y exámenes resueltos basados en los programas académicos de las mejores universidades
Responde a preguntas de exámenes reales y pon a prueba tu preparación
Consigue puntos base para descargar
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Comunidad
Pide ayuda a la comunidad y resuelve tus dudas de estudio
Ebooks gratuitos
Descarga nuestras guías gratuitas sobre técnicas de estudio, métodos para controlar la ansiedad y consejos para la tesis preparadas por los tutores de Docsity
un buen libro para empezar a programar
Tipo: Ejercicios
1 / 72
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!

































































P.O.Box 342-01000 Thika Email: [email protected] Web: www.ku.ac.ke
Algorithms for manipulating linked list Algorithm for manipulating trees Algorithms for manipulating postfix and infix expressions
4. Recursive functions Factorial functions Power functions Fibonacci functions 5. Postfix and infix expressions 6 .Traversal of the tree Pre-order traversal In-order traversal Post-order traversal 7. Search Linear search Binary search 8. Sorting Bubble sort algorithm Selection sort algorithm Insertion sort algorithm Quick sort algorithm AAsssseessssmmeennttss Continuous Assessment Tests (CATs) (30%) End of semester examination (70%) Total = 100%
RReeqquuiirreedd^ tteexxtt^ bbooookkss
Salamis S, Data structures, Algorithms and application in Java, McGraw Hill Banachowski I et al, Analysis of algorithms and Data structures, Addison wiley
TTeexxtt^ bbooookkss^ ffoorr^ ffuurrtthheerr^ rreeaaddiinngg
MKlaus W., Algorithms, data structures, programmes, New Delhi, MCGraw Hill
What is an array?
Array is a very basic data structure provided by every programming language. Let’s talk about an example scenario where we need to store ten employees’ data in our C/C++ program including name, age and salary. One of the solutions is to declare ten different variables to store employee
By the end of the chapter a student shall be able to: Understand and Manipulate data structures Design and apply algorithms to various data structures Understand the application areas of various data structures
int Age[NUM_EMPLOYEE];
How to initialise an array?
Initialisation of array is very simple in c programming. There are two ways you can initialise arrays.
Declare and initialise array in one statement.
Declare and initialise array separately.
Look at the following C code which demonstrates the declaration and initialisation of an array.
int Age [5] = {30, 22, 33, 44, 25};
int Age [5];
Age [0]=30;
Age [1]=22;
Age [2]=33;
Age [3]=44;
Age [4]=25;
Array can also be initialised in a ways that array size is omitted, in such case compiler automatically allocates memory to array.
int Age [ ] = {30, 22, 33, 44, 25};
Let’s write a simple program that uses arrays to print out number of employees having salary more than 3000.
Array in C Programming
#include
#include
#include
#define NUM_EMPLOYEE 10
int main(int argc, char *argv[]){
int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;
printf("Enter employee salary (Max 10)\n ");
for (i=0; i
1.1.2 How to declare and initialize multi-dimensional arrays?
Often there is need to manipulate tabular data or matrices. For example if employee salary is increased by 20% and you are required to store both the salaries in your program. Then you will need to store this information into a two dimensional arrays. C/C++ gives you the ability to have arrays of any dimension.
Multi dimension arrays
Consider the example above, you have to store, previous salary, present salary and amount of increment. In that case you will need to store this information in three dimensional arrays.
First I will show you how to declare a two dimensional array and initialise it. Then write a complete program to use multidimensional arrays.
int Salary[10][2];
This defines an array containing 10 elements of type int. Each of these elements itself is an array of two integers. So to keep track of each element of this array is we have to use two indices. One is to keep track of row and other is to keep track of column.
Elements of multidimensional arrays
Here is a graphical view of multidimensional array that we use to store salary and increment on salary. First column stores the salary element of the array and second column stores increment on salary. We could add another column to store the new salary which adds the increment to the salary.
Column 0 – Salary
Column 1 – Increment
Row 0
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Initialising multidimensional arrays
Multidimensional arrays can also be initialised in two ways just like one dimensional array. Two braces are used to surround the row element of arrays.
If you are initialising more than one dimension then you will have to use as many braces as the dimensions of the array are.
int Salary [5][2] = {
{2300, 460},
{3400, 680},
{3200, 640},
{1200, 240},
{3450, 690}
};
int Salary [5][2] ={0}; //This will initialise all the array elements to 0
int Salary [5][2];
Salary [0][0]=2300;
Salary [1][0]=3400;
int lCount=0,gCount=0,i=0;
for(i=0; i
#include
#define NUM_EMPLOYEE 10
using namespace std;
int main(int argc, char *argv[]){
//initialise Salary of each employee
int Salary[NUM_EMPLOYEE][2]={
{2300,0},
{3400,0},
{3200,0},
{1200,0},
{3450,0},
Two of the more common data objects found in computer algorithms are stacks and queues. Both of these objects are special cases of the more general data object, an ordered list.
A stack is an ordered list in which all insertions and deletions are made at one end, called the top. A queue is an ordered list in which all insertions take place at one end, the rear , while all deletions take place at the other end, the front. Given a stack S=(a[1],a[2],.......a[n]) then we say that a1 is the bottommost element and element a[i]) is on top of element a[i-1], 1
If the top = = 0
Increment top by 1
Insert the item
Decrease top by 1
1.2.2 The stack class
Class stack
{
Int top;
Int stackarray[50];
Public:
Stack( );
Int emptystack( );
Int fullstack( );
Void push(int item);
Void pop(int &item);
};
Example
Using stack structure write a program for displaying numbers in the reverse order
Solution
#Include < iostream.h>
Const int maxsize = 20;
{
Int top;
Int stackarray[maxsize];
Public:
Int emptystack( );
Int fullstack( );
Void push(int item);
Void pop(int & item);
};
Stack : : stack( )
{
Top = 0;
}
Int stack : : emptystack( )
{
Return top = = 0;
}
Int stack : : fullstack( )
{
Return top = = maxsize;
}
S. pop (x);
cout <
i)Compute remainder ii)Push remainder into the stack iii)Compute next number (the quotient become the next number)
Example 2
Using stack, write a program for converting a number from base 10 to any other base (1-9)
Solution
Const int max size = 50;
{
class stack Int top; Int stackarray(maxsize)
Public:
Stack ( );
Int emptystack( );
Int fullstack( );
Void push(int item);
};
{
Top = 0;
}
int stack : : emptystack ( )
{
Return top = = 0;
}
Int stack: : fullstack ( )
{return top = = max size;
}
Void stock : : push (int item)
{
Top ++;
Stakarray (top) = item;
}
Void stock : : pop (int pitem)
{
Item = stackrray (top);
Top = - ;
}
Void main( )