Algo Assignment
Algo Assignment
Algo Assignment
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.
ii. Implement your algorithm in the PalindromeChecker class or function. Write down
the full program palindrome
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
the entry M[i][j] for i ≤ j contains the average of the array entries A[i] through A[j].
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;
}
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
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)