Featured

bcsl-45 solved assignment

Course Code                           : BCSL-045
Course Title                             : Introduction to Algorithm Design Lab
Assignment Number              : BCA (IV)/045/Assignment/14-15
Maximum Marks                     : 50
Weightage                               : 25%
Last Dates for Submission    : 15th October, 2014 (For July 2014 Session)
15th April, 2015 (For January 2015 Session)
All questions carry eight marks each. Rest 10 marks are for viva-voce. Answer all The questions. All programmes are required to be run and tested including test Reports.
(i) A Palindrome is a string that reads the same forward and backward. Write an algorithm for determining whether a string of characters is palindrome.
Solution:-
C program to check whether it is a palindrome and also find the length of string
1. #include <stdio.h>
2. #include <string.h>
3.  
4. void main()
5. {
6.     char string[25], reverse_string[25] = {'\0'};
7.     int i, length = 0, flag = 0;
8.  
9.     printf("Enter a string \n");
        gets(string);
    /*  keep going through each character of the string till its end */
   for (i = 0; string[i] != '\0'; i++)
    {
        length++;
    }
    printf("The length of the string '%s' = %d\n", string, length);
    for (i = length - 1; i >= 0 ; i--)
    {
        reverse_string[length - i - 1] = string[i];
    }
   /*  Check if the string is a Palindrome */
    for (flag = 1, i = 0; i < length ; i++)
    {
        if (reverse_string[i] != string[i])
            flag = 0;

    }
    if (flag == 1)
       printf ("%s is a palindrome \n", string);
    else
       printf("%s is not a palindrome \n", string);
}
Output:-
Enter a string
  how  are you
The length of the string 'how  are you' = 12
how  are you is not a palindrome
-------------------------------------------------------------------
 Enter a string
   madam
 The length of the string 'madam' = 5
 madam is a palindrome
Algorithm:-
step 1 : start

step 2 : Declare variables char_string[25], reverse_string[25]
                    int i, length = 0, flag = 0;

step 3 : read value string.

step 4 : for (i = 0; string[i] != '\0'; i++)
         length++
step 5 : display 'the length of string =', string, length

step 6 : for (i = length - 1; i >= 0 ; i--)
        do reverse_string[length - i - 1] = string[i]
          /*  Check if the string is a Palindrome */

step 7 : for (flag = 1, i = 0; i < length ; i++)
                  if revers_string[i] ! = string[i]
                           do flag = 0
                        if flag == 1
                         display string is palindrome.
                         else 
                         display string is not palindrome.

step 8 : stop.
(ii) Find the largest number in an array and count a number of comparison operations as well as running time complexity of each statement and total complexity of the problem.
Solution:-
#include <stdio.h>
int main() {

    int i,n, count = 0;
    float arr[100];
    printf("Enter total number of elements(1 to 100): ");
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;++i)  /* Stores number entered by user. */
    {
       printf("Enter Number %d: ",i+1);
       scanf("%f",&arr[i]);
    }

    for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
    {
       if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
       {       
       count++;
       }
           arr[0]=arr[i];
    }
    printf("Largest element = %.2f \n",arr[0]);
    printf(“the number of comparison %d”, count);    
    return 0;
       getch();
}
Output:-
largestnum

Time Complexity of AlgorithmsTime complexity of an algorithm signifies the total time required by the program to run to completion. The time complexity of algorithms is most commonly expressed using the big O NotationTime Complexity is most commonly estimated by counting the number of elementary functions performed by the algorithm.
Calculating the Complexity
Now lets tap onto the next big topic related to Time complexity, which is How to Calculate Time Complexity. It becomes very confusing some times, but we will try to explain it in the simplest way.
Now the most common metric for calculating time complexity is Big O notation. This removes all constant factors so that the running time can be estimated in relation to N, as N approaches infinity. In general you can think of it like this:
statement;
Above we have a single statement. Its Time Complexity will be Constant. The running time of the statement will not change in relation to N
for(i=0; i &lt; N; i++)
              {
                     statement;
               }
The time complexity for the above algorithm will be Linear. The running time of the loop is directly proportional to N. When N doubles, so does the running time.
for(i=0; i &lt; N; i++)
    {
        for(j=0; j &lt; N;j++)
            {
              statement;
             }
    }
This time, the time complexity for the above code will be Quadratic. The running time of the two loops is proportional to the square of N. When N doubles, the running time increases by N * N.
(iii) Write a programme which interchanges the values the variable using only assignment operations. What is the minimum number of assignments operations required?
Solution:-
#include <stdio.h>
#include <string.h> 
/* Function Prototype */

void swap(int*, int *);
void main()
{
    int num1, num2;
printf("\nEnter two numbers:");
scanf("%d %d", &num1, &num2);
printf("\nThe numbers before swapping are Number1= %d Number2 = %d", num1, num2);
swap(&num1, &num2);   /* Call by Reference to function swap */
printf("\nThe numbers after swapping are Number1= %d Number2 = %d", num1, num2);

}
/* Code to swap two numbers using Assignment operator */
void swap(int *x, int *y)
  {
    *x = *x ^= *y;
    *y = *y ^= *y;
    *x = *x ^= *y;
}
Output:-
numsapThe minimum number of operator assigned is six because as we can see in the void swap function in that we used assignment operator six time to swap the value of num1 and num2.
author

Author Name

Author Description!

Get Free Email Updates to your Inbox!

Entri Populer

www.CodeNirvana.in

infolink adds

Powered by Blogger.

Translate

Total Pageviews

Copyright © ignou solved assignments | Distributed By My Blogger Themes | Designed By Code Nirvana