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 {
/* go to last node */
while(tmp->next != NULL)
tmp = tmp->next;

/* add node at the end */
tmp->next = malloc(sizeof(struct lnode));
tmp = tmp->next;
}

/* assign data to the last node */
tmp->data = num;
tmp->next = NULL;
}

/* print the entire linked list */
void llist_print(void)
{
visit = head;

while(visit != NULL)
{
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}

/* preform a bubble sort on the linked list */
void llist_bubble_sort(void) {
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *e = NULL;
struct lnode *tmp = NULL;

/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != head->next)
{
c = a = head;
b = a->next;
while(a != e)
{
if(a->data > b->data)
{
if(a == head)
{
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else
{
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}

<< Go to Index Page >>

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("\nPlease enter

amount in multiples of 100");
}else if(withdraw>(amount-500))
{

printf("\nInsufficient Funds");
}else
{

amount=amount-withdraw;

printf("\n\nPlease collect cash");

printf("\nYour balance is %lu",amount);
}
break;
case 3:
printf("\nEnter amount to deposit");
scanf("%lu",&deposit);
amount=amount+deposit;
printf("Your balance is %lu",amount);
break;
case 4:
printf("\nThank you for using ATM");
break;
default:
printf("\nInvalid Choice");
}
printf("\n\n\nDo you want another transaction?(y/n): ");
fflush(stdin);
scanf("%c",&another);
if(another=='n'||another==​'N')
k=1;
}while(!k);
printf("\n\nHave a nice day");
getch();

}

Descending an array using c programming

ARRANGE THE ELEMENTS IN ARRAY IN DESCENDING ORDER

main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

add two numbers without using operator as like +

Add Two numbers without using operator


#include<stdio.h>
int main(){
  
    int a,b;
    int sum;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    //sum = a - (-b);
    sum = a - ~b -1;
    printf("Sum of two integers: %d",sum);
    return 0;
}

Add two matrices and store the result into another matrice

Add two matrices and store the result

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the elements into matrix A\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the elements into matrix B\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
{
printf("\t\t");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n\a");
}


getch();
}

use of pointer with array to do a sum


Add Pointers

#include<stdio.h>
#include<conio.h>
 void main()
 {
 int a[10],sum=0;
 int *j;
 int i,n;
 clrscr();
 printf("Enter how many elements u want to add:");
 scanf("%d",&n);
 printf("Enter the elements:");
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 }
  j=&a[0];
 for(i=0;i<n;i++)
     {


       sum=sum+*j;
       j++;
     }
 printf("sum=%d",sum);
}

Add numbers using command line arguments (CLA)

Add numbers using command line arguments (CLA)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main(int argc,char *argv[])
{
int sum=0,i;
//Compare if proper number of arguments have been entered
if(argc<3)

{
printf("Insufficient number of arguments:\n");
getch();
return 0;
}

//Add all the numbers entered using atoi function
for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}

//print the sum
printf("Ans=%d",sum);
getch();
}

bubble sort using user defined function

A bubble sort routine

# include
# include
void bubblesort(int array[],int size);
void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);
}
void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

insertion sort using struct

2d example insertion sort

#include <stdio.h>
#include <conio.h>

struct node
{
int number;
struct node *next;
};

struct node *head = NULL;

/* insert a node directly at the right place in the linked list */
void insert_node(int value);

int main(void)
{
struct node *current = NULL;
struct node *next = NULL;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i = 0;

/* insert some numbers into the linked list */
for(i = 0; i < i =" 0;">next != NULL)
{
printf("%4d\t%4d\n", test[i++], head->number);
head = head->next;
}

/* free the list */
for(current = head; current != NULL; current = next)
next = current->next, free(current);

return 0;
}

void insert_node(int value)
{
struct node *temp = NULL;
struct node *one = NULL;
struct node *two = NULL;

if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}

one = head;
two = head->next;

temp = (struct node *)malloc(sizeof(struct node *));
temp->number = value;

while(two != NULL && temp->number <>number) {
one = one->next;
two = two->next;
}

one->next = temp;
temp->next = two;
}

Find address of char, string, integer using c program

Find address of char, string, integer

#include<stdio.h>
#include<conio.h>
 main()
  {
   char *chp,*sp;
   int i;
   char ch,s[10];
   int *ip;
   clrscr();
   printf("Enter a char:");
   scanf("%c",ch);
   printf("Enter a string:");
   scanf("%s",s);
   printf("Enter a integer:");
   scanf("%d",&i);
   chp=&ch;
   sp=s;
   ip=&i;
   printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");
   printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);
   printf("\nchar pointer value is:%u",chp);
   printf("\nstring pointer value is:%u",sp);
   printf("\nint pointer value is:%u",ip);
   getch();
  }

Subtraction of Two Matrices

Subtraction of Two Matrices


C Program for subtraction of two matrices 

#include<stdio.h>
#include<conio.h>

//Read Matrix
void read_mat(float a[][10],int m,int n)
{
int i,j;
printf("\n\nEnter %d X %d matrix below:\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}

//Write Matrix
void write_mat(float a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
}

//Subtract matrices
void sub_mat(float a[][10],float b[][10],float c[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j] - b[i][j];
}

//main function
void main()
{
float x[10][10],y[10][10],z[10][10];
int m,n;
clrscr();

//Accept two matrices
printf("\n\nEnter order of matrix \n");
scanf("%d %d",&m,&n);
read_mat(x,m,n);
read_mat(y,m,n);

//call sub_mat() function
sub_mat(x,y,z,m,n);
printf("\n\nSUBTRACTION OF THE GIVEN MATRICES IS:\n");
write_mat(z,m,n);
getch();
}

Swapping of two Values using Functions

  c program to Swapping of two Values using Functions


#include<stdio.h>
#include<conio.h>
int swapval(int,int);
int swapref(int*,int*);
int a,b;
void main()
{
clrscr();
printf(“enter the two values\n”);
scanf(%d%d”,&a,&b);
printf(“pass by value\n”);
printf(“before function call a=%d b=%d “,a,b);
swapval(a,b);
printf(“after function swapval a=%d b=%d “,a,b);
printf(“pass by reference\n”);
printf(“before function call a=%d b=%d “,a,b);
swapref(&a,&b);
printf(“after function swapref a=%d b=%d “,a,b);
getch();
}
swapval(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf(“\nwith swap val x=%d y=%d”,x,y);
}
swapref(int*x,int*y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
printf(“\nwith swapref x=%d y=%d “,*x,*y);
}

Output:
give two numbers 
5
6
pass by value
 before function call a=5 b=6
with swapval x=6 y=5
after function swapval a=5 b=6
pass by reference
before function call a=5 b=6
with swapref x=6 y=5l
after function swapref a=6 b=5

delete Characters from a given position in a given string

To delete n Characters from a given position in a given string

/* Write a C program that uses functions to perform the following operations:
To delete n Characters from a given position in a given string.
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts(“Enter the string”);
gets(string);
printf(“Enter the position from where to delete”);
scanf(“%d”,&pos);
printf(“Enter the number of characters to be deleted”);
scanf(“%d”,&n);
delchar(string, n,pos);
getch();
}
// Function to delete n characters
void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}

Total Number of Consonants in a String

Total Number of Consonants in a String

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int c=0,i,l,p;
char a[10];
clrscr();
printf("program that gives total number of consonants in a string");
printf("\n\n\n\t\t------------INPUT-------------");
printf("\n\nenter any string");//taking input from the user
scanf("%s",&a);
l=strlen(a);
for(i=0;i
{
if(a[i]=='a' || a[i]=='e' || a[i]=='o' || a[i]=='i' || a[i]=='u')
c++;
}
p=l-c;
printf("\n\n\n\t\t------------OUTPUT------------");
printf("\n\nthe total no. of consonants in the string are=%d",p);//printing output
getch();
}

Tree sort- string array

Tree sort- string array

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

struct tnode
{
char *str;
struct tnode *left;
struct tnode *right;
};

void insert(struct tnode **p, char *value);
void print(struct tnode *root);

int main(void)
{
char line[1024];
struct tnode *root;

root = NULL;
while((fgets(line, 1024, stdin)) != NULL)
insert(&root, line);

print(root);
return 0;
}

/* call by reference .. ! */
void insert(struct tnode **p, char *value)
{
if(!*p) {
*p = (struct tnode *)malloc(sizeof(struct tnode));
(*p)->left = (*p)->right = NULL;
(*p)->str = strdup(value);
return;
}

if(strcmp(value, (*p)->str) < 0)
insert(&(*p)->left, value);
else
insert(&(*p)->right, value);
}

/* inorder binary tree print ... */
void print(struct tnode *root)
{
if(root != NULL)
{
print(root->left);
printf("%s", root->str);
print(root->right);
}
}