Showing posts with label Module 1. Show all posts
Showing posts with label Module 1. Show all posts

Friday 12 August 2016

strlwr: Convert from Upper case to Lower case using Library function

String: strlwr(string)

Convert from Upper case to Lower case using Library function


#include<stdio.h>
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nString in lowercase: %s", strlwr(s));
}
Output:
Enter string:   Hello How Are You
String in lowercase:  hello  how are you

Thursday 11 August 2016

Length of the string without using library function

Length of the string without using library function

#include<stdio.h>
main()
{
                char s[20];
                int i = 0 ;
                printf("\n Enter a string: ");
                gets(s);

                while(s[i] != '\0')
                {
                                i++;
                }
                printf("\n Length of string: %d", i );
}
Output:
 Enter a string: Hello Hii
 Length of string: 9

strlen: Length of the string using library function

String: strlen(string)

Length of the string using library function


#include<stdio.h>          
#include<string.h>
main()
{
                char s[60];
                printf("\nEnter string:\t");
                gets(s);
                printf("\nLength of string is: %d", strlen(s));
}

Output:
Enter string:   Hello How Are You
Length of string is: 17

Unformatted Input and output functions for string


Unformatted Input and output functions for string


#include<stdio.h>
void main()
{
           char s[10];
           printf("\nEnter a string: ");
           gets(s);                      //unformatted input
           printf("\nEntered string is: ");
           puts(s);                    //unformatted output
}

Output:
Enter a string: How are you
Entered string is: How are you

Unformatted Input and output functions for character

Unformatted Input and output functions for character


#include<stdio.h>
void main()
{
              char c;
              printf("\nEnter a character: ");
              c = getchar();                    //unformatted input
              printf("\nEntered character is: ");
              putchar(c);                     //unformatted output
}

Output:
Enter a character: h
Entered character is: h

Formatted Input and output functions for string

Formatted Input and output functions for string


#include<stdio.h>
void main()
{
         char s[10];
         printf("\nEnter a string: ");
         scanf("%s", s);                  //formatted input

         printf("\nEntered string is: ");
         printf("%s", s);               //formatted output
}

Output:
Enter a string: How are you
Entered string is: How

Formatted Input and output functions for character

Formatted Input and output functions for character

#include<stdio.h>
void main()
{
          char ch;
          printf("\n Enter a character: ");
          scanf("%c", &ch);                          //formatted input
          printf("\n Entered character is: ");
          printf("%c", ch);                          //formatted output
}

Output:

1) Enter a character: h
    Entered character is: h

2) Enter a character: hii
    Entered character is: h

Tuesday 9 August 2016

Sparse Matrix: Transpose

Sparse Matrix: Transpose

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct
{
       int row;
  int col;
       int val;
}sparse;

void readsparse(sparse a[], int m, int n)
{
  int i, j, k, item, p;
 a[0].row = m;
 a[0].col = n;
 k = 1;

printf("\nEnter the elements:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &item);
if(item == 0)
continue;
a[k].row = i;
a[k].col = j;
a[k].val = item;
k++;
}
}
a[0].val = k-1;

printf("\nThe entered sparse matrix is:\n");
printf("\nRow\tColumn\tValue\n");
for(p=0; p<=a[0].val; p++)
{
printf("%d\t", a[p].row);
printf("%d\t", a[p].col);
printf("%d\n", a[p].val);
}
}

void transpose(sparse a[], sparse b[])
{
          int i, j, k, n, p;
          n = a[0].val;

b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].val  = n;

if( n > 0)
       {
                k = 1;
               for(i =  0; i<a[0].col ; i++)
              {
                     for(j=1; j<=n ; j++)
                    {
                           if(a[j].col == i)
                           {
                                 b[k].row = a[j].col;
                                     b[k].col = a[j].row;
                                     b[k].val = a[j].val;
                                     k++;
                            }
                     }
              }
        }

printf("\nThe Transpose sparse matrix is:\n");
printf("\nRow\tColumn\tValue\n");
for(p=0; p<=a[0].val; p++)
{
printf("%d\t", b[p].row);
printf("%d\t", b[p].col);
printf("%d\n", b[p].val);
}
}

void main()
{
int m, n;
sparse a[MAX], b[MAX];
printf("\nEnter  the no of rows and columns:\t");
scanf("%d%d",&m, &n);
readsparse(a, m, n);
transpose(a,b);
}

Output:

Enter  the no of rows and columns:      6      6

Enter the elements:
15   0     0    22   0   -15
0     11   3    0     0   0
0     0     0    -6   0   0
0     0     0    0    0   0
91   0     0    0    0   0
0     0     28  0    0   0

The entered sparse matrix is:

Row     Column  Value
6                6          8
0                0         15
0                3         22
0                5        -15
1                1        11
1                2        3
2                4       -6
4                1       91
5                3       28

The Transpose sparse matrix is:

Row     Column  Value
6               6            8
0               0           15
1               1           11
1               4           91
2               1           3
3               0           22
3               5          28
4               2         -6
5               0        -15

Sparse Matrix

Sparse Matrix

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct
{
int row;
int col;
int val;
}sparse;

void readsparse(sparse a[], int m, int n)
{
int i, j, k, item, p;
a[0].row = m;
a[0].col = n;
k = 1;

printf("\nEnter the elements:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &item);
if(item == 0)
continue;
a[k].row = i;
a[k].col = j;
a[k].val = item;
k++;
}
}
a[0].val = k-1;

printf("\nThe entered sparse matrix is:\n");
printf("\nRow\tColumn\tValue\n");
for(p=0; p<=a[0].val; p++)
{
printf("%d\t", a[p].row);
printf("%d\t", a[p].col);
printf("%d\n", a[p].val);
}
}

void main()
{
int m, n;
sparse a[MAX];
printf("\nEnter  the no of rows and columns:\t");
scanf("%d%d",&m, &n);
readsparse(a, m, n);
}

Output:
Enter  the no of rows and columns:      6    6

Enter the elements:
15   0   0   22  0  -15
0    11  3   0    0   0
0    0    0   -6   0   0
0    0    0   0    0   0
91  0    0   0    0   0
0    0    28 0    0   0

The entered sparse matrix is:

Row     Column   Value
6           6                8
0           0                15
0           3                22
0           5                -15
1           1                11
1           2                3
2           3               -6
4           0               91
5           2               28

Polynomial Addtion

Polynomail Addition

#include<stdio.h>
#include<stdlib.h>
#define  MAX_TERMS 100    /* size of terms array */

void Creat_polynomail(int start,int finish);
void Print_polynomial(int start,int finish);
int COMPARE(int exp1,int exp2);
void poly_add(int startA, int  finishA, int startB, int finishB, int *startD,int *finishD);

typedef  struct
{
         float  coef;
         int expon;
} polynomial;

polynomial  terms[ MAX_TERMS];

int avail = 0;

void Creat_polynomail(int start,int finish)
{
printf("\nEnter the polynomial:\n");
          while(start <= finish)
         {
                  printf("\n\tEnter details of term %d:",start);
                  printf("\n\tEnter coeff:    ");
                  scanf("%f", &terms[start].coef);
                  printf("\n\tEnter exp:  ");
                  scanf("%d", &terms[start].expon);
                  start++;
          }
}

void Print_polynomial(int start,int finish)
{
while(start<= finish)
           {
                      printf("(%.2f * x^%d)",terms[start].coef,terms[start].expon);
                      if(start != finish)
                                 printf(" + ");
                      start++;
           }
}

int COMPARE(int exp1,int exp2)
{
           if(exp1<exp2)
                      return  -1;
           else if(exp1 == exp2)
                     return   0;
           else
                  return 1;
}

void attach(float coefficient, int exponent)
{
           if (avail > MAX_TERMS)
         {
                     printf("\nToo many terms in the polynomial \n");
                     exit(1);
          }
          terms[avail].coef = coefficient;
          terms[avail].expon = exponent;
          avail++;
}

void poly_add(int startA, int  finishA, int startB, int finishB, int *startD,int *finishD)
{
           float coefficient;
avail = finishB+1;

           *startD = avail;

           while ( startA <= finishA && startB <=finishB)
          {
                    switch (COMPARE(terms[startA].expon, terms[startB].expon))
{
                              case -1:
                                             attach(terms[startB].coef, terms[startB].expon);
                                             startB ++;
                                             break;
                              case 0:
                                            coefficient = terms[startA].coef + terms[startB].coef;
                                            if (coefficient)
                                                       attach(coefficient, terms[startA].expon);
                                            startA++;
                                            startB ++;
                                            break;
                              case 1:
                                           attach(terms[startA].coef, terms[startA].expon);
                                           startA++;
                    }
          }
           for (; startA <= finishA;  startA++)
                             attach(terms[startA].coef, terms[startA].expon);
           for (; startB <= finishB;  startB++)
                              attach(terms[startB].coef, terms[startB].expon);
            *finishD = avail-1;

    printf("\n startD=%d", *startD);
             printf("\n finishD=%d", *finishD);

printf("\n Resultant polynomial is: \n");
            Print_polynomial(*startD, *finishD);
}

void main()
{
            int startA, finishA;
            int startB, finishB;
            int startD, finishD;
printf("\n Enter start and end for A:  ");
            scanf("%d%d", &startA, &finishA);
            Creat_polynomail(startA, finishA);
            printf("\nThe polynomial A is: \n");
            Print_polynomial(startA, finishA);

             printf("\n\n Enter start and end for B:   ");
             scanf("%d%d", &startB, &finishB);
             Creat_polynomail(startB, finishB);
             printf("\nThe polynomial B is: \n");
             Print_polynomial(startB, finishB);

            poly_add(startA, finishA, startB, finishB, &startD, &finishD);
}
Output:

Enter start and end for A:  0         1

Enter the polynomial:

        Enter details of term 0:
        Enter coeff:    2
        Enter exp:  1000

        Enter details of term 1:
        Enter coeff:    1
        Enter exp:  0

The polynomial A is:
(2.00 * x^1000) + (1.00 * x^0)

Enter start and end for B:   2       5

Enter the polynomial:

        Enter details of term 2:
        Enter coeff:    1
        Enter exp:  4

        Enter details of term 3:
        Enter coeff:    10
        Enter exp:  3

        Enter details of term 4:
        Enter coeff:    3
        Enter exp:  2

        Enter details of term 5:
        Enter coeff:    1
        Enter exp:  0

The polynomial B is:
(1.00 * x^4) + (10.00 * x^3) + (3.00 * x^2) + (1.00 * x^0)

 startD = 6
 finishD = 10

 Resultant polynomial is:
(2.00 * x^1000) + (1.00 * x^4) + (10.00 * x^3) + (3.00 * x^2) + (2.00 * x^0)

Monday 8 August 2016

2 Dimensional Arrays: Addresses and Values

2 Dimensional Arrays: Addresses and Values 

#include<stdio.h>
void main()
{
           int a[3][4] = {      {10,11,12,13}, {20,21,22,23}, {30,31,32,33}      };
           int i, j;

           for(i=0; i<3;  i++)
                    printf("\n *(a+%d) is %d", i, *(a + i) );

           for(i=0;i<3;i++)
           {
                    for(j=0; j<4; j++)
                   {
                               printf("\n (*(a+%d) + %d) is %d", i, j, (*(a + i) + j) );
                    }
           }

           for(i=0;i<3;i++)
          {
                    for(j=0; j<4; j++)
                   {
                              printf("\n *(*(a+%d) + %d) is %d", i, j, *(*(a + i) + j) );
                   }
          }
}


Output:

 *(a+0) is 2686736
 *(a+1) is 2686752
 *(a+2) is 2686768

--------------------------------------
 (*(a+0) + 0) is 2686736
 (*(a+0) + 1) is 2686740
 (*(a+0) + 2) is 2686744
 (*(a+0) + 3) is 2686748

 (*(a+1) + 0) is 2686752
 (*(a+1) + 1) is 2686756
 (*(a+1) + 2) is 2686760
 (*(a+1) + 3) is 2686764

 (*(a+2) + 0) is 2686768
 (*(a+2) + 1) is 2686772
 (*(a+2) + 2) is 2686776
 (*(a+2) + 3) is 2686780

------------------------------------
 *(*(a+0) + 0) is 10
 *(*(a+0) + 1) is 11
 *(*(a+0) + 2) is 12
 *(*(a+0) + 3) is 13

 *(*(a+1) + 0) is 20
 *(*(a+1) + 1) is 21
 *(*(a+1) + 2) is 22
 *(*(a+1) + 3) is 23

 *(*(a+2) + 0) is 30
 *(*(a+2) + 1) is 31
 *(*(a+2) + 2) is 32
 *(*(a+2) + 3) is 33

Thursday 4 August 2016

Arrays Bubble sort

Sorting an Array using Bubble Sort

#include<stdio.h>
void main()
{
         int i, j, n, a[10], temp;
         printf("\nEnter the value of n:");
         scanf("%d", &n);

         printf("\nEnter array elements:");
         for(i=0;  i<n;  i++)
                   scanf("%d", &a[i]);

         for(i=0; i<n-1; i++)
        {
              for(j=0; j<n-1-i; j++)
              {
                    if(a[j+1]<a[j])
                    {
                             temp = a[j+1];
                             a[j+1] = a[j];
                             a[j] = temp;
                     }
                }
         }

          printf("\nBubble sort: array elements:");
          for(i=0;i<n;i++)
                   printf("%d ", a[i]);
 }

Output

Enter the value of n: 7
Enter array elements: 3 2 1 5 6 9 1

Bubble sort: array elements: 1 1 2 3 5 6 9

Arrays Selection Sort

Sorting an Array using Selection Sort

#include<stdio.h>
void main()
{
          int i, j, n, a[10], temp;
          printf("\nEnter the value of n:");
          scanf("%d", &n);

          printf("\nEnter array elements:");
          for(i=0;i<n;i++)
                  scanf("%d", &a[i]);

          for(i=0; i<n-1; i++)
         {
               for(j=i+1; j<n; j++)
               {
                     if(a[j]<a[i])
                    {
                            temp = a[j];
                            a[j] = a[i];
                            a[i] = temp;
                    }
               }
          }

          printf("\nSelection sort: array elements:");
          for(i=0;i<n;i++)
                  printf("%d ", a[i]);
}

Output:
Enter the value of n:7
Enter array elements:3 2 1 5 6 9 1
Selection sort: array elements:1 1 2 3 5 6 9

Structure and Union Initialization

Structure and Union Initialization


#include<stdio.h>
struct first
{
            int x, y;
};

union second
{
            int x, y;
};
main()
{
            struct first f1 = {1, 2};
            printf("\nf1.x = %d \t f1.y = %d", f1.x, f1.y);

            union second s1;
            s1.x = 3;
            printf("\ns1.x = %d\t", s1.x);
            s1.y = 4;
            printf("\ns1.y = %d", s1.y);

            union second s2 = {6, 7};
            printf("\ns2.x = %d\t s2.y = %d", s2.x, s2.y);

            union second s3;
            s3.x = 10;
            s3.y = 9;
            printf("\ns3.x = %d\t s3.y = %d", s3.x, s3.y);
}
Output:
f1.x = 1   f1.y = 2
s1.x = 3   s1.y = 4
s2.x = 6   s2.y = 6
s3.x = 9   s3.y = 9

Unions

Pointers

Pointer to Pointer

Pointer to Pointer


#include<stdio.h>
void main()
{
                int a = 5;
                int *p;
                int **q;
                p = &a;
                q = &p;

                printf("\n a = %d", a);
                printf("\n &a = %p", &a);

                printf("\n  p = %p",  p);
                printf("\n  *p = %d",  *p);
                printf("\n  &p = %p", &p);

                printf("\n q = %p", q);
                printf("\n  *q = %p", *q);
                printf("\n  **q = %d", **q);
                printf("\n &q = %p", &q);
}

Output:
 a            = 5
 &a         = 0028FF44

 p            = 0028FF44
 *p          = 5
 &p         = 0028FF40

 q            = 0028FF40
 *q          = 0028FF44
 **q        = 5
 &q         = 0028FF3C

Pointer Arithmetic

Pointer Arithmetic


#include<stdio.h>
void main()
{
                char c = 'x', *pc;
                int i = 11, *pi;
                float f = 35.6, *pf;
                pc = &c;
                pi = &i;
                pf = &f;
                printf("\nValue of pc = Address of c = %p ", pc);
                printf("\nValue of pi = Address of i = %p ", pi);
                printf("\nValue of pf = Address of f = %p ", pf);

                pc++;
                pi++;
                pf++;

                printf("\nNow Value of pc = %p", pc);
                printf("\nNow Value of pi = %p", pi);
                printf("\nNow Value of pf = %p", pf);
}

Output:
Value of pc  =     Address of c       = 0028FF47
Value of pi   =     Address of i        = 0028FF3C
Value of pf   =    Address of f       = 0028FF34
Now Value of pc = 0028FF48
Now Value of pi = 0028FF40
Now Value of pf = 0028FF38

Pointers and address basics

Pointers and Address


#include<stdio.h>
void main()
{
                 int a = 10;
                float b = 35.6;
                int *p1;
                float *p2;
                p1 = &a;
                p2 = &b;
                printf("\nAddress of a = %p ", &a);
                printf("\nAddress of b = %p ", &b);

                printf("\nValue of a = %d %d %d", a, *p1, *(&a));
                printf("\nValue of b = %.1f %.1f %.1f", b, *p2, *(&b));

                printf("\nValue of p1 = Address of a = %p ", p1);
                printf("\nValue of p2 = Address of b = %p ", p2);

                printf("\nAddress of p1 = %p ", &p1);
                printf("\nAddress of p2 = %p ", &p2);
}

Output:
Address of a = 0028FF44
Address of b = 0028FF40
Value of a = 10    10   10
Value of b = 35.6   35.6   35.6
Value of p1 = Address of a = 0028FF44
Value of p2 = Address of b = 0028FF40
Address of p1 = 0028FF3C
Address of p2 = 0028FF38

Tuesday 2 August 2016

Arrays Linear Search

Search for key Element in an Array using Linear Search

Download the Program

#include<stdio.h>
void main()
{
    int i, j, n, a[100], key;
    printf("\nEnter value of n:");
    scanf("%d", &n);
    printf("\nEnter array elements:");
    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("\nEnter key:");
    scanf("%d", &key);

    for(i=0; i<n; i++)
    {
            if(a[i] == key)
                break;
    }
    if(i<n)
        printf("\nKey found at position %d", i+1);
    else
        printf("\nNot found");
}

Output:
Enter value of n: 6
Enter array elements: 1  3  2  4  5  1
Enter key: 3
Key found at position 2