Você está na página 1de 7

ALGORITHMS FOR RECORD PROGRAMS

ARRAY OF STRUCTURE

AIM: To sort the records of array of structure in ascending order using the technique of
Exchange Selection Sort

Algorithm

Step 1: Start
Step 2: Definition of structure Worker
Step 3: Declartion of array of structure.
Step 4: Definition of function Selsort( )
(a) Declaration of a temporary structure variable
(b) Inner j loop finds the worker with lowest salary
(c) Outer I loop swaps the record
Step 5 : Read the size and the records of the structure.
Step 5: Invocation of function Selsort( )
Step 6: Print the records in ascending order
Step 8: End.

RETURN OF OBJECTS

AIM: To illustrate the concept of function which return objects

ALGORITHM:

Step 1: Start
Step 2: Definition of class student
(a) class name: student
(b) data members in private section
long regno
char name[20],grade
int total
(c) member functions in public section
rettotal( ) – accessor function which returns total
Enter() – reads the data members
(d) print() – display the details of Topper on screen
step 3: Definition of findtopper
(a)array of student and size are taken as argument
(b)assigns max with the total of the first record
(c ) compares the records with the total to find the record with the highest total
(d) returns the record with the highest total
Step 5 : End
CONSTRUCTORS & DESTRUCTORS

AIM: To write a C++ program to illustrate the concept of constructor and destructor using the
class box which contains the data members length,breadth and height and the function volume()
to calculate the volume of the box.
Algorithm for main():
Step 1: Start.
Step 2: Create an object of box.
Step 3: Initialize the character variable rep to “y”.
Step 4: Execute steps 5 to 10 while rep is equal to “y”.
Step 5: Accept the choice of constructor.
Step 6: If choice is equal to 1
i. Create an object ob1 without arguments.
ii. Invoke volume function.
Step 7: If choice is equal to 2
i. Accept the values of length,breadth and height.
ii. Create an object ob2 with arguments.
iii. Invoke volume function.
Step 8: If choice is equal to 3
i. Create an object ob3 and assign object bo to ob3.
ii. Invoke volume function.
Step 9: Ask user whether the user wants to continue or not.
Step 10: Accept the rep.
Step 11: End.
Algorithm for Default Constructor box():
Step 1: Assign some value for length,breadth and height.
Step 2: Return
Algorithm for Parameterised Constructor - box(float,float,float):
Step 1: Assign argument value for length,breadth and height.
Step 2:Return
Algorithm for Copy Constructor - box(box&):
Step 1: Copy the value of length,breadth and height from the object passed as argument to the
local object.
Step 2:Return
Algorithm for Volume():
Step 1: Print the value of length*breadth*height.
Step 2: Return

CHARACTER POINTERS
Aim: Program to illustrarte pointers as function argument and return of pointers
Algorithm :
Step 1: Start
Step 2: Declaration of Character Pointer
Step 3: Initialisation of pointer to NULL
Step 4: Reading the string and the searching character
Step 5: Invocation of function match with the string and character as argument
Step 6:Repeat steps 7 and 8 till the match is found or the string exists
Step 7: Checking whether the character matches
Step 7: Incrementation of pointer
Step 8: Return the String from the point of match

SMALLEST AND LARGEST ELEMENT IN AN ARRAY


AIM: Program to print the smallest and largest numbers among array of integers entered by the user.
Algorithm for main():
Step 1: Start
Step 2: Declare array element and variables n,small,big.
Step 3: Accept the values of n and array elements.
Step 4: Assign small=a[0] and big=a[0].
Step 5: Repeat steps 6 to 8 till i<n, initializing i=1.
Step 6: Check if a[i]<small, if true assign small=a[i].
Step 7: Check if a[i]>big, if true assign big=a[i].
Step 8: Increment i.
Step 9: Print big and small.
Step 10: Stop.
ONE DIMENSIONAL ARRAY MANIPULATION

AIM: To read the users choice and print the array in reverse order or swap the adjacent elements
of an array with the help of a menu driven program

Algorithm
REVERSING ARRAY ELEMENTS
.Step 1: Start
Step 2: Declare array element and variables size,temp.
Step 3: Accept the values of size and array elements.
Step 4: Repeat steps 5 and 6 till i<j,initializing i=0,j=n-1.
Step 5: Assign temp=a[i],a[i]=a[j] and a[j]=temp.(Swapping a[i] and a[j]).
Step 6: Increment i and decrement j.
Step 7: Print array elements.
Step 8: End

SWAPPING ADJACENT ELEMENTS


Step 1: Start
Step 2: Declare array element and variables size,temp.
Step 3: Accept the values of size and array elements.
Step 4: Initialise variable I to 0
step 5:Repeat steps 5 and 6 till i=n-1.
Step 6: Assign temp=a[i],a[i]=a[i+1] and a[i+1]=temp.
Step 6: increment I to i+2.
Step 7: Print array elements.
Step 8:End

MULTIPLE INHERITANCE
AIM: To illustrate the concept of Multiple inheritance
ALGORITHM:
Step 1: Definition of Base class worker with relevant data members and functions
step 2 : Definition of Base class Personal with relevant data members and functions
Step 3: Definition of Derived class Salary that inherits worker publicly and personal privately
(i) Declaration of its own data members and functions
(ii) Invocation of Base class functions inside derived class to read the details and print
them
step 4: Definition of function Main
(i) Declaration of Object of Derived class
(ii) invocation of derived class member functions to perform the required task
step 5: End
SUM OF DIAGONALS
AIM: Program to print sum of diagonals depending upon user’s choice.

Algorithm for main():

Step 1: Start
Step 2: Declare the variables m,n,ch and arrays a of size 10*10 respectively.
Step 3: Accept the number of rows and columns for array ‘a’ amd store it in the variables m and n
respectively.
Step 4: Initialize i to 0 and repeat the steps 5 to 8 till i<m.
Step 5: Initialize j to 0 and repeat the steps 6 to 7 till j<m.
Step 6: Accept the values of elements from the user and store it in a[i][j].
Step 7: Increment j by 1.
Step 8: Increment i by 1.
Step 9: Accept the choice(ch). (1 for main diagonal,2 for other diagonal,3 for both diagonals).
Step 10: If ch’s value is 1 then call mdiagonal(a,m).
Step 11: If ch’s value is 2 then call odiagonal(a,m).
Step 12: If ch’s value is 3 then call bdiagonal(a,m).
Step 13: If ch’s value is 4 then call exit(0).
Step 14: End.
Algorithm for mdiagonal(a,m):
Step 1: Declare variables i,j,sum.
Step 2: Initialize sum to 0.
Step 3: Initialize i to 0 and repeat the steps 4 to 7 till i<m.
Step 4: Initialize j to 0 and repeat the steps 5 to 6 till j<m.
Step 5: Check if(i==j) then add to sum
Step 6: Increment j by 1.
Step 7: Increment i by 1.
Step 8: Print sum.
Algorithm for odiagonal(a,m,n):
Step 1: Declare variables i,j,sum.
Step 2: Initialize sum to 0.
Step 3: Initialize i to 0 and repeat the steps 4 to 7 till i<m.
Step 4: Initialize j to m-1 and repeat the steps 5 to 6 till j>=0.
Step 5: Check if(i+j==m-1) and the element with sum.
Step 6: Decrement j by 1.
Step 7: Increment i by 1.
Step 8: Print sum.
Algorithm for bdiagonal(a,m,n):
Step 1: Declare variables i,j,sum.
Step 2: Initialize sum to 0.
Step 3: Initialize i to 0 and repeat the steps 4 to 7 till i<m.
Step 4: Initialize j to 0 and repeat the steps 5 to 6 till j<m.
Step 5: Check if(i==j) and addthe element to sum
Step 6: Increment j by 1.
Step 7: Increment i by 1.
Step 8: Initialize i to 0 and repeat the steps 4 to 7 till i<m.
Step 4: Initialize j to m-1 and repeat the steps 5 to 6 till j>=0.
Step 5: Check if(i+j==m-1) and (i!=j)and add the element with sum.
Step 6: Decrement j by 1.
Step 7: Increment i by 1.
Step 8: Print sum.
SWAP FIRST ROW AND LAST ROW

AIM: Program to swap first row and last row of a 2Dimensional integer array

Algorithm for main():

Step 1: Start
Step 2: Declare the variables m,n and arrays a of size 10*10 respectively.
Step 3: Accept the number of rows and columns for array ‘a’ amd store it in the variables m and n
respectively.
Step 4: Initialize i to 0 and repeat the steps 5 to 8 till i<m.
Step 5: Initialize j to 0 and repeat the steps 6 to 7 till j<n.
Step 6: Accept the values of elements from the user and store it in a[i][j].
Step 7: Increment j by 1.
Step 8: Increment i by 1.
Step 9: Call the function swap(a,m,n).
Step 10: Initialize i to 0 and repeat the steps 11 to 14 till i<m.
Step 11: Initialize j to 0 and repeat the steps 12 to 13 till j<n.
Step 12: Display the values stored in a[i][j].
Step 13: Increment j by 1.
Step 14: Increment i by 1.
Step 15: End
Algorithm for swap(a,m,n):

Step 1: Declare a variable temp.


Step 2: Initialize j to 0 and repeat the step 3 till j<n.
Step 3: Assign temp=a[0][j],a[0][j]=a[m-1][j] and a[m-1][j]=temp.
Step 4: Return.

MERGE SORT
AIM: Program to sort two arrays A, B where A is arranged in ascending order and B is arranged in
descending order using merge sort into an array C in ascending order.
Algorithm for main():
Step 1: Start
Step 2: Declare array elements A,B,C and variables m,n.
Step 3: Accept the values of sizes (m and n) and array elements for A array and B array.
Step 4: Call the function mergesort(A,m,B,n,C) to sort the elements in ascending order.
Step 5: Print the merged array.
Step 6: End
Algorithm for mergesort():
Step 1: Initialize j=n-1, k=0 and i=0.
Step 2: Repeat the steps 3 and 4 till j>=0 and i<m.
Step 3: Compare the elements in array A and B. If array element in ith position of array A is lesser
than or equal to array element in jth position in array B then:
i. Initialize array element in ith position of array A in the kth position of array C. Increment k
and i by 1.
Step 4: Else Initialize array element in jth position of array B in the kth position of array C.
Increment k and decrement j by 1.
Step 5: Repeat the step 6 till i<m.
Step 6: Initialize the array element in ith position of array A in the kth position of array C.
Increment k and i by 1.
Step 7: Repeat the step 8 till j>=0.
Step 8: Initialize array element in jth position of array B in the kth position of array C. Increment k
and decrement j by 1.
Step 9: Return
INSERTION IN ONE DIMENSIONAL ARRAY
AIM: To insert an element into a sorted one dimensional integer array
Algorithm for main():
Step 1: Start.
Step 2: Accept the number of elements of the array in the variable n.
Step 3: Accept the x number of elements in to the array a[]. Enter no. of elements you want to
enter as x. Assign u=x-1.
Step 4: Repeat the steps 5 and 6 till rep=’y’.
Step 5: i Accept the elements to be inserted in the variable item.
i. Call the function insert() with the array ‘a’,item and n as argument.
Step 6: Accept the value in rep to continue or to end.
Step 7 : Stop.
Algorithm for insert():
Step 1: If u=n-1 then display “Overflow”.
Step 2: If the item is lesser than the first element of the array, initialize pos=0.
i. Initialize i=0.
ii. Repeat (iii) and (iv) till i<=u.
iii. If the array element in the ith position is lesser than or equal to the item and if the item is
lesser than or equal to the array element in the (i+1)th position then initialize pos=i+1.
iv. Increment i by 1.
Step 3: If i=u then assign pos=u+1.
Step 4: Initialize j=u+1.
Step 5: Repeat the steps 6 and 7 till j>pos.
Step 6: Initialize the array element in the (j-1)th position to the element in jth position.
Step 7: Decrement j by 1.
Step 8: Initialize the array element in the pos position with the item.
Step 9: Increment u by 1.
Step 10: Display the resultant array.
Step 11: Return.

DELETION IN ONE DIMENSIONAL ARRAY

Algorithm for main():


Step 1: Start.
Step 2: Accept the number of elements of the array in the variable n.
Step 3: Accept the x number of elements in to the array a[]. Enter no. of elements you want to
enter as x. Assign u=x-1.
Step 4: Repeat the steps 5 AND 6 till rep=’y’.
Step 5: i Accept the elements to be deleted in the variable item.
ii Call the function delete() with the array ‘a’, item and n as argument.
Step 6: Accept the value in rep to continue or to end.
Step 8: End.

Algorithm for delete():


Step 1: Initialize flag=0, i=0.
Step 2: If u=0 then display “Underflow Condition”.
Step 3: Repeat the steps 4 and 5 till i<n.
Step 4: If the array element in the ith position is equal to the item, then initialize pos=i,flag=1,break
out of the loop.
Step 5: Increment i by 1.
Step 6: If flag=0, display “item not found”.
Step 7: Initialize i=pos.
Step 8: Repeat the steps 9 and 10 till i<u.
Step 9: Initialize the array element in the (i+1)th position to the array element in the ith position.
Step 10: Increment i by 1.
Step 11: Decrement u by 1.
Step 12: Display the resultant array.
Step 13: Return.

TRANSPOSE OF A MATRIX
AIM: Program to print the transpose of a matrix.

Algorithm for main():


Step 1: Start
Step 2: Declare the variables m,n,ch,i,j and arrays a,b of size 10*10 respectively.
Step 3: Accept the number of rows and columns for array ‘a’ amd store it in the variables m and n
respectively.
Step 4: Initialize i to 0 and repeat the steps 5 to 8 till i<m.
Step 5: Initialize j to 0 and repeat the steps 6 to 7 till j<n.
Step 6: Accept the values of elements from the user and store it in a[i][j].
Step 7: Increment j by 1.
Step 8: Increment i by 1.
Step 9: Call the function transpose(a,b,m,n).
Step 10: Stop.
Algorithm for transpose(a,b,m,n):
Step 1: Declare variables i and j.
Step 2: Initialize i to 0 and repeat the steps 3 to 6 till i<m.
Step 3: Initialize j to 0 and repeat the steps 4 to 5 till j<n.
Step 4: Assign b[i][j]=a[j][i].
Step 5: Increment j by 1.
Step 6: Increment i by 1.
Step 7: Initialize i to 0 and repeat the steps 8 to 11 till i<m.
Step 8: Initialize j to 0 and repeat the steps 9 to 10 till j<n.
Step 9: Display b[i][j].
Step 10: Increment j by 1.
Step 11: Increment i by 1.
Step 12: Return.

Você também pode gostar