Posts

string array sorting using bubble sort algorithm

Bubble sort in string array #include <stdio.h> #include <conio.h> #include <string.h> #define MAX 50 #define N 2000 void sort_words(char *x[], int y); void swap(char **, char **); int main(void) { char word[MAX]; char *x[N]; int n = 0; int i = 0; for(i = 0; scanf("%s", word) == 1; ++i) { if(i >= N) printf("Limit reached: %d\n", N), exit(1); x[i] = calloc(strlen(word)+1, sizeof(char)); strcpy(x[i], word); } n = i; sort_words(x, n); for(i = 0; i < n; ++i) printf("%s\n", x[i]); return(0); } void sort_words(char *x[], int y) { int i = 0; int j = 0; for(i = 0; i < y; ++i) for(j = i + 1; j < y; ++j) if(strcmp(x[i], x[j]) > 0) swap(&x[i], &x[j]); } void swap(char **p, char **q) { char *tmp; tmp = *p; *p = *q; *q = tmp; } << Go to Index Page >>

bubble sort using user defined function

bubble sort #include <stdio.h> void bubble_sort(int a[], int size); int main(void) { int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9}; int i = 0; printf("before:\n"); for(i = 0; i < 10; i++) printf("%d ", arr[i]); printf("\n"); bubble_sort(arr, 10); printf("after:\n"); for(i = 0; i < 10; i++) printf("%d ", arr[i]); printf("\n"); return 0; } void bubble_sort(int a[], int size) { int switched = 1; int hold = 0; int i = 0; int j = 0; size -= 1; for(i = 0; i < size && switched; i++) { switched = 0; for(j = 0; j < size - i; j++) if(a[j] > a[j+1]) { switched = 1; hold = a[j]; a[j] = a[j + 1]; a[j + 1] = hold; } } }

bubble sort with struct using c programming

Bubble sort - linked list #define MAX 10 struct lnode { int data; struct lnode *next; } *head, *visit; /* add a new entry to the linked list */ void llist_add(struct lnode **q, int num); /* preform a bubble sort on the linked list */ void llist_bubble_sort(void); /* print the entire linked list */ void llist_print(void); int main(void) { /* linked list */ struct lnode *newnode = NULL; int i = 0; /* a general counter */ /* load some random values into the linked list */ for(i = 0; i < MAX; i++) { llist_add(&newnode, (rand() % 100)); } head = newnode; printf("Before bubble sort:\n"); llist_print(); printf("After bubble sort:\n"); llist_bubble_sort(); llist_print(); return 0; } /* adds a node at the end of a linked list */ void llist_add(struct lnode **q, int num) { struct lnode *tmp; tmp = *q; /* if the list is empty, create first node */ if(*q == NULL) { *q = malloc(sizeof(struct lnode)); tmp = *q; } else {

binary search using c

Binary search #define TRUE 0 #define FALSE 1 int main(void) { int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int left = 0; int right = 10; int middle = 0; int number = 0; int bsearch = FALSE; int i = 0; printf("ARRAY: "); for(i = 1; i <= 10; i++) printf("[%d] ", i); printf("\nSearch for Number: "); scanf("%d", &number); while(bsearch == FALSE && left <= right) { middle = (left + right) / 2; if(number == array[middle]) { bsearch = TRUE; printf("** Number Found **\n"); } else { if(number < array[middle]) right = middle - 1; if(number > array[middle]) left = middle + 1; } } if(bsearch == FALSE) printf("-- Number Not found --\n"); return 0; }

Count number of students above,below and average students

Count no. of students above,below and average students   #include<stdio.h>   #include<conio.h>    void main()     {      int a[10];      int aa=0,ba=0,ae=0,i;      clrscr();      printf("Enter the marks:\n");      for(i=0;i<10;i++)        { scanf("%d",&a[i]); if(a[i]>55) aa++; else if(a[i]<55) ba++; else ae++;        }      printf("No. OF AVG STUDENTS ARE:%d\n",ae);      printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);      printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);     getch();    }

constants usage in C

Basic example showing constants usage in C #include /*constants for bonus rates and sales*/ #define BONUSRATE1 0.1 #define BONUSRATE2 0.15 #define BONUSRATE3 0.2 #define SALES1 2000 #define SALES2 5000 #define SALES3 10000 int main() { int sales; double commission; /*get employees sales*/ printf("Please enter your total sales to the nearest dollar.\n"); scanf("%d", &sales); /*calculate employees bonus based on info*/ if(sales <=2000) { commission = sales * BONUSRATE1; printf("%g\n" , commission); } else if(sales > 2000 && sales <=5000) { commission = sales * BONUSRATE2; printf("%g\n" , commission); } else { commission = sales * BONUSRATE3; printf("%g\n" , commission); } return 0; }

a demo program which represent the ATM transaction

ATM programing ATM C programing language Program code  ‎/*Note Pin code is 1234*/ #include<stdio.h> #include<conio.h> void main(void) { unsigned long amount=1000,deposit,withdr​aw; int choice,pin=0,k=0; char another='y'; while(pin!=1234) { clrscr(); gotoxy(30,25); printf("Enter pin:"); scanf("%d",&pin); } clrscr(); do { printf("********Welcome to ATM Service**************\n"); printf("1. Check Balance\n"); printf("2. Withdraw Cash\n"); printf("3. Deposit Cash\n"); printf("4. Quit\n"); printf("******************​**************************​*\n\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nYour Balance is Rs : %lu ",amount); break; case 2: printf("\nEnter the amount to withdraw: "); scanf("%lu",&withdraw); if(withdraw%100!=0) { printf("\