Posts

Showing posts from January, 2020

Write a function for each of the following problems and count the number of steps

Image
Write a function for each of the following problems and count the number of steps for execution and write the time complexity of each function, 1. To calculate the sum of 1 to N numbers using loop. 2. To calculate the sum of 1 to N numbers using equation. 3. To calculate the sum of 1 to N numbers using recursion. #include <stdio.h> #include <conio.h> int rcount=0; int uloop(int n) {   int sum=0,count=0 , i;   count++; count++;   for(i=1;i<=n;i++)   {     sum=sum+i;     count++;   }   count++;   printf(" \n Count is:% d ",count);   return sum; } int uequation( int n) {    int sum,count=0;    count++;    sum=(n*(n+1))/2;    count ++;    count ++; printf(" \n count is:% d ",count);    return sum; } int urecursion(int n) {  rcount++;   if(n<=0)    { rcount++;      return n;    }    else    { rcount++;      return urecursion(n-1)+n;    } }   void main() { i nt n,choice , sum; clrscr();  

Implement functions to print nth Fibonacci number using iteration and recursive method.

Image
Implement functions to print nth Fibonacci number using iteration and recursive method. Compare the performance of two methods by counting number of steps executed on various inputs. Also draw a comparative chart. (Fibonacci series 1, 1, 2, 3, 5, 8…..  Here 8 is the 6th Fibonacci number) #include<stdio.h> #include<conio.h> int iteration(int); int recursive(int); int count=0; void main() { int n,choice,ans; clrscr(); printf("\nenter nth number : "); scanf("%d",&n); printf(“\n==iteration==”); ans=iteration(n); printf(“\nans=%d\ncount=%d”,ans,count); printf(“\n\n==recursive==”); ans=recursive(n); printf("\nans=%d\ncount=%d",ans,count); getch(); } int iteration(int n) { int f=1,s=1,t,i; count++;   count++; for(i=3;i<=n;i++) { t=f+s; count++; f=s; count++; s=t; count++; } count++; return t; } int recursive(int n) { count++; if(n==0 || n==1) { cou

Write user defined functions for the following sorting methods and compare their performance by time measurement with random data and Sorted data.

Image
SELECTION SORT: #include<stdio.h> #include<conio.h> #include<math.h> #include<time.h> #define n 10000 void selection_sort(int[]); unsigned long int count=0; void main() {             int a[n];             int choice,i;             clrscr();             printf("1.Sorted Data  2.Random Data \n");             scanf("%d",&choice);             switch(choice)             {                         case 1:                                     for(i=0;i<n;i++)                                     {                                                 a[i]=i+1;                                     }                                     selection_sort(a);                                     break;                         case 2:                                     for(i=0;i<n;i++)                                     {                                                 a[i]=n-i;