Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Sunday, 31 July 2016

Selection Sort



#include<stdio.h>
void s_sort(int* ,int);
int main()
{
int i;

// intialize array and give value
int a[]={-6,4,-8,0,2,3,1};
int n=(sizeof(a))/(sizeof(int));
printf("%d\n",n);

// define function
s_sort(a,n);

// print sorted array
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

void s_sort(int* a,int n)
{
int i,j,temp;

// intialize loop for sorting elements
for(i=0;i<(n-1);i++)
{
// define minimum value

int min = i;

// find minimum value in array
for(j=i+1;j<n;j++)
{
if (a[j] < a[min]) 
            min = j;
}

// if minimum value is find in array then swap 
if(min!=i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}


Evaluate Postfix


stack.h


#include <stdlib.h>
typedef struct stack
{
    int *values;
    int maxSize;
    int top;
}stack;

stack * createstack(int maxElements)
{
    //create stack
    stack *s;
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (int *)malloc(sizeof(int)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
    
    //return
    return s;
}

void init(stack *s,int maxElements)
{
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (int *)malloc(sizeof(int)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
}

void push(stack *s, int n)
{
    if(s->top == s->maxSize)
    {
        printf("stack is Full\n");
    }
    else
    {
        printf("Pushing Value:%d\n",n);

        //increament first then push
        ++s->top;
        s->values[s->top] = n;
    }
    return;
}

int pop(stack *s)
{
    int value;
    if(s->top == -1)
    {
        printf("stack is Empty\n");
    }
    else
    {
        //remove first then decrement
        value = s->values[s->top];
        s->top--;
        printf("Poping value:%d\n",value);
    }
    return  value;
}

void display(stack *s)
{
    int i=0;
    for(i=0;i<=s->top;i++)
    {
        printf("%d ",s->values[i]);
    }
}

imp.c


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"
int calc(int n1,int n2, char c){
  switch(c)
  {
    case '*':
      return n1*n2;
      break;

    case '/':
      return n1/n2;
      break;

    case '+':
      return n1+n2;
      break;

    case '-':
      return n1-n2;
      break;

    default:
      return 0;
  }
}

void main()
{
  int i=0,value=0;
  stack *s = createstack(10);
  char *exp = "10 10 10 9 * + +";
  for(i=0;i<strlen(exp);i++)
  {
    if(isdigit(exp[i]))
    {
      value = (value * 10) + (exp[i] - '0');
    }
    else if(exp[i] == '+' || exp[i] == '*' || exp[i] == '/' || exp[i] == '-'){
      value = calc(pop(s),pop(s),exp[i]);
    }
    else
    {
      push(s,value);
      value =0;
    }
  }
  printf("%d\n",value);
}

http://bebe.com

Tuesday, 26 July 2016

Infix To Postfix Converter



char_stack.h


typedef struct stack
{
char *values;
int maxSize;
int top;
}stack;

stack * createstack(int maxElements)
{
//create stack
    stack *s;
    s = (stack *)malloc(sizeof(stack));
    
    //initialize
    s->values = (char *)malloc(sizeof(char)*maxElements);
    s->top = -1;
    s->maxSize = maxElements;
    
    //return
    return s;
}

void push(stack *s, char n)
{
if(s->top == s->maxSize)
    {
        printf("stack is Full\n");
    }
    else
    {
        printf("Pushing Value:%c\n",n);

        //increament first then push
        ++s->top;
        s->values[s->top] = n;
    }
    return;
}

char pop(stack *s)
{
char value;
if(s->top == -1)
    {
        printf("stack is Empty\n");
    }
    else
    {
        //remove first then decrement
    value = s->values[s->top];
        s->top--;
        printf("Poping value:%c\n",value);
    }
    return  value;
}

int isEmpty(stack *s)
{
    if(s->top == -1)
        return 1;
    else
        return 0;
}

void display(stack *s)
{
    int i=0;
    for(i=0;i<=s->top;i++)
    {
        printf("%c",s->values[i]);
    }
}

Postfix.c


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "char_stack.h"
#define SIZE 30
#define OPERAND 100

int priority(char c)
{
int priority;
switch(c){
case '^':
priority = 4;
break;
case '/':
case '*':
priority = 3;
break;
case '+':
case '-':
priority = 2;
break;
case '(':
priority = 1;
break;
case '$':
priority = 0;
break;
default:
//default operand
priority = OPERAND;
break;
}
return priority;
}

char *inTopost(char *infix)
{
int i = 0;
char x;
//init postfix
char *postfix = (char*)malloc( SIZE *sizeof(char));
//init stack
stack *s = createstack(SIZE);

//push $ as starting char
push(s,'$');

//postfix counter
int j = 0;
//iterate over infix
for (i = 0; i < strlen(infix); i++){

//get infix charcter
   char in =  infix[i];

   printf("%d.Character:%c\n",i+1,in);

   //get top of stack character
   char tos = s->values[s->top];

   //check infix character

   //if input character is '(' push it
   if(in=='(')
   {
    push(s,in);
   }

   //if input character is ')' then pop all character till ')'
   else if(in== ')')
   {
    while((x=pop(s))!='(')
{
    postfix[j++]=x;
}
   }
   //if infix character priority=-1 then it is operand
else if(priority(in)==OPERAND)
{
//if operand to directly move to postfix
postfix[j++]= in;
}

//else check priority of stack character and infix character
else if(priority(in) > priority(tos)){

//if priority of infix is greater then or equal to infix then push character to stack
push(s,in);
}

//else pop all the from stack till infix priority is less or equal than stack
//and push infix character
else{
while(priority(in) <= priority(tos))
{
postfix[j++] = pop(s);
tos = s->values[s->top];
}
push(s,in);
}
printf("  %-27s",postfix);
display(s);
printf("\n\n");
}

//pop all stack character till its not '$'
while((x=pop(s))!='$')
{
      postfix[j++]=x;
}

// return postfix string
return postfix;
}

int main()
{
//initialize array
char *infix;

//read value
infix = "(A+B)*D+E/(F+G*D)+C";
//scanf("%s",infix);

//convert to postfix
char *postfix = inTopost(infix);

//print postfix value
printf("%s\n", postfix);
return 0;
}

Thursday, 21 July 2016

Insertion Sort



#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("Enter total number of element :");
scanf("%d",&n);
int a[n];
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }

printf("\n");

for(i=1;i<n;i++)
    {
    for(j=i;j>0;j--)
        {
            if(a[j-1]>a[j])
                {
                    temp=a[j-1];
                    a[j-1]=a[j];
                    a[j]=temp;
                }
        }
     }
   
printf("After Sorting: \n");
for(i=0;i<n;i++)
        {
            printf("%d\n",a[i]);
        }
    return 0;
}

Bubble Sort



#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("Enter total number of element :");
scanf("%d",&n);
int a[n];
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }

printf("\n");

for(i=0;i<(n-1);i++)
    {
    for(j=1;j<(n-i);j++)
        {
            if(a[j-1]>a[j])
                {
                    temp=a[j-1];
                    a[j-1]=a[j];
                    a[j]=temp;
                }
        }
     }
   
printf("After Sorting: \n");
for(i=0;i<n;i++)
        {
            printf("%d\n",a[i]);
        }
    return 0;
}

Monday, 18 April 2016

Sum of two matrix by using array in c


 

#include <stdio.h>
#include<conio.h>
int main()
{
   int m, n, i, j, first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &first[i][j]);

   printf("Enter the elements of second matrix\n");

   for (i = 0; i < m; i++)
      for (j = 0 ; j < n; j++)
            scanf("%d", &second[i][j]);

   printf("Sum of entered matrices:\n");

   for (i = 0; i < m; i++) {
      for (j = 0 ; j < n; j++) {
         sum[i][j] = first[i][j] + second[i][j];
         printf("%d\t", sum[i][j]);
      }
      printf("\n");
   }
   getch();
   return 0;
}

Ads:-India's largest store flipkart.com