Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views9 pages

Algo Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Algorithm And Data Structure Assignment 1 NMJ20003

Name: Angel Ling Ho Ee


Matric No: 211021080
Programme: Computer Engineering (UR6523002)

Q1. Recursion

A palindrome is a phrase that reads the same forward and backward (examples: ‘race-

car’, ‘radar’, ‘noon’, or ‘rats live on no evil star’). By extension we call every string a

palindrome that reads the same from left to right and from right to left.

Develop a recursive algorithm that takes as input a string and decides whether the string

is a palindrome.

i. Write down your algorithm in pseudocode.


1. Start
2. Create the function prototypes with the return type of function and
function name and declare the parameter of the function.
3. Declare the size of array for the character which is string.
4. Initialize the parameter n, word index as 0.
5. Ask user for input the string.
6. Call the function to proceed the function definition.
7. Declare parameter x= string length- (word index +1)
8. Compare the word[x] and word[n].
9. If same, increment word index, the function will keep calling itself
recursively until there is no mismatch, then print “The word is a
palindrome”
10. If difference, return print “The word is not a palindrome”.
11. End

ii. Implement your algorithm in the PalindromeChecker class or function. Write down
the full program palindrome

#include <stdio.h> //library


#include <string.h> //library for string
void PalindromeChecker (char word[], int wl); //function prototype name
PalindromeChecker with two parameter

int main()
{
char word[1000]; //char array word with size of 1000
int n=0; //initialize the integer n with 0
char Ans; //declare Ans as char

1
Algorithm And Data Structure Assignment 1 NMJ20003

do{
fflush(stdin); //get the buffer to read the input
printf("Enter the word: "); //Ask user to input
gets(word); //read the strings from the input
PalindromeChecker(word,n); //call function palindrome
printf("\n\nDo you want to continue? "); //return from function call and ask
for user want to continue or not
scanf("%c",&Ans); //read input from user
printf("\n");
}
while(Ans=='Y'||Ans=='y'); //if the user enter other than y , then end the
program
{
return 0;
}
}
void PalindromeChecker(char word[], int wl) //function definition
{
int x=strlen(word)-(wl+1); //declare x as int with the formula
if (word[wl]==word[x]){ // compare the two array with different index if it
is same
if (wl+1==x||wl==x){ //if wl+1 equal to x or wl equal to x
printf("The entered word is a palindrome."); //word is confirmed as a
palindrome
return;
}
PalindromeChecker(word,wl+1); //call again the function as recursion
function
}
else{ // if wl+1 not equal to x or wl not equal to x
printf("The entered word is not palindrome."); //word is confirmed as not
a palindrome

2
Algorithm And Data Structure Assignment 1 NMJ20003

}
}
Output:

(10 Points)

3
Algorithm And Data Structure Assignment 1 NMJ20003

2. Array of Averages

Design an efficient algorithm that achieves the following t ask: Given an array A[1::n] of

floating point numbers, it returns a two-dimensional array, say M, of size n × n in which

the entry M[i][j] for i ≤ j contains the average of the array entries A[i] through A[j].

That is: if i ≤ j, then

whereas for i > j we have that M[i][j] = 0.

i. Describe your idea for an algorithm that creates this matrix.


1. Start
2. Ask user to enter the input for the number of elements.
3. Two-dimensional arrays declare as M has n number of elements.
4. The elements are entered at only one condition if i==j.
5. M[i][j] will equal to 0 when i>j.
6. The average operation will happen when the condition is i<j.
7. Adding the element from i to j and divide it by the total number of elements.
8. The results will show in floating point with 2 decimal places.
9. End

ii. Write down the algorithm in pseudocode.


1. Start
2. Create a ArrayofAvarage function with four int parameter.
3. Ask user to input the number of element.
4. Initialize the int n,x,y,z.
5. Scanf(“%d”,&n);
6. Declare the matrix as float M[n][n];
7. Ask user to input the element in the matrix.
8. Call ArrayofAvarage function
9. Start a loop with condition (i=0;i<n;i++)
10. Inner loop condition (j=0;j<n;j++)
11. if(i==j){ scanf(“%.2f”,&M[i][j]);}
12. else if(i>j){ M[i][j]=0};
13. End loop to proceed next.
14. The start a new loop with same condition (i=0;i<n;i++)
15. Inner loop condition (j=0;j<n;j++)
16. Initialize sum=0.
17. if( i<j){
18. sum=0;
19. Start a loop with condition (a=I;a<=j;a++)
20. Sum+=M[a][a]; end loop
21. M[i][j]=sum/(j-i)+1;end all loop
22. Printf(“M[i][j]=%.2f”.M[i][j]).
4
Algorithm And Data Structure Assignment 1 NMJ20003

iii. How many assignments will your algorithm perform for an input of size n?
 3
iv. Implement the algorithm in the ArrayOfAverages function.

#include <stdio.h>
#include <stdlib.h>
void ArrayofAvarage(int,int,int,int);
int main()
{
printf("Please enter the number of element of the two-dimensional array: ");
int n,x,y,z;
scanf("%d",&n);
float M[n][n];
printf("Please enter the element: \n");
ArrayofAvarage(n,x,y,z);
return 0;
}
void ArrayofAvarage(int n,int i,int j, int a)
{
float M[n][n];
float sum=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j){
scanf("%f",&M[i][j]);
}
else if (i>j){
M[i][j]=0;
}
}}
for(i=0;i<n;i++){
for(j=0;j<n;j++){

5
Algorithm And Data Structure Assignment 1 NMJ20003

if (i<j){
sum=0;
for(a=i;a<=j;a++){
sum+=M[a][a];
}
M[i][j]=sum/((j-i)+1);
}
}
}
printf("The required matrix:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%.2f ",M[i][j]);
}
printf("\n");
}
return;
}
Output:

6
Algorithm And Data Structure Assignment 1 NMJ20003

v. Write code to measure the running time of your Java or C program for random inputs of size
n = 5, 10, 15, etc. Does the growth of the running time correspond to your estimate of the
number of assignments?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void ArrayofAvarage(int,int,int,int);
int main()
{
clock_t start, end;
double execution_time;
start = clock();
printf("Please enter the number of element of the two-dimensional array: ");
int n,x,y,z;
float timetaken;
scanf("%d",&n);
float M[n][n];
printf("Please enter the element: \n");
ArrayofAvarage(n,x,y,z);
end = clock();
execution_time = ((double)(end - start))/CLOCKS_PER_SEC;
printf("Time taken to execute in seconds : %f", execution_time);
return 0;
}

void ArrayofAvarage(int n,int i,int j, int a)


{
float M[n][n];
float sum=0;
for(i=0;i<n;i++){

7
Algorithm And Data Structure Assignment 1 NMJ20003

for(j=0;j<n;j++){
if(i==j){
scanf("%f",&M[i][j]);
}
else if (i>j){
M[i][j]=0;
}
}}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if (i<j){
sum=0;
for(a=i;a<=j;a++){
sum+=M[a][a];
}
M[i][j]=sum/((j-i)+1);
}
}}
printf("The required matrix:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%.2f ",M[i][j]);
}
printf("\n");
}
return;
}

8
Algorithm And Data Structure Assignment 1 NMJ20003

Output :

Input of size ,n =5

Input of size, n=10

Input of size, n=15

 By comparing the input size of 5,10, and 15, the executing time is increasing. Hence, it is
same as my estimation of the number of assignments.
(20 Points)

You might also like