OBJECTIVE- Write a C program to implement Quick Sort and also plot time complexity. ALGORITHM- -select a pivot either from 1-first element 2-middle element 3-last element and with ech iteration arrange the element such that towards left of it lies all element smaller than pivot element and in it's right all element grater than the pivot then repeat the process quicksort(arr,low,p-1) quicksort(arr,p+1,high) #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> void quicksort(int *arr,int low,int high) { int n,pivot,i=low,j=high; if(low<high) { pivot=low; while(i<j) { while(arr[i...
Posts
OBJECTIVE- Write a program to implement Merge sort and also draw it's plot. ALGORITHM- MergeSort(arr[], l, r) If r > l 1. Find the middle point to divide the array into two halves: middle m = (l+r)/2 2. Call mergeSort for first half: mergeSort(arr, l, m) 3. Call mergeSort for second half: mergeSort(arr, m+1, r) 4. Merge the two halves sorted in step 2 and 3: merge(arr, l, m, r) To draw the plot we need to know the execution time for every single input size so for that purpose we are using clock() function here which will give us time after each exe...
OBJECTIVE - Write a C program to Sort the N Names in an Alphabetical Order. ALGORITHM - -Start from the first name and compare it with all other names -if it is lexicographically large from others then swap both of them -Repeat this process for n-1 names CODE- #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> void merge(char a[][30],long int l,long int m,long int r) { long int i,j,k; long int n1=m-l+1; long int n2=r-m; char la[n1][30],ra[n2][30]; for(i=0;i<n1;i++) strcpy(la[i],a[l+i]); for(j=0;j<n2;j++) strcpy(ra[j],a[m+1+j]); i=0;j=0;k=l; while(i<n1 && j<n2) { if(strcmp(la[i],ra[j])<0) { ...
%s is a format specifier for accepting the string in c language by scanf. But the problem of using this is that it will terminate when we enter a space character as input.So whenever we need to take input string with space character we can not just use %s.To resolve this problem we can use gets() and we can also use %[^\n]s. The expression scanf("%[^\n]s", string) will take input from the keyboard until and unless we will not press Enter button because as we can see that we are using \n in the above scanf() which is used for new line. if you want that your scanf terminates on entering any character or integer then use that at the place of \n. Example- scanf("%[^&]s", string) -using this will terminates the scanf on entering & character. scanf("%[^#]s", string)-this will stop taking input when we press # character. and so on...... In this way you can use this in your own way.
This is a program to count the number of words in a string entered by the user.I am performing this task by using ASCII value of lower case letters (as the string , user will enter must be in lower case only) . T o count the number of words we are not considering any other character oth er then lowercase letters. To enter the text we are using %[^\n]s as %s terminates on entering space (' ') to resolve this problem we can also use gets but i am using %[^\n]s her e. #include<stdio.h> #include<string.h> int main() { char str[100],c=0,i=0; scanf("%[^\n]s",str); while(str[i++]!='\0') { if(str[i]<97 || str[i]>123) { if(str[i+1]>=97 && str[i+1]<=123) c++; } } printf("no of worlds=%d",c); return 0; }
We know that factorial of number greater than 50 is too large that we can not store it's value in any standard data type available.So, if we need to find the factorial of large numbers we will use an array to store the factorial value. The concept is based on that we store single digit of factorial in reverse order to the array and multiply these numbers with the next number of the factorial sequence and each time when we have a carry on multiplying the last digit of array we will increase the size of array and store that carry value there.At last we will print the array in reverse order. Example- We are storing 1 at a[0] and then multiply it with 2 and then 3 and so on..... till that number whose factorial we need.if in any case there is a carry the increase the size of array by 1. Note that we are storing the result in reverse order like for the factorial of 5 and 6 result will be stored like 5!= 0 2 1 6!= 0 ...
P rinting p atterns in C prog ramming is very easy. H ere we are printing the equilateral triangle of numbers . #include<stdio.h> int main() { int i,j,k,l,n,val; printf("no of lines="); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=n-i;j++) { printf(" "); } for(k=1;k<=i;k++) { printf("%d",k);} for(l=i-1;l>=1;l--) printf("%d",l); } return 0; } OUTPUT- no of lines=6 1 121 12321 1234321 123454321 12345654321
#include<stdio.h> int main() { int i,j,k,l,n,val; printf("no of lines="); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=n-i;j++) { printf(" "); } for(k=1;k<=i;k++) { printf("%d",k);} for(l=i-1;l>=1;l--) printf("%d",l); } return 0; } OUTPUT- no of lines=6 1 121 12321 1234321 123454321 12345654321
Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal . #include<stdio.h> int main() { int i,j,l,binom; printf("enter the no of lines="); scanf("%d",&l); for(i=1;i<=l;i++) { for(j=1;j<=l-i;j++) printf(" "); binom=1; for(j=1;j<=i;j++) { printf("%d ",binom); if(i==j+1) binom=1; else binom=(binom*(i-j))/j; } printf("\n"); } return 0; } OUTPUT- enter the no of lines=6 1 ...
#include<iostream.h> #include<conio.h> int operate(int a,int b) { return(a*b); } float operate(float c,float d) { return(c/d); } void main() {clrscr(); int a,b; float m,n; cout<<"enter a and b "; cin>>a>>b; cout<<operate(a,b); cout<<"\n"; cout<<"enter m and n "; cin>>m>>n; cout<<operate(m,n); getch(); } OUT PUT enter a and b 45 47 2115 enter m and n 45 47 0.957447
you can also divide numbers having decimal point using this program. c++ code - #include<iostream.h> #include<conio.h> void main() { clrscr(); float a,b,c; cout<<"\n enter a="; cin>>a; cout<<"\n enter b="; cin>>b; c=a/b; cout<<"\n division="<<c; getch(); } OUTPUT enter a=56 enter b=7 division=8 Similar posts - 1- C++ program to find the absolute value of a number. 2- C++ program to find the reverse of a number. 3- c++ program to swap two numbers using pointers 4- C++ program to print a pyramid of stars