Skip to main content

Posts

INSERTION SORT, BUBBLE SORT, SELECTION SORT

// C program for insertion sort #include <math.h> #include <stdio.h> void insertionSort(int arr[], int n);     // A utility function to print a sorted array of size n void printArray(int arr[], int n) {     int i;     for (i = 0; i < n; i++)         printf("%d ", arr[i]);     printf("\n"); }    // Driver code int main() {     int arr[15],n,i;         printf("enter number of elements in array \n");     scanf("%d", &n);         printf("enter array values to sort\n");     for(i=0; i<n; i++)     scanf("%d", &arr[i]);         insertionSort(arr, n);     printArray(arr, n);        return 0; } /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) {     int i, key, j;     for (i = 1; i < n; i++) ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 40

Write a C program to count the lines , words and characters in a given   text. Algorithm:   step 1 : Start Step 2: Read the text until an empty line Step 3: Compare each character with newline char „\n‟ to count no of lines Step 4 : Compare each character with tab char „\t\‟ or space char „ „ to count no of words Step 5: Compare first character with NULL char „\0‟ to find the end of text  Step 6 : No of characters = length of each line of text Step 7 : Print no of lines, no of words, no of chars  Step 8: Stop   PROGRAM #include <stdio.h> int main() {     char str[100];//input string with size 100     int words=0,newline=0,characters=0; // counter variables     printf("ENTER STRING   AND PRESS ~ \n");     scanf("%[^~]",&str);//scanf formatting         for(int i=0;str[i]!='\0';i++)      {           if(str[i] == ' ')   ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 39

Write a C program that displays the position of a character ch in  the string S or – 1 if S doesn„t contain   ch.   Algorithm: Step 1: Start Step 2: read the string and then displayed Step 3: read the string to be searched and then displayed Step 4: searching the string T in string S and then perform the following steps i.     found = strstr(S, T) ii.     if found print the second string is found in the first string at the position. If not goto step5 Step 5: print the -1 Step 6: Stop  Program:  #include<stdio.h>  #include<string.h>  #include<conio.h>  void main() { char s[30], t[20];  char *found;  clrscr(); puts("Enter the first string: ");  gets(s); puts("Enter the string to be searched: ");  gets(t); found = strstr(s, t);  if(found) { printf("Second String is found in the First String at %d position.\n", found - s); } else { printf("-1"); } getch(); } ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 24

A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding   two terms in the sequence. Write a C program to generate the first n terms of the sequence. Formula:  let t1, t2,............... tn be terms in fibinacci sequence t1 = 0, t2 = 1 tn = tn - 2 + tn - 1......... where n > 2   Algorithm: Step 1: Start Step 2: Read a, b, sum,n values as integers  Step 3: Set a as 0 and b as 1 Step 4: for counter: 2 to no increment counter by 1 begin                        sum ← a + b;                        Print sum a ← b;                         b ← sum;                         end            Step 5: Stop. ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 21

  Write a C program that sorts a given array of   names #include<stdio.h>  #include<string.h>  void main() { int i,j,count; char str[25][25],temp[25];  //clrscr(); printf("\n How many strings u are going to enter?: \n "); scanf("%d",&count); printf("\n Enter Strings one by one:\n ");  for(i=0;i<=count;i++) gets(str[i]);  for(i=0;i<=count;i++) for(j=i+1;j<=count;j++) { if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]);  strcpy(str[i],str[j]);  strcpy(str[j],temp); } } printf("\n Order of Sorted Strings:\n ");  for(i=0;i<=count;i++) puts(str[i]); getch(); } OUTPUT: How many strings u are going to enter?:   6  Enter Strings one by one:  YAK  BROWNEE  ZEEBRA  APPLE  DUCKLING  PATRIC  Order of Sorted Strings:   APPLE BROWNEE DUCKLING PATRIC YAK ZEEBRA

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 20

Write a C program that uses non recursive function to search for a Key value in a given Sorted list of integers using binary search   method ALGORITHM: BINARY SEARCH 1. 1. Start 2. 2. Read the value of n 3. 3. for i=1 to n increment in steps of 1 Read the value of ith element into array 4. 4. Read the element(x) to be searched 5. 5. search<--binary(a,n,x) 6. 6. if search equal to 0 goto step 7 otherwise goto step 8 7. 7. print unsuccessful search 8. 8. print successful search 9. 9. stop PROGRAM: #include<stdio.h>  #include<conio.h> int BinarySearching(int arr[20], int max, int element) { int low = 0, high = max - 1, middle;  while(low <= high) { middle = (low + high) / 2;  if(element > arr[middle])  low = middle + 1; else if(element < arr[middle])  high = middle - 1;  else return middle; } return -1; } void main() { int i,j,element,temp,n, arr[50], position;  //clrscr(); printf("\n Enter the N vaule of an ...