Skip to main content

Posts

Showing posts from October, 2022

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 ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 23

Write a C program to find the sum of individual digits of a positive integer and   test given number is palindrome. ( Palindrome number is such number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.) #include <stdio.h> void main() { int n, temp, r=0,sum=0, rev = 0;  clrscr(); printf("\n Enter an integer: \n");  scanf("%d", &n); /* original number is stored at temp */ temp = n;  while (n > 0) { r = n % 10; sum=sum+r; // sum of Individual Numbers  rev = rev * 10 + r; n = n/10; } printf("\n Given number is = %d\n", temp); printf(“\n Sum of individual digits of a given number is:%d”, sum);  printf("\n Its reverse is = %d\n", rev); if (temp == rev) printf("\n %d Number is a palindrome \n", temp);  else printf("\n %d is Number is not a palindrome \n", temp);  getch(); } OUTPUT: Enter an integer:  545 Given number is = 545  Sum of individual digits of a given number is:14  Its reve...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 18

  Write a C program that sorts the given array of integers using insertion sort in ascending   order. // C program for insertion sort #include <math.h> #include <stdio.h>    /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) {     int i, key, j;     for (i = 1; i < n; i++)      {         key = arr[i];         j = i - 1;         while (j >= 0 && arr[j] > key)          {             arr[j + 1] = arr[j];             j = j - 1;         }         arr[j + 1] = key;     }     printf("\n sorted array using insertion sort \n"); }    // A utility function to print a sorted array of size n void printArray(int arr[], int n) {     int i;   ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 19

Write a C program that uses non recursive function to search for a Key value in a given List of integers using linear search method. Algorithm: 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<--linear(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> long linear_search(long a[100], long, long);  void main() { long array[100], search, i, n, position;  //clrscr(); printf("\n Entered N values:\n");  scanf("%ld", &n); printf("\n Entered %d numbers:\n", n);  for (i= 0; i < n; i++) scanf("%ld", &array[i]); printf("\n Enter number to search: \n");  scanf("%ld", &search); position = linear_search(array, n, search);  if (position == -1) printf("%l...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 17

Write a C program that sorts the given array of integers using selection sort in descending   order. #include <stdio.h>  void main() { int data[100],i,n,j,temp;  clrscr(); printf("\n Enter the number of elements to be sorted:\n ");  scanf("%d",&n); for(i=0;i<n;++i) {  printf("\n %d. Enter element: ",i+1);   scanf("%d",&data[i]); } printf("\n The Entered Elements are:\n");  for(i=0;i<n;i++) printf("%d\t",data[i]); for(j=0;j<n;++j)  for(i=j+1;i<n;++i) { if(data[j]<data[i]) //To sort in ascending order, change < to > { temp=data[j];  data[j]=data[i];  data[i]=temp; } } printf("\n The Elements in descending order:\n");  for(i=0;i<n;++i) printf("%d\t",data[i]);  getch(); } OUTPUT: Enter the number of elements to be sorted:  7  1. Enter element: 43  2. Enter element: 45  13. Enter element: 6  4. Enter element: 87  5. Enter element: 49  6. Enter element: 52 ...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 16

Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order. Algorithm: Bubble Sort: 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. call function to sort (bubble_sort(a,n)) 5.5. for i= 1 to n increment in steps of 1 print the value of ith element in the array 6.6. stop BUBBLE SORT FUNCTION 1. start 2. initialise last to n 3. for i= 1 to n increment in steps of 1 begin 4. initialise ex to 0 5. for i= 1 to last-1 increment in steps of 1 begin 6. if k[i]>k[i+1] goto step 7 otherwise goto step 5 begin 7. assign k[i] to temp assign k[i+1] to k[i] assign temp to k[i+1] increment ex by 1 end-if 8. end inner for loop 9. if ex!=0 assign last-1 to last end for loop 10. Stop. PROGRAM: #include <stdio.h>  void main() { int data[100],i,n,j,temp;  //clrscr(); printf("\n Enter the number of elements to be sort...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 14

Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent.  #include <stdio.h>  #include <conio.h>  #include <string.h>  #include <stdlib.h>  void main() { char rom[30]; int a[30], l, i, k, dec;  clrscr(); printf("Enter the roman number\n");  scanf("%s", &rom); l =strlen(rom);  for(i = 0; i < l; i++) { switch (rom[i]) { case 'I': a[i] = 1;  break;  case 'V': a[i] = 5;   break; case 'X': a[i] = 10;  break; case 'L': a[i] = 50;  break; case 'C': a[i] = 100;  break; case 'D': dec = dec + 500;  break; case 'M': a[i] = 1000;  break; default : printf("Invalid choice");  break; } } k = a[l - 1]; for(i = l - 1; i > 0; i--) { if(a[i] > a[i - 1]) { k = k - a[i - 1]; } if(a[i] <= a[i - 1]) { k = k + a[i - 1]; } } printf("decimal equivalent is %d", k);  getch(); } OUTPUT: Enter the roman number XV decimal equivalent is 15

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 13

Write a program through pointer variable to sum of n elements from   array.  #include<stdio.h>  void main() { int array[5]; int i,n, sum=0;  int *ptr; clrscr(); printf(" \n Enter the size of an array:\n ");  scanf("%d",&n); printf("\nEnter %d array elements:\n",n); for(i=0;i<n;i++) scanf("%d",&array[i]); /* array is equal to base address * array = &array[0] */ ptr = array; for(i=0;i<n;i++) { //*ptr refers to the value at address sum = sum + *ptr; ptr++; } printf("\nThe sum of array elements using pointers: %d",sum);  getch(); } OUTPUT: Enter the size of an array:  5  Enter 5 array elements: 56 76 43 12 34 The sum of array elements using pointers: 221

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 12

Write a program for display values reverse order from array using   pointer. #include <stdio.h>  void main() { int n, i, arr1[15]; int *pt; clrscr(); printf("\n\n Pointer : Print the elements of an array in reverse order :\n");  printf(" \n"); printf(" \n Enter the size of an array:\n ");  scanf("%d",&n); pt = &arr1[0]; // pt stores the address of base array arr1  printf("\n Enter %d integers into array: \n",n); for(i=0;i<n;i++) { printf("\n Element : %d is:",i+1);  scanf("%d",pt);//accept the address of the value  pt++; } pt = &arr1[n - 1]; printf("\n The elements of array in reverse order are :");  for (i = n; i > 0; i--) { printf("\n Element : %d is:%d", i, *pt);  pt--; } printf("\n\n");  getch(); } OUTPUT: Pointer : Print the elements of an array in reverse order :    Enter the size of an array:  5  Enter 5 integers into array:   Element : 1 is:45  Element...

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 11

Write a program for reading elements using pointer into array and display   the values using   array. #include<stdio.h>  #include<conio.h> void main() { int a[50],*p,i,n;  clrscr(); p=a; printf("\n Enter size of array:\n ");  scanf("%d",&n); printf("\n Enter elements of array:\n"); for(i=0;i<n;++i) scanf("%d",p+i); printf("\n The array Elements are:\n");  for(i=0;i<n;++i) printf("\t%d ",*(p+i)); getch(); } OUTPUT: Enter size of array:  5  Enter elements of array: 45 67 87 23 31 The array Elements are: 45 67 87 23 31 

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 10

Write a C program  to perform the   following: i.    Addition of Two Matrices  ii.    Multiplication of Two   Matrices iii.   Write a C program that uses functions to perform the   Transpose of a matrix with memory dynamically allocated for the   new matrix as row and column counts may not be   same. # include<stdio.h>  #include<conio.h> void read_arr(int a[10][10],int row,int col) { int i,j; for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { printf("Enter Element %d %d : ",i,j);  scanf("%d",&a[i][j]); } } } void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col) { int i,j;  for(i=1;i<=row;i++)  { for(j=1;j<=col;j++) { m3[i][j] = (m1[i][j] + m2[i][j]); } } } void mul_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col) { int i,j,k; for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { m3[i][j]=0; for (k=1;k<=row;k++) { m3[i][j] = m3[i][j] + (m1[i][k] * m2[k...