SlideShare a Scribd company logo
1 of 87
2012
Saket Kumar Pathak
Software Developer
3D Graphics




[Programming in C of College days ]
Programs are tested with compiler and respective IDEs Bloodshed-DevC++, Visual Studio
2008, Qt 4.2. These are running successfully with console in windows platform. So just
enjoy coding. For Visual Studio 2008 and Qt 4.2, please notice the note at the end.
Programming in C of College days

   1. WAP to convert
      a. Binary to Decimal
      b. Decimal to Binary

Program:
     Binary to Decimal:

#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include   <math.h>

#define DATA_SIZE 32

char* rev_data_seq(char* chp_data_seq);

void bin_to_dec()
{
     printf("WAP to convert :nt-> Binary to Decimal");
     printf("nLimitation: nt-> Fractional Numbers will not
     considered. nt-> Binary format is limited to 32 bit.
     nt-> Input will considered in Unsigned format.");
     printf("nnn");

     int i_res, i_num, i_count;
     char ch_digit;
     char* chp_data_seq = (char*)malloc(sizeof(char) *
DATA_SIZE);

      i_res = i_num = i_count = 0;

      printf("Please Enter any number (Binary in base 2): ");

      while((ch_digit = getchar()) != 'n')
      {
           if((ch_digit == '0')||(ch_digit == '1'))
           {
                *(chp_data_seq + i_count) = ch_digit;
                i_count++;
           }
           else if(i_count > 32)
           {
                i_res = 0;
                printf("nInvalid Input.n");
                break;
           }
           else

M.Tech – CSE – Weekend (2012 – 2015)                            Page 2
Programming in C of College days

             {
                   i_res = 0;
                   printf("nInvalid Input.n");
                   break;
           }
      }
      *(chp_data_seq + i_count) = '0';

      chp_data_seq = rev_data_seq(chp_data_seq);

      i_count = 0;
      while (*(chp_data_seq + i_count) != '0')
      {
           ch_digit = *(chp_data_seq + i_count);
           if(ch_digit == '0')
                i_num = (int)(0 * pow(2.0f, i_count));
           else if(ch_digit == '1')
                i_num = (int)(1 * pow(2.0f, i_count));
           i_res += i_num;
           i_count++;
      }

      printf("nResultant: %d", i_res);
}

char* rev_data_seq(char* chp_data_seq)
{
     char* temp_seq = (char*)malloc(sizeof(char) *
     (strlen(chp_data_seq) + 1));

      int i_count = 0;
      int i_rev = (strlen(chp_data_seq)-1);
      while (*(chp_data_seq + i_count) != '0')
      {
           *(temp_seq + i_rev) = *(chp_data_seq + i_count);
           i_count++;
           i_rev--;
      }

      *(temp_seq + i_count) = '0';

      return temp_seq;
}




M.Tech – CSE – Weekend (2012 – 2015)                          Page 3
Programming in C of College days

int main()
{
    dec_to_bin();

     getch();
     return 0;
}


       ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~


      Decimal to Binary:

#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include   <math.h>

#define DATA_SIZE 32

void dec_to_bin()
{
     printf("WAP to convert :nt-> Decimal to Binary");
     printf("nLimitation: nt-> Fractional Numbers will not
     considered. nt-> Binary format is limited to 32 bit.
     nt-> Generated result will be in Unsigned format.");
     printf("nnn");

      int i_num;
      printf("Please Enter any number (Decimal in base 10): ");
      scanf("%d", &i_num);

      int* i_res = (int*)malloc(sizeof(int) * DATA_SIZE);
      int i_count = 0;
      while(i_count < DATA_SIZE)
      {
           *(i_res + i_count) = 0;
           i_count++;
      }

      printf("n");
      i_count = (DATA_SIZE-1);
      int i_quotent, i_remainder;
      do
      {
           i_quotent = i_num / 2;

M.Tech – CSE – Weekend (2012 – 2015)                         Page 4
Programming in C of College days

             i_remainder = i_num % 2;
             i_num = i_quotent;
             *(i_res + i_count) = i_remainder;
             i_count--;
      }
      while(i_quotent > 0);

      printf("nResultant: ");
      i_count = 0;
      while(i_count < DATA_SIZE)
      {
           printf("%d", *(i_res + i_count));
           i_count++;
      }
}

int main()
{
    dec_to_bin();

     getch();
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 5
Programming in C of College days

    2. WAP to generate prime numbers within a given range.

Program:

#include <stdio.h>
#include <stdbool.h>

int i_lr, i_hr, i_count_prime;
int *i_collector;
bool b_check;

void set_argument()
{
    printf("Please Enter Number to for lower limit of Range: ");
    scanf("%d", &i_lr);
    printf("Please Enter Number to for Upper limit of Range: ");
    scanf("%d", &i_hr);

     printf("nnn");
}

void find_numbers()
{
    printf(" Given Range to find prime numbers is %d - %d.n",
i_lr, i_hr);

     i_collector = (int*)malloc(sizeof(int) * 1024);
     *i_collector = 1;

    {
    int i_count;
        for(i_count = i_lr; i_count <= i_hr; ++i_count)
        {
            if(i_count > 3)
            {
            int i_loopcount;
for(i_loopcount = 2; i_loopcount <= (i_count/2); ++i_loopcount)
                 {
                     if((i_count % i_loopcount) == 0)
                     {
                          b_check = true;
                          break;
                     }
                     else
                          b_check = false;
                 }
                 if(!b_check)

M.Tech – CSE – Weekend (2012 – 2015)                         Page 6
Programming in C of College days

                      {
                          *i_collector = i_count;
                          i_collector+=sizeof(int);
                          i_count_prime++;
                      }
               }
               else
               {
                      *i_collector = i_count;
                      i_collector+=sizeof(int);
                      printf("n The Number 2 and 3 are prime.");
               }
          }
     }
     printf("nnn");
}

void show_result()
{
    i_collector-=sizeof(int);

     printf("Prime Number So obtained: n");
     printf("Index t PrimeNumbern");

     int i_idx = 1;
     while(i_count_prime > 0)
     {
         printf(" %dt %dn", i_idx, *i_collector);
         i_collector = i_collector - sizeof(int);
         --i_count_prime;
         ++i_idx;
     }
}
int main()
{
    printf("WAP to Find Prime numbers with a given range.");
    printf("nnn");

     set_argument();
     find_numbers();
     show_result();

     printf("nnn");
     system("pause");
     return 0;
}


M.Tech – CSE – Weekend (2012 – 2015)                                Page 7
Programming in C of College days

    3. WAP to generate following patterns, if line =5
           (a)

                                1
                                0      1
                                1      0   1
                                0      1   0    1
                                1      0   1    0       1


             (b)

                                         *
                                        ***
                                      * ****
                                     *******
                                    *********

Program:

      (a)

#include <stdio.h>
#include <stdbool.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t1n");
     printf("t01n");
     printf("t101n");
     printf("t0101n");
     printf("t10101n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}



M.Tech – CSE – Weekend (2012 – 2015)                        Page 8
Programming in C of College days

void draw_pattern()
{
    int container[2] = {1,0};

     unsigned int ui_row, ui_col;
     for(ui_row = 1; ui_row <= (unsigned int)i_number; ++ui_row)
     {
         printf("nt");
         int count;
         bool b_flag = true;
         for(ui_col = 1; ui_col <= ui_row; ++ui_col)
         {
             if(b_flag)
             {
                 if((ui_row%2)==0)
                      count = 1;
                 else
                      count = 0;
                 b_flag = false;
             }

               printf("%d", container[count]);

               if(count==1)
                    count = 0;
               else
                    count = 1;
          }
     }
}

int main()
{
    set_argument();
    draw_pattern();

     getch();
     return 0;
}



    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~




M.Tech – CSE – Weekend (2012 – 2015)                         Page 9
Programming in C of College days

      (b)

#include <stdio.h>
#include <stdbool.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t    *n");
     printf("t   ***n");
     printf("t *****n");
     printf("t *******n");
     printf("t*********n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}

void draw_pattern()
{
      unsigned int ui_row, ui_blank, ui_col;
   for(ui_row = 0; ui_row <= (unsigned int)i_number; ++ui_row)
    {
         printf("nt");
         for(ui_blank = 1; ui_blank <= (i_number-ui_row);
++ui_blank)
             printf(" ");

            for(ui_col = 1; ui_col <= (2*ui_row + 1); ++ui_col)
                printf("*");
     }
}




M.Tech – CSE – Weekend (2012 – 2015)                              Page 10
Programming in C of College days

int main()
{
    set_argument();
    draw_pattern();

     getch();
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 11
Programming in C of College days

    4. WAP to generate recursive Fibonacci series.

Program:

#include <stdio.h>
#include <stdbool.h>

void create_fibonacci(bool flag, unsigned int i_count);

int main()
{
    printf("WAP to generate Fibonacci series using
recursion.n");
    printf("nnn");

     int i_term, i_rec;
     unsigned int ui_count;
     printf("Enter a term to create fibonacci: ");
     scanf("%d", &i_term);

     printf("Fibonacci: 0 1");
     create_fibonacci(true, (i_term-2));

     system("pause");
     return 0;
}

void create_fibonacci(bool flag, unsigned int i_count)
{
     static int i_num_1, i_num_2, i_num_3;

      if (flag)
      {
         i_num_1 = 0;
         i_num_2 = 1;
         flag = false;
      }

      if (i_count > 0)
      {
         i_num_3 = i_num_1 + i_num_2;
         printf(" %d", i_num_3);

          i_num_1 = i_num_2;
          i_num_2 = i_num_3;
          create_fibonacci(false, --i_count);
      }

M.Tech – CSE – Weekend (2012 – 2015)                      Page 12
Programming in C of College days

      printf("n");
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 13
Programming in C of College days

    5. WAP to perform binary search by using
        a. Recursion.
        b. Non recursion.

Program:

      a. Recursion

#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include   <stdbool.h>
#include   <math.h>

bool check_item_order(int* i_storage)
{
     //Function to check the Item - List is in Sorted order.
      return true;
}

void recursive_bin_search(int* i_storage, int i_srch_item, int
i_low, int i_mid, int i_hi)
{
     if(i_low < i_hi)
          {
               if(i_storage[i_mid] == i_srch_item)
               {
printf("Item (%d) is found at position: %d", i_srch_item,
i_mid);
               }
               else if(i_storage[i_mid] > i_srch_item)
               {
                    i_hi = i_mid;
                    i_mid = (i_low + i_hi)/2;
recursive_bin_search(i_storage, i_srch_item, i_low, i_mid,
i_hi);
               }
               else if(i_storage[i_mid] < i_srch_item)
               {
                    i_low = i_mid;
                    i_mid = (i_low + i_hi)/2;
recursive_bin_search(i_storage, i_srch_item, i_low, i_mid,
i_hi);
               }
          }

}

M.Tech – CSE – Weekend (2012 – 2015)                       Page 14
Programming in C of College days


void recursive_binary_search(int* i_storage, int i_num_item)
{
     bool b_check = check_item_order(i_storage);

      printf("Your Storage Items are: nt");

     int i_loop_count;
     for(i_loop_count = 0; i_loop_count < i_num_item;
++i_loop_count)
     {
          printf("%d, ", i_storage[i_loop_count]);
     }

      printf("nnn");

      int i_srch_item;
      printf("Please Enter the item (number) to search: ");
      scanf("%d", &i_srch_item);

      printf("nnn");
      int i_low = 0;
      int i_hi = i_num_item;
      int i_mid = (i_low + i_hi)/2;

     if(b_check)
          recursive_bin_search(i_storage, i_srch_item, i_low,
i_mid, i_hi);
     else
          printf("nItems are not in Order.n");
}

void binary_search(void)
{
     printf("WAP for Binary - Search.");
printf("nLimitation: nt-> Items are restrickted with integer
number.nt-> Starting index of storage is 0.");
     printf("nnn");

      int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

     recursive_binary_search(i_storage,
(sizeof(i_storage)/sizeof(int)));
}



M.Tech – CSE – Weekend (2012 – 2015)                          Page 15
Programming in C of College days

int main()
{
     binary_search();

      printf("nnn");
      getch();
      return 0;
}


    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~



      b. Non recursion

#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include   <stdbool.h>
#include   <math.h>

bool check_item_order(int* i_storage)
{
     return true;
}

void iterative_binary_search(int* i_storage, int i_num_item)
{
     bool b_check = check_item_order(i_storage);

      printf("Your Storage Items are: nt");

     int i_loop_count;
     for(i_loop_count = 0; i_loop_count < i_num_item;
++i_loop_count)
     {
          printf("%d, ", i_storage[i_loop_count]);
     }

      printf("nnn");

      int i_srch_item;
      printf("Please Enter the item (number) to search: ");
      scanf("%d", &i_srch_item);

      printf("nnn");

M.Tech – CSE – Weekend (2012 – 2015)                          Page 16
Programming in C of College days

      printf("Result: ");

     int i_low = 0;
     int i_hi = i_num_item;
     int i_mid = (i_low + i_hi)/2;
     if (b_check)
     {
          while(i_low < i_hi)
          {
               if(i_storage[i_mid] == i_srch_item)
               {
printf("Item (%d) is found at position: %d", i_srch_item,
i_mid);
                    break;
               }
               else if(i_storage[i_mid] > i_srch_item)
               {
                    i_hi = i_mid;
                    i_mid = (i_low + i_hi)/2;
               }
               else if(i_storage[i_mid] < i_srch_item)
               {
                    i_low = i_mid;
                    i_mid = (i_low + i_hi)/2;
               }
          }
     }
}

void binary_search(void)
{
     printf("WAP for Binary - Search.");
printf("nLimitation: nt-> Items are restrickted with integer
number.nt-> Starting index of storage is 0.");
     printf("nnn");

      int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

     iterative_binary_search(i_storage,
(sizeof(i_storage)/sizeof(int)));
}




M.Tech – CSE – Weekend (2012 – 2015)                        Page 17
Programming in C of College days

int main()
{
     binary_search();

     printf("nnn");
      getch();
      return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 18
Programming in C of College days

   6. WAP for Tower of Hanoi. Also draw the recursive tree for n=4 disks.

Program:

#include   <stdio.h>
#include   <stdlib.h>
#include   <stdbool.h>
#include   <string.h>
#include   <math.h>

void generate_moves_toh(int i_disk_num, char from_peg, char
to_peg, char aux_peg)
{
     /* If only 1 disk, make the move and return */
     if(i_disk_num == 1)
     {
          printf("nMove disk 1 from peg %c to peg %c",
from_peg, to_peg);
          return;
     }

     /* Move top n-1 disks from A to B, using C as auxiliary */
     generate_moves_toh(i_disk_num-1, from_peg, aux_peg,
to_peg);

     printf("nMove disk %d from peg %c to peg %c", i_disk_num,
from_peg, to_peg);

     /* Move n-1 disks from B to C using A as auxiliary */
     generate_moves_toh(i_disk_num-1, aux_peg, to_peg,
from_peg);
}

void gen_tow_of_honoi()
{
     printf("WAP to display the steps required in solving 'Tower
of Hanoi' for 'n' number of disks.");
      printf("nLimitation: nt-> Disks are represented as
integral numbers in assending order.");
      printf("nnn");

       int i_num_disk;
       printf("Enter the number of disks : ");
       scanf("%d",&i_num_disk);
       printf("The Tower of Hanoi involves the moves :nn");

       generate_moves_toh(i_num_disk, 'A', 'C', 'B');

M.Tech – CSE – Weekend (2012 – 2015)                                Page 19
Programming in C of College days

}

int main()
{
     gen_tow_of_honoi();

      printf("nnn");
      getch();
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 20
Programming in C of College days

   7. WAP to generate Pascal triangle viz. if line =5
                1
                1     1
                1     2     1
                1     3     3     1
                1     4     6     4      1

Program:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    printf("Pascal's Seriesn");

     printf("nnn");

     int i_num_row;
     printf("Enter number of rows: ");
     scanf("%d", &i_num_row);

     int arr[6], arr_temp[6];

     static bool flag = true;

    int i_row_count, i_col_count, i_count, i_counter;
    for (i_row_count = 1; i_row_count <= i_num_row;
++i_row_count)
    {
        for (i_col_count = 1; i_col_count <= i_num_row;
++i_col_count)
        {
            for (i_count = 0; i_count <= i_row_count; ++i_count)
            {
                if (flag)
                {
                   arr[i_count] = i_count;
                   if (arr[i_count] != 0)
                      printf("t%d", arr[i_count]);
                }
                else if(i_count > 0)
                {
                    arr_temp[i_count] = arr[i_count-1] +
arr[i_count];
                    printf("t%d", arr_temp[i_count]);
                }

M.Tech – CSE – Weekend (2012 – 2015)                       Page 21
Programming in C of College days

                     else
                     {
                            arr_temp[i_count] = i_count;
                 }
             }
for (i_counter = i_count; i_counter <= (i_num_row+1);
++i_counter)
             {
                 if (flag)
                 {
                    arr[i_counter] = 0;
                 }
                 else if(i_counter > 0)
                 {
                     arr_temp[i_counter] = arr[i_counter-1] +
arr[i_counter];
                 }
             }
             flag = false;
             break;
        }
        if (i_row_count > 1)
           for (i_count = 1; i_count <= i_num_row; ++i_count)
           {
                arr[i_count] = arr_temp[i_count];
           }
        printf("n");
    }

     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                       Page 22
Programming in C of College days

    8. Write following programs by using passing array to function :
         a) Bubble sort
         b) Insertion sort
         c) Selection sort

Program:

      a) Bubble sort

#include <stdio.h>

int* bubble_sort(int i_store[], int i_size);
int* get_elem(int i_size);
void disp_elem(int i_store[], int i_size);

int main()
{
    printf("WAP of Bubble sort by using array.");
    printf("nnn");

     int i_store_size;
     printf("Enter the total number of items to store: ");
     scanf("%d", &i_store_size);

     int* ip_store = get_elem(i_store_size);
     ip_store = bubble_sort(ip_store, i_store_size);
     disp_elem(ip_store, i_store_size);

     printf("nnn");
     system("pause");
     return 0;
}

int* get_elem(int i_size)
{
     int* ip_store = malloc(sizeof(int) * i_size);
     int i_count = 0;
     while (i_size)
     {
           printf("nEnter item for index - %d : ", i_count);
           scanf("%d", (ip_store+i_count));
           i_count++;
           i_size--;
     }

      return ip_store;

M.Tech – CSE – Weekend (2012 – 2015)                                   Page 23
Programming in C of College days

}

int* bubble_sort(int i_store[], int i_size)
{
     int i_temp;
     int i_count_0, i_count_1;
     for (i_count_0 = (i_size - 1); i_count_0 > 0; --i_count_0)
     {
         for (i_count_1 = 1; i_count_1 <= i_count_0;
++i_count_1)
         {
             if (i_store[i_count_1 - 1] > i_store[i_count_1])
             {
                 i_temp = i_store[i_count_1 - 1];
                 i_store[i_count_1 - 1] = i_store[i_count_1];
                 i_store[i_count_1] = i_temp;
             }
         }
     }

      return i_store;
}

void disp_elem(int i_store[], int i_size)
{
     printf("nDisplaying Elements of store: n");
     int i_ndx = 0;
     while (i_size)
     {
           printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]);
           i_ndx++;
           i_size--;
     }
}




    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~


M.Tech – CSE – Weekend (2012 – 2015)                       Page 24
Programming in C of College days

      Insertion sort

#include <stdio.h>

int* insertion_sort(int i_store[], int i_size);
int* get_elem(int i_size);
void disp_elem(int i_store[], int i_size);

int main()
{
    printf("WAP of Insertion sort by using array.");
    printf("nnn");

     int i_store_size;
     printf("Enter the total number of items to store: ");
     scanf("%d", &i_store_size);

     int* ip_store = get_elem(i_store_size);
     ip_store = insertion_sort(ip_store, i_store_size);
     disp_elem(ip_store, i_store_size);

     printf("nnn");
     system("pause");
     return 0;
}

int* get_elem(int i_size)
{
     int* ip_store = malloc(sizeof(int) * i_size);
     int i_count = 0;
     while (i_size)
     {
           printf("nEnter item for index - %d : ", i_count);
           scanf("%d", (ip_store+i_count));
           i_count++;
           i_size--;
     }

      return ip_store;
}

int* insertion_sort(int i_store[], int i_size)
{
     int i_temp;
     int i_count_0, i_count_1, i_count_2;
     for (i_count_0 = 1; i_count_0 <= (i_size-1); ++i_count_0)
     {

M.Tech – CSE – Weekend (2012 – 2015)                         Page 25
Programming in C of College days

           for (i_count_1 = 0; i_count_1 < i_count_0; ++i_count_1)
            {
                 if (i_store[i_count_1] > i_store[i_count_0])
                 {
                      i_temp = i_store[i_count_1] ;
                      i_store[i_count_1] = i_store[i_count_0] ;

                    for (i_count_2 = i_count_0; i_count_2 >
i_count_1; i_count_2--)
                         i_store[i_count_2] = i_store[i_count_2
- 1] ;

                          i_store[i_count_2 + 1] = i_temp ;
                   }
             }
      }

      return i_store;
}

void disp_elem(int i_store[], int i_size)
{
     printf("nDisplaying Elements of store: n");
     int i_ndx = 0;
     while (i_size)
     {
           printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]);
           i_ndx++;
           i_size--;
     }
}




    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~


M.Tech – CSE – Weekend (2012 – 2015)                          Page 26
Programming in C of College days

      Selection sort

#include <stdio.h>

int* selection_sort(int i_store[], int i_size);
int* get_elem(int i_size);
void disp_elem(int i_store[], int i_size);

int main()
{
    printf("WAP of Selection sort by using array.");
    printf("nnn");

     int i_store_size;
     printf("Enter the total number of items to store: ");
     scanf("%d", &i_store_size);

     int* ip_store = get_elem(i_store_size);
     ip_store = selection_sort(ip_store, i_store_size);
     disp_elem(ip_store, i_store_size);

     printf("nnn");
     system("pause");
     return 0;
}

int* get_elem(int i_size)
{
     int* ip_store = malloc(sizeof(int) * i_size);
     int i_count = 0;
     while (i_size)
     {
           printf("nEnter item for index - %d : ", i_count);
           scanf("%d", (ip_store+i_count));
           i_count++;
           i_size--;
     }

      return ip_store;
}

int* selection_sort(int i_store[], int i_size)
{
     int i_temp;
     int i_count_0, i_count_1;
     for (i_count_0 = 0; i_count_0 < (i_size-1); ++i_count_0)
     {

M.Tech – CSE – Weekend (2012 – 2015)                         Page 27
Programming in C of College days

         for (i_count_1 = (i_count_0+1); i_count_1 < i_size;
++i_count_1)
         {
             if (i_store[i_count_0] > i_store[i_count_1])
             {
                i_temp = i_store[i_count_0];
                i_store[i_count_0] = i_store[i_count_1];
                i_store[i_count_1] = i_temp;
             }
         }
     }

      return i_store;
}

void disp_elem(int i_store[], int i_size)
{
     printf("nDisplaying Elements of store: n");
     int i_ndx = 0;
     while (i_size)
     {
           printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]);
           i_ndx++;
           i_size--;
     }
}




M.Tech – CSE – Weekend (2012 – 2015)                       Page 28
Programming in C of College days

    9. WAP for matrix multiplication of given order.

Program:

#include <stdio.h>
#include <stdbool.h>

void get_matrices(void);
void mult_matrices(void);
void disp_matrix(void);

int **mat_1;
int **mat_2;
int **res_mat;

int i_m1_row, i_m1_col;
int i_m2_row, i_m2_col;

int main()
{
    printf("WAP for matrix multiplication of given order.n");
    printf("nnn");

     get_matrices();
     mult_matrices();
     disp_matrix();

     system("pause");
     return 0;
}

void get_matrices()
{
     printf("Enter number of rows for Matrix-1: ");
     scanf("%d", &i_m1_row);
     printf("Enter number of columbs for Matrix-1: ");
     scanf("%d", &i_m1_col);

      mat_1 = malloc(sizeof(int) * i_m1_row);

     int i_row_count, i_col_count;
     printf("Enter elements for Matrix-1:n");
     for (i_row_count = 0; i_row_count < i_m1_row;
++i_row_count)
     {
         *(mat_1+i_row_count) = malloc(sizeof(int) * i_m1_col);


M.Tech – CSE – Weekend (2012 – 2015)                       Page 29
Programming in C of College days

         for (i_col_count = 0; i_col_count < i_m1_col;
++i_col_count)
         {
             printf("Element[%d][%d]: ", i_row_count,
i_col_count);
             scanf("%d",&(*(*(mat_1+i_row_count)+i_col_count)));
             *(mat_1+i_row_count)+i_col_count;
         }
     }

      printf("nnn");

      printf("Enter number of rows for Matrix-2: ");
      scanf("%d", &i_m2_row);
      printf("Enter number of columbs for Matrix-2: ");
      scanf("%d", &i_m2_col);

      mat_2 = malloc(sizeof(int) * i_m2_row);

     printf("Enter elements for Matrix-2:n");
     for (i_row_count = 0; i_row_count < i_m2_row;
++i_row_count)
     {
         *(mat_2+i_row_count) = malloc(sizeof(int) * i_m2_col);
         for (i_col_count = 0; i_col_count < i_m2_col;
++i_col_count)
         {
             printf("Element[%d][%d]: ", i_row_count,
i_col_count);
             scanf("%d",&(*(*(mat_2+i_row_count)+i_col_count)));
             *(mat_2+i_row_count)+i_col_count;
         }
     }

      printf("nnn");

     for (i_row_count = 0; i_row_count < i_m1_row;
++i_row_count)
     {
         for (i_col_count = 0; i_col_count < i_m1_col;
++i_col_count)
         {
             printf("Element[%d][%d] - Mat-1: %dn",
i_row_count, i_col_count, mat_1[i_row_count][i_col_count]);
         }
     }
     printf("nnn");

M.Tech – CSE – Weekend (2012 – 2015)                          Page 30
Programming in C of College days

     for (i_row_count = 0; i_row_count < i_m1_row;
++i_row_count)
     {
         for (i_col_count = 0; i_col_count < i_m1_col;
++i_col_count)
         {
             printf("Element[%d][%d] - Mat-2: %dn",
i_row_count, i_col_count, mat_2[i_row_count][i_col_count]);
         }
     }
     printf("n");
}

void mult_matrices(void)
{
     printf("nnn");

      int i_row_count, i_col_count, i_count;
      res_mat = malloc(sizeof(int) * i_m1_row);

     printf("Resultant Matrix: Row- %dt Column- %dn",
i_m1_row, i_m2_col);
     for (i_row_count = 0; i_row_count < i_m1_row;
++i_row_count)
     {
         *(res_mat+i_row_count) = malloc(sizeof(int) *
i_m2_col);
         for (i_col_count = 0; i_col_count < i_m2_col;
++i_col_count)
         {
             *(*(res_mat+i_row_count)+i_col_count) = 0;
             for (i_count = 0; i_count < i_m1_row; ++i_count)
             {
                 *(*(res_mat+i_row_count)+i_col_count) +=
(*(*(mat_1 + i_row_count)+i_count)) * (*(*(mat_2 +
i_count)+i_col_count));
             }
         }
     }

      printf("nnn");
}




M.Tech – CSE – Weekend (2012 – 2015)                          Page 31
Programming in C of College days

void disp_matrix(void)
{
     int i_row_count, i_col_count;
     for (i_row_count = 0; i_row_count < i_m1_row;
++i_row_count)
     {
         for (i_col_count = 0; i_col_count < i_m2_col;
++i_col_count)
         {
             printf("Resultant[%d][%d] - Res-2: %dn",
i_row_count, i_col_count,
*(*(res_mat+i_row_count)+i_col_count));
         }
     }

      printf("nnn");
}




M.Tech – CSE – Weekend (2012 – 2015)                     Page 32
Programming in C of College days

    10.        WAP without using string‟s function for :
          a.   Copy of one string in another
          b.   Concatenation of string
          c.   Comparison of strings
          d.   Reverse the string

Program:

      a. Copy of one string in another

#include <stdio.h>

#define STR_SIZE 1024

char* get_string(void);
char* str_copy(char* gvn_str);

int main()
{
    printf("WAP without using string's function to Copy one
string into another.");
    printf("nnn");

     char* temp = malloc(sizeof(char) * STR_SIZE);
     temp = get_string();

     printf("nnn");

     printf("Copied String: %s", str_copy(temp));

     printf("nnn");
     system("pause");
     return 0;
}

char* get_string(void)
{
     char* chp_var1;
     char ch_temp;
     int i_num = 0;

      chp_var1 = malloc(sizeof(char) * STR_SIZE);

     printf("Please Enter an string to copy ('n' is
terminating character): ");
     while ((ch_temp = getchar()) != 'n')
     {

M.Tech – CSE – Weekend (2012 – 2015)                          Page 33
Programming in C of College days

             *(chp_var1 + i_num) = ch_temp;
             i_num++;
      }

      *(chp_var1 + i_num) = '0';

      return chp_var1;
}

char* str_copy(char* gvn_str)
{
      char* chp_var;
      int i_num = 0;

       chp_var = malloc(sizeof(char) * STR_SIZE);

       while (*(gvn_str + i_num) != '0')
       {
           *(chp_var + i_num) = *(gvn_str + i_num);
           i_num++;
       }

       *(chp_var + i_num) = '0';

       return chp_var;
}


    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

      b. Concatenation of string

#include <stdio.h>

#define STR_SIZE 1024

char* concat_str(char* chp_var1, char* chp_var2);
unsigned int get_str_len(char* chp_var);

int main()
{
    printf("WAP without using string's function for
Concatenating of two strings. ");
    char* chp_var1;
    char* chp_var2;
    char* chp_res;


M.Tech – CSE – Weekend (2012 – 2015)                    Page 34
Programming in C of College days

     printf("nnn");

     chp_var1 = malloc(sizeof(char) * STR_SIZE);
     chp_var2 = malloc(sizeof(char) * STR_SIZE);

     printf("Please Enter an string: ");
     scanf("%s", chp_var1);
     printf("nn");
     printf("Please Enter another string to concatenate: ");
     scanf("%s", chp_var2);

     chp_res = concat_str(chp_var1, chp_var2);
     printf("nResultant String (after Copying): %s", chp_res);

     printf("n");
     system("pause");
     return 0;
}

char* concat_str(char* chp_var1, char* chp_var2)
{
      size_t size = get_str_len(chp_var1) +
get_str_len(chp_var2);

       chp_var1 = (char*)realloc(chp_var1, (size+1));

       unsigned int ui_count = 0;
       while (*(chp_var1+ui_count) != '0')
             ui_count++;

       while (*chp_var2 != '0')
       {
             *(chp_var1+ui_count) = *chp_var2;

               ui_count++;
               chp_var2++;
       }
       *(chp_var1+ui_count) = '0';

       return chp_var1;
}




unsigned int get_str_len(char* chp_var)
{

M.Tech – CSE – Weekend (2012 – 2015)                           Page 35
Programming in C of College days

           unsigned int ui_count = 0;
           while (*chp_var != '0')
           {
                 ui_count++;
                 chp_var++;
           }

           return ui_count;
}

    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

      c. Comparison of strings

#include <stdio.h>
#include <stdbool.h>

#define STR_SIZE 1024

bool comp_str(char* chp_var1, char* chp_var2);

int main()
{
    printf("WAP without using string's function to Comparison of
strings.");
    char* chp_var1;
    char* chp_var2;
    bool b_res;

     printf("nnn");

     chp_var1 = malloc(sizeof(char) * STR_SIZE);
     chp_var2 = malloc(sizeof(char) * STR_SIZE);

     printf("Please Enter an string: ");
     scanf("%s", chp_var1);
     printf("nn");
     printf("Please Enter another string to compare: ");
     scanf("%s", chp_var2);

     b_res = comp_str(chp_var1, chp_var2);

     if (b_res)
        printf("nStrings are same.");
     else
          printf("nStrings are not same.");


M.Tech – CSE – Weekend (2012 – 2015)                       Page 36
Programming in C of College days

     printf("n");
     system("pause");
     return 0;
}

bool comp_str(char* chp_var1, char* chp_var2)
{
      unsigned int ui_count = 0;
      while (*(chp_var1+ui_count)== *(chp_var2+ui_count))
      {
            if (*(chp_var1+ui_count) == '0')
               return true;
            ui_count++;
      }

       return false;
}


    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~

      d. Reverse the string

#include <stdio.h>
#include <stdbool.h>

#define STR_SIZE 1024

char* comp_str(char* chp_var1);
unsigned int get_str_len(char* chp_var);

int main()
{
    printf("WAP without using string's function to Reverse the
string.");
    char* chp_var1;
    char* chp_res;

     printf("nnn");

     chp_var1 = malloc(sizeof(char) * STR_SIZE);

     printf("Please Enter an string: ");
     scanf("%s", chp_var1);

     chp_res = comp_str(chp_var1);


M.Tech – CSE – Weekend (2012 – 2015)                        Page 37
Programming in C of College days

     printf("nResultant Strings (after reverse): %s", chp_res);

     printf("n");
     system("pause");
     return 0;
}

char* comp_str(char* chp_var1)
{
      size_t size = get_str_len(chp_var1);

       char* chp_str = malloc(size+1);

       unsigned int ui_rev_idx = (size - 1);
       unsigned int ui_count = 0;
       while (*(chp_var1+ui_count) != '0')
       {
             *(chp_str+ui_rev_idx) = *(chp_var1+ui_count);
             ui_rev_idx--;
             ui_count++;
       }
       *(chp_str+size) = '0';

       return chp_str;
}

unsigned int get_str_len(char* chp_var)
{
         unsigned int ui_count = 0;
         while (*chp_var != '0')
         {
               ui_count++;
               chp_var++;
         }

           return ui_count;
}




M.Tech – CSE – Weekend (2012 – 2015)                         Page 38
Programming in C of College days

    11. WAP to check that given string is palindrome or not?

Program:

#include <stdio.h>
#include <stdbool.h>

bool check(char* usr_str, char* chk_str);

int main()
{
    printf("WAP to check given string is Palindrome.n");
    printf("nnn");

     bool b_check;
     char* chp_var = malloc(sizeof(char) * 1024);
     char* chk_var = malloc(sizeof(char) * 1024);
     char* temp = chp_var;
     unsigned int ui_no_char = 0;

     printf("Enter string to check palindrome: ");
     while ((*chp_var = getchar())!='n')
     {
           *chk_var = *chp_var;
           chk_var++;
           chp_var++;
           ui_no_char++;
     }
     *chp_var = '0';
     chp_var = temp;
     chk_var--;

     b_check = check(chp_var, chk_var);
     if (b_check)
        printf("nResult: %s, is palindrome.n", chp_var);
     else
          printf("nResult: %s, isn't palindrome.n", chp_var);

     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                           Page 39
Programming in C of College days

bool check(char* usr_str, char* chk_str)
{
     unsigned int ui_count, ui_chk;
     ui_chk = ui_count = 0;
     while (*usr_str != '0')
     {
           ui_count++;
           if (*usr_str != *chk_str)
           {
              return false;
           }
           else
           {
              ui_chk++;
           }
           usr_str++;
           chk_str--;
     }

      if (ui_count == ui_chk)
         return true;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 40
Programming in C of College days

   12. WAP to store the information about book‟s Title, Author, Page, &
       Price. Display the information by using pointer & structure with
       following restriction :
          a. Display elements by passing one element at time.
          b. By passing all elements at time.

Program:

#include   <stdio.h>
#include   <stdbool.h>
#include   <ctype.h>
#include   <string.h>

#define HEAD_SIZE 9
#define DIGIT_SIZE 9
#define STRING_LEN 30

void get_choice(void);
bool store_info(void);
bool disp_one_elem(void);
bool disp_all_elem(void);
char* int_to_alpha(int i_num);

struct bk_detail
{
       char* chp_stitle;
       char* chp_sname;
       int i_spage;
       int i_sprice;
};

int main()
{
    printf("WAP to store the information about book's Title,
Author, Page, & Price. nDisplay the information by using
pointer & structure with following restriction :");
    printf("nt-> Display elements by passing one element at
time. nt-> By passing all elements at time.");
    printf("nnn");

     char ch_temp;
     FILE* fp;
     fp = fopen("book_idx_mgr.txt", "r+");
     ch_temp = fgetc(fp);
     fclose(fp);

     if (ch_temp == -1)

M.Tech – CSE – Weekend (2012 – 2015)                                 Page 41
Programming in C of College days

     {
             printf("No record inserted.n");
             get_choice();
     }
     else
     {
          printf("nBook Index File is ready for use.n");
          get_choice();
     }

     fclose(fp);

     printf("nnn");
     system("pause");
     return 0;
}

char* int_to_alpha(int i_num)
{
      int i_count, i_sign, i_rev_count;
      char* chp_num = malloc(sizeof(char) * DIGIT_SIZE);
      char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE);

         i_sign = 0;
         i_count = i_rev_count = 0;
         if (i_num == 0)
         {
            *(chp_num + i_count) = '0';
            i_count++;
         }
         else if (i_num < 0)
         {
            i_sign = -1;
            i_num *= -1;
         }
         while (i_num > 0)
         {
               *(chp_num + i_count) = i_num % 10 + '0';
               i_count++;

               i_num /= 10;
         }
         if (i_sign < 0)
         {
            *(chp_num + i_count) = '-';
            i_count++;
         }

M.Tech – CSE – Weekend (2012 – 2015)                         Page 42
Programming in C of College days

       *(chp_num + i_count) = '0';

       i_count = 0;
       i_rev_count = strlen(chp_num)-1;

       while (*(chp_num + i_count) != '0')
       {
             *(chp_rev_num + i_count) = *(chp_num + i_rev_count);
             i_count++;
             i_rev_count--;
       }

       *(chp_rev_num + i_count) = '0';

       return chp_rev_num;
}

void get_choice(void)
{
     bool (*pfunc_seq[3])();
     pfunc_seq[0] = store_info;
     pfunc_seq[1] = disp_one_elem;
     pfunc_seq[2] = disp_all_elem;

     printf("nYour choices for operation:n");
     printf("nt a. To insert record (press) - 1n");
     printf("nt b. To display complete info of the nt book
(your selected name of book) (press) - 2n");
     printf("nt a. To display all records (press) - 3n");

      printf("nPlease Enter your choice: ");
      char ch_choice;
      if ((ch_choice = getchar()) == '1')
      {
         (*pfunc_seq[0])();
      }
      else if (ch_choice == '2')
      {
            (*pfunc_seq[1])();
      }
      else if (ch_choice == '3')
      {
            (*pfunc_seq[2])();
      }
      else
           printf("nChoice is invalid.n");
}

M.Tech – CSE – Weekend (2012 – 2015)                        Page 43
Programming in C of College days

bool store_info()
{
     FILE* fp;
     fp = fopen("book_idx_mgr.txt", "a+");
     printf("nTo store - records.n");

      char* chp_bk_title = malloc(sizeof(char) * STRING_LEN);
      char* chp_bk_author = malloc(sizeof(char) * STRING_LEN);
      char* chp_bk_page = malloc(sizeof(char) * STRING_LEN);
      char* chp_bk_price = malloc(sizeof(char) * STRING_LEN);
      int i_temp;

      printf("Please Enter: n");
      printf("tTitle: ");
      scanf("%s", chp_bk_title);
      fputs("Title: ", fp);
      fputs(chp_bk_title, fp);

      printf("tAuthor: ");
      scanf("%s", chp_bk_author);
      fputs("nAuthor: ", fp);
      fputs(chp_bk_author, fp);

      printf("tPage: ");
      scanf("%d", &i_temp);
      chp_bk_page = int_to_alpha(i_temp);
      fputs("nPage: ", fp);
      fputs(chp_bk_page, fp);

      printf("tPrice(only Integer): ");
      scanf("%d", &i_temp);
      chp_bk_price = int_to_alpha(i_temp);
      fputs("nPrice: ", fp);
      fputs(chp_bk_price, fp);

     char* chp_item_separator = "n-----------------------------
---------------n";
     fputs(chp_item_separator, fp);

      fclose(fp);
      return true;
}

bool disp_one_elem()
{


M.Tech – CSE – Weekend (2012 – 2015)                        Page 44
Programming in C of College days

     printf("nTo display the details of record as per your
choice.n");

      struct bk_detail s_bk_detail;
      s_bk_detail.chp_sname = malloc(sizeof(char) * STRING_LEN);
      s_bk_detail.chp_stitle = malloc(sizeof(char) * STRING_LEN);
      s_bk_detail.i_spage = 0;
      s_bk_detail.i_sprice = 0;

      char* chp_usr_book = malloc(sizeof(char) * STRING_LEN);
      printf("Please Enter the title of the book: ");
      scanf("%s", chp_usr_book);

      FILE* fp;
      fp = fopen("book_idx_mgr.txt", "r+");

     char* chp_str = malloc(sizeof(char) * HEAD_SIZE);
     char* chp_prev_str = malloc(sizeof(char) * HEAD_SIZE);
     char ch_temp;
     int i_ch_count = 0;
     bool b_store = false;
     bool b_assign = false;
     while ((ch_temp = fgetc(fp))!= EOF)
     {
           if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp
== '_'))
           {
              *(chp_str + i_ch_count) = ch_temp;
              i_ch_count++;
           }
           else if ((ch_temp == ':')||(ch_temp == 'n'))
           {
                *(chp_str + i_ch_count) = '0';
                i_ch_count = 0;

                     if (ch_temp == ':')
                     {
                        b_store = false;
                     }
                     else if (ch_temp == 'n')
                          b_store = true;

                     if (b_store)
                     {
                        if (strcmp(chp_str, chp_usr_book) == 0)
                        {
                           b_assign = true;

M.Tech – CSE – Weekend (2012 – 2015)                              Page 45
Programming in C of College days

                         }

                   if (b_assign)
                   {
                        if (strcmp(chp_prev_str, "Title") == 0)
                        {
                           s_bk_detail.chp_stitle =
strcpy(s_bk_detail.chp_stitle, chp_str);
                           printf("nTitle - %s",
s_bk_detail.chp_stitle);
                        }
                        else if (strcmp(chp_prev_str, "Author")
== 0)
                        {
                             s_bk_detail.chp_sname =
strcpy(s_bk_detail.chp_sname, chp_str);
                             printf("nAuthor - %s",
s_bk_detail.chp_sname);
                        }
                        else if (strcmp(chp_prev_str, "Page") ==
0)
                        {
                             int i_temp = atoi(chp_str);
                             s_bk_detail.i_spage = i_temp;
                             printf("nPage - %d",
s_bk_detail.i_spage);
                        }
                        else if (strcmp(chp_prev_str, "Price") ==
0)
                        {
                             int i_temp = atoi(chp_str);
                             s_bk_detail.i_sprice = i_temp;
                             printf("nPrice - %d",
s_bk_detail.i_sprice);

                                       b_assign = false;
                               }
                         }
                     }
                     else
                     {
                            chp_prev_str = strcpy(chp_prev_str,
chp_str);
                     }
              }
      }


M.Tech – CSE – Weekend (2012 – 2015)                              Page 46
Programming in C of College days

      fclose(fp);
      return true;
}

bool disp_all_elem()
{
     printf("nTo display the details of record.n");

      FILE* fp;
      fp = fopen("book_idx_mgr.txt", "r+");

      char* chp_str = malloc(sizeof(char) * HEAD_SIZE);
      char ch_temp;
      int i_ch_count = 0;


     while ((ch_temp = fgetc(fp))!= EOF)
     {
           if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp
== ':')||(ch_temp == ' ')||(ch_temp == '_'))
           {
              *(chp_str + i_ch_count) = ch_temp;
              i_ch_count++;
           }
           else if (ch_temp == 'n')
           {
                *(chp_str + i_ch_count) = '0';
                i_ch_count = 0;

                     fputs(chp_str, stdout);
                     fputs("n", stdout);
              }
      }

      fclose(fp);
      return true;
}




M.Tech – CSE – Weekend (2012 – 2015)                       Page 47
Programming in C of College days

    13. WAP to display the sum of entered number by writing on & reading
        from file.

Program:

#include   <stdio.h>
#include   <ctype.h>
#include   <stdlib.h>
#include   <stdbool.h>

#define DIGIT_SIZE 9

void write_file(FILE* fp);
void read_file(FILE* fp);

int main()
{
    printf("WAP to write integer numbers to files, then separate
even & odd numbers among them & store & display them in separate
files.");
    printf("nnn");

     FILE* fp;


    printf("Please Note: nt->Comma(','), nt->Blank-Space ('
'), nt->Tab ('t') nt->and Enter ('n') are separators");
    printf("nt->whereas Dot ('.') is terminator, nLimitation:
Program will not accept any number more than 9 digits.");
    printf("nPlease use any of spearator before terminator.");

     printf("nnn");
     printf("Please Enter numbers: ");

     write_file(fp);
     printf("nnn");
     read_file(fp);

     system("pause");
     return 0;
}

void write_file(FILE* fp)
{
     fp = fopen("Integer_data.txt", "w+");

      int* ip_usr_data;

M.Tech – CSE – Weekend (2012 – 2015)                                Page 48
Programming in C of College days

      ip_usr_data = malloc(sizeof(int));

     char ch_temp_data;
     char* ch_num = malloc(sizeof(char) * DIGIT_SIZE);
     int i_count = 0;
     bool b_check = false;
     while ((ch_temp_data = getchar()) != '.')
     {
          if (((ch_temp_data == ',')||(ch_temp_data == '
')||(ch_temp_data == 't')||(ch_temp_data == 'n'))&&(b_check))
          {
             b_check = false;
             *(ch_num + i_count) = '0';
             i_count = 0;

                 *ip_usr_data = atoi(ch_num);
                 if (*ip_usr_data)
                 {
                    fputs(ch_num, fp);
                    fputs(", ", fp);
                 }
             }
             else if (isdigit(ch_temp_data))
             {
                  b_check = true;
                  *(ch_num + i_count) = ch_temp_data;
                  i_count++;
             }
             else if (isalpha(ch_temp_data))
             {
                 printf("Invalid input.");
             }
      }

      fclose(fp);
}

void read_file(FILE* fp)
{
     fp = fopen("Integer_data.txt", "r+");

      FILE* fp_even;
      fp_even = fopen("Even_data.txt", "w+");
      FILE* fp_odd;
      fp_odd = fopen("Odd_data.txt", "w+");

      int* ip_usr_data;

M.Tech – CSE – Weekend (2012 – 2015)                       Page 49
Programming in C of College days

      ip_usr_data = malloc(sizeof(int));

     char* ch_num = malloc(sizeof(char) * DIGIT_SIZE);
     int i_count = 0;
     bool b_check = false;
     char ch_temp_data;
     while ((ch_temp_data = fgetc(fp)) != EOF)
     {
           if (((ch_temp_data == ',')||(ch_temp_data == '
')||(ch_temp_data == 't')||(ch_temp_data == 'n'))&&(b_check))
           {
              b_check = false;
              *(ch_num + i_count) = '0';
              i_count = 0;

                  *ip_usr_data = atoi(ch_num);
                  if (*ip_usr_data)
                  {
                     if ((*ip_usr_data % 2) == 0)
                     {
                        fputs(ch_num, fp_even);
                        fputs(", ", fp_even);
                     }
                     else
                     {
                        fputs(ch_num, fp_odd);
                        fputs(", ", fp_odd);
                     }

                 }
              }
              else if (isdigit(ch_temp_data))
              {
                   b_check = true;
                   *(ch_num + i_count) = ch_temp_data;
                   i_count++;
              }
      }

      if (fclose(fp_even) == 0)
         if (fclose(fp_odd) == 0)
            if (fclose(fp) == 0)
               printf("Normal termination.n");
            else
                 printf("Abnormal termination.n");
         else
                 printf("Abnormal termination.n");

M.Tech – CSE – Weekend (2012 – 2015)                       Page 50
Programming in C of College days

      else
                   printf("Abnormal termination.n");

}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 51
Programming in C of College days

    14. Write integer numbers to files, then separate even & odd numbers
        among them & store & display them in separate files.

Program:

#include   <stdio.h>
#include   <ctype.h>
#include   <stdlib.h>
#include   <stdbool.h>

#define DIGIT_SIZE 9

char* int_to_alpha(int i_num);
void write_file(FILE* fp, int i_addend, int i_augend);
void read_file(FILE* fp);

int main()
{
    printf("WAP to display the sum of entered number by writing
on & reading from file.");
    printf("nnn");

    printf("Limitation: Program will only accept intergral
number of size 4 bytes.");

     printf("nnn");

     int i_addend, i_augend;
     printf("Please Enter numbers: ");
     scanf("%d", &i_addend);

     printf("Please Enter numbers: ");
     scanf("%d", &i_augend);

     FILE* fp;
     write_file(fp, i_addend, i_augend);
     read_file(fp);

     system("pause");
     return 0;
}

char* int_to_alpha(int i_num)
{
      int i_count, i_sign, i_rev_count;
      char* chp_num = malloc(sizeof(char) * DIGIT_SIZE);
      char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE);

M.Tech – CSE – Weekend (2012 – 2015)                                 Page 52
Programming in C of College days

       i_sign = 0;
       i_count = i_rev_count = 0;
       if (i_num == 0)
       {
          *(chp_num + i_count) = '0';
          i_count++;
       }
       else if (i_num < 0)
       {
          i_sign = -1;
          i_num *= -1;
       }
       while (i_num > 0)
       {
             *(chp_num + i_count) = i_num % 10 + '0';
             i_count++;

             i_num /= 10;
       }
       if (i_sign < 0)
       {
          *(chp_num + i_count) = '-';
          i_count++;
       }
       *(chp_num + i_count) = '0';

       i_count = 0;
       i_rev_count = strlen(chp_num)-1;

       while (*(chp_num + i_count) != '0')
       {
             *(chp_rev_num + i_count) = *(chp_num + i_rev_count);
             i_count++;
             i_rev_count--;
       }

       *(chp_rev_num + i_count) = '0';

       return chp_rev_num;
}

void write_file(FILE* fp, int i_addend, int i_augend)
{
     fp = fopen("Summation.txt", "w+");

      char* chp_addend = int_to_alpha(i_addend);

M.Tech – CSE – Weekend (2012 – 2015)                        Page 53
Programming in C of College days

      char* chp_augend = int_to_alpha(i_augend);

      fputs(chp_addend, fp);
      fputs(" + ", fp);
      fputs(chp_augend, fp);
      fputs(" = ", fp);

      fclose(fp);
}

void read_file(FILE* fp)
{
     int i_addend, i_augend, i_result;
     fp = fopen("Summation.txt", "r+");

      int* ip_usr_data;
      ip_usr_data = malloc(sizeof(int));

      char* chp_result;
      char* ch_num = malloc(sizeof(char) * DIGIT_SIZE);
      int i_count = 0;
      int i_sign = 0;
      bool b_check = false;
      bool b_flag = false;
      char ch_temp_data;
      while ((ch_temp_data = fgetc(fp)) != EOF)
      {
            if ((ch_temp_data == ' ')&&(b_check))
            {
               b_check = false;
               *(ch_num + i_count) = '0';
               i_count = 0;

                  *ip_usr_data = atoi(ch_num);

                  if (i_sign < 0)
                  {
                     *ip_usr_data *= -1;
                     i_sign = 0;
                  }

                  if (*ip_usr_data)
                  {
                     if (b_flag)
                     {
                        i_augend = *ip_usr_data;
                        b_flag = false;

M.Tech – CSE – Weekend (2012 – 2015)                      Page 54
Programming in C of College days

                       }
                       else if (!b_flag)
                       {
                            i_addend = *ip_usr_data;
                       }
                  }
              }
              else if (isdigit(ch_temp_data))
              {
                   b_check = true;
                   *(ch_num + i_count) = ch_temp_data;
                   i_count++;
              }
              else if (ch_temp_data == '+')
              {
                   b_flag = true;
              }
              else if (ch_temp_data == '-')
              {
                   i_sign = -1;
              }
              else if (ch_temp_data == '=')
              {
                   i_result = i_addend + i_augend;
                   printf("Addend: %dn", i_addend);
                   printf("Augend: %dn", i_augend);

                      chp_result = int_to_alpha(i_result);
                      printf("Summation-: %sn", chp_result);
              }
      }

      fputs(chp_result, fp);
      if (fclose(fp) == 0)
         printf("Normal termination.n");
      else
         printf("Abnormal termination.n");

}




M.Tech – CSE – Weekend (2012 – 2015)                            Page 55
Programming in C of College days

    15. WAP to draw the given pattern as per user value.

                          *
                          **
                          ***
                          ****
                          *****

Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t*n");
     printf("t**n");
     printf("t***n");
     printf("t****n");
     printf("t*****n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col;
     for(ui_row = 1; ui_row <= i_number; ++ui_row)
     {
          printf("nt");
          for(ui_col = 1; ui_col <= ui_row; ++ui_col)
               printf("*");
     }

      printf("nnn");
}



M.Tech – CSE – Weekend (2012 – 2015)                       Page 56
Programming in C of College days

int main()
{
    set_argument();
    draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 57
Programming in C of College days

    16. WAP to draw the given pattern as per user value.

                                1
                                12
                                123
                                1234
                                12345

    Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t1n");
     printf("t12n");
     printf("t123n");
     printf("t1234n");
     printf("t12345n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col;
     for(ui_row = 1; ui_row <= i_number; ++ui_row)
     {
        printf("nt");
        for(ui_col = 1; ui_col <= ui_row; ++ui_col)
            printf("%d", ui_col);
     }
}

int main()
{
    set_argument();

M.Tech – CSE – Weekend (2012 – 2015)                       Page 58
Programming in C of College days

     draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 59
Programming in C of College days

    17. WAP to draw the given pattern as per user value.

                                    1
                                   21
                                  321
                                 4321
                                54321

    Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

      printf("n");
      printf("t    1n");
      printf("t   21n");
      printf("t 321n");
      printf("t 4321n");
      printf("t54321n");
      printf("n");

      printf("Please Enter a single digit number to draw pattern:
");
      scanf("%d", &i_number);

      printf("nnn");
}

void draw_pattern()
{
      unsigned int ui_row, ui_col, ui_count;
    for(ui_row = 1; ui_row <= i_number; ++ui_row)
    {
         printf("nt");
         unsigned int ui_blank_count = (i_number - ui_row);
         for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col)
             printf(" ");

          unsigned int ui_num_count = (i_number - ui_blank_count);
          for(ui_count = ui_num_count; ui_count >= 1; --ui_count)
              printf("%d", ui_count);

M.Tech – CSE – Weekend (2012 – 2015)                           Page 60
Programming in C of College days

     }
}


int main()
{
    set_argument();
    draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 61
Programming in C of College days

    18.WAP to draw the given pattern as per user value.

                                5
                                54
                                543
                                5432
                                54321

    Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t5n");
     printf("t54n");
     printf("t543n");
     printf("t5432n");
     printf("t54321n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col;

      for(ui_row = 1; ui_row <= i_number; ++ui_row)
      {
         printf("nt");
         int i_count = i_number;
         for(ui_col = 1; ui_col <= ui_row; ++ui_col, --i_count)
             printf("%d", i_count);
      }
}



M.Tech – CSE – Weekend (2012 – 2015)                        Page 62
Programming in C of College days

int main()
{
    set_argument();
    draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 63
Programming in C of College days

    19. WAP to draw the given pattern as per user value.

                                 5
                                45
                               345
                              2345
                             12345

    Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

      printf("n");
      printf("t    5n");
      printf("t   45n");
      printf("t 345n");
      printf("t 2345n");
      printf("t12345n");
      printf("n");

      printf("Please Enter a single digit number to draw pattern:
");
      scanf("%d", &i_number);

      printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col, ui_count;
     for(ui_row = 1; ui_row <= i_number; ++ui_row)
     {
        printf("nt");
        unsigned int ui_blank_count = (i_number - ui_row);
        for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col)
            printf(" ");

        int i_count = ui_blank_count;
        for(ui_count = 1; ui_count <= ui_row; ++ui_count,
++i_count)

M.Tech – CSE – Weekend (2012 – 2015)                          Page 64
Programming in C of College days

               printf("%d", (i_count+1));
           }
}

int main()
{
    set_argument();
    draw_pattern();

        printf("nnn");
        system("pause");
        return 0;
    }




M.Tech – CSE – Weekend (2012 – 2015)                    Page 65
Programming in C of College days

    20.      WAP to draw the given pattern as per user value.

                                54321
                                5432
                                543
                                54
                                5

Program:

#include <stdio.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

      printf("n");
      printf("t54321n");
      printf("t5432n");
      printf("t543n");
      printf("t54n");
      printf("t5n");
      printf("n");

      printf("Please Enter a single digit number to draw pattern:
");
      scanf("%d", &i_number);

      printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col;
     for(ui_row = 1; ui_row <= i_number; ++ui_row)
     {
        printf("nt");
        for(ui_col = i_number; ui_col >= ui_row; --ui_col)
            printf("%d", ui_col);
        }
}




M.Tech – CSE – Weekend (2012 – 2015)                            Page 66
Programming in C of College days

int main()
{
    set_argument();
    draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 67
Programming in C of College days

    21. WAP to draw the given pattern as per user value.

                               5
                             545
                            54345
                           5432345
                          543212345

Program:

#include <stdio.h>
#include <stdbool.h>

int i_number;

void set_argument()
{
    printf("***To Draw The given pattern as per user
value.***nnn");

     printf("n");
     printf("t    5n");
     printf("t   545n");
     printf("t 54345n");
     printf("t 5432345n");
     printf("t543212345n");
     printf("n");

     printf("Please Enter a number to draw pattern: ");
     scanf("%d", &i_number);

     printf("nnn");
}

void draw_pattern()
{
     unsigned int ui_row, ui_col, ui_blank;
    for(ui_row = 0; ui_row <= i_number; ++ui_row)
     {
          printf("nt");
          unsigned int ui_blank_count = (i_number - ui_row);
          for(ui_blank = 1; ui_blank <= ui_blank_count;
++ui_blank)
             printf(" ");

             bool b_flag_f = true;
             bool b_flag_b = true;

M.Tech – CSE – Weekend (2012 – 2015)                       Page 68
Programming in C of College days

         for(ui_col = (2*ui_row + 1); ui_col >= 1; --ui_col)
         {
             if (((ui_col%2)==0)&&(b_flag_f))
             {
                unsigned int ui_count;
                    int i_count = i_number;
                  for(ui_count = 1; ui_count <= (ui_col/2);
++ui_count, --i_count)
                  printf("%d", i_count);

                     b_flag_f = false;
             }
             else if(((ui_col%2)==0)&&(!b_flag_f)&&(b_flag_b))
             {
                  unsigned int ui_count;
                     int i_count = i_number - (ui_col/2) +1;
                  for (ui_count = (ui_col/2); ui_count >= 1; --
ui_count, ++i_count)
                       printf("%d", i_count);

                      b_flag_b = false;
                 }
           }
      }
}


int main()
{
    set_argument();
    draw_pattern();

     printf("nnn");
     system("pause");
     return 0;
}




M.Tech – CSE – Weekend (2012 – 2015)                       Page 69
Programming in C of College days

    22.      WAP to pass array element one by one using pointer and
       function.

Program:

#include <stdio.h>

#define SIZE_ARRAY 3

void print_item(int* item);

int main()
{
    printf("WAP to pass array element one by one using pointer
and function.n");
    printf("nnn");

     int storage[SIZE_ARRAY][SIZE_ARRAY];

    printf("Given order of array is %d * %dn", SIZE_ARRAY,
SIZE_ARRAY);

    int i_loop_count, j_loop_count;
    for (i_loop_count = 0; i_loop_count < SIZE_ARRAY;
++i_loop_count)
        for (j_loop_count = 0; j_loop_count < SIZE_ARRAY;
++j_loop_count)
        {
            printf("Enter item for array[%d][%d]:
",i_loop_count, j_loop_count);
            scanf("%d", &storage[i_loop_count][j_loop_count]);
        }


    printf("nnn");
    printf("Output as Array-element:n");
    for (i_loop_count = 0; i_loop_count < SIZE_ARRAY;
++i_loop_count)
        for (j_loop_count = 0; j_loop_count < SIZE_ARRAY;
++j_loop_count)
            print_item(&storage[i_loop_count][j_loop_count]);

     printf("nnn");
     system("pause");
     return 0;
}


M.Tech – CSE – Weekend (2012 – 2015)                                  Page 70
Programming in C of College days

void print_item(int* i_item)
{
     static int i_count = 1;

      printf("%d ", *i_item);

      if ((i_count%3)==0)
         printf("n");

      i_count++;
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 71
Programming in C of College days

   23.       WAP to find (+)ive, (-)ive, even, odd, prime element from array.

Program:

#include <stdio.h>
#include <stdbool.h>

#define SIZE_ARRAY 3

void   print_pos_item(int** item, int i_row, int i_col);
void   print_neg_item(int** item, int i_row, int i_col);
void   print_evn_item(int** item, int i_row, int i_col);
void   print_odd_item(int** item, int i_row, int i_col);
void   print_prime_item(int** item, int i_row, int i_col);

int main()
{
    printf("WAP to find (+)ive, (-)ive, even, odd, prime element
from array.n");
    printf("nnn");

     int i_row, i_col;

     printf("Please enter the Order of Mtrix.");
     printf("nEnter number of Rows: ");
     scanf("%d", &i_row);
     printf("nEnter number of Columns: ");
     scanf("%d", &i_col);

     int** ip_storage = malloc(sizeof(int) * i_row * i_col);

     int i_count;
     for (i_count = 0; i_count < i_row; ++i_count)
         *(ip_storage + i_count) = malloc(sizeof(int) * i_col);

    printf("Please enter %d items for corresponding rows and
columns of storage: nn", (i_row * i_col));

    int i_loop_count, j_loop_count;
    for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count)
        for (j_loop_count = 0; j_loop_count < i_col;
++j_loop_count)
        {
            printf("Enter item for array[%d][%d]:
",i_loop_count, j_loop_count);
            scanf("%d", &(*(*(ip_storage + i_loop_count) +
j_loop_count)));

M.Tech – CSE – Weekend (2012 – 2015)                                   Page 72
Programming in C of College days

          }

     printf("nnn");
     print_pos_item(ip_storage, i_row, i_col);

     printf("nnn");
     print_neg_item(ip_storage, i_row, i_col);

     printf("nnn");
     print_evn_item(ip_storage, i_row, i_col);

     printf("nnn");
     print_odd_item(ip_storage, i_row, i_col);

     printf("nnn");
     print_prime_item(ip_storage, i_row, i_col);

     printf("nnn");
     system("pause");
     return 0;
}

void print_pos_item(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of (+)ive items:n");

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             if ((*(*(ip_storage + i_loop_count) + j_loop_count)
>= 0))
                printf("%d, ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
}

void print_neg_item(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of (-)ive items:n");



M.Tech – CSE – Weekend (2012 – 2015)                          Page 73
Programming in C of College days

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             if ((*(*(ip_storage + i_loop_count) + j_loop_count)
< 0))
                printf("%d, ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
}

void print_evn_item(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of even items:n");

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             if (((*(*(ip_storage + i_loop_count) +
j_loop_count)%2) == 0))
                printf("%d, ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
}

void print_odd_item(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of odd items:n");

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             if ((*(*(ip_storage + i_loop_count) +
j_loop_count)%2) != 0)
                printf("%d, ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
}

M.Tech – CSE – Weekend (2012 – 2015)                          Page 74
Programming in C of College days

void print_prime_item(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;
     bool b_check;

      printf("List of prime items:n");

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
                int i_count;
                for(i_count = 2; i_count <= (i_count/2);
++i_count)
                {
                    if((*(*(ip_storage + i_loop_count) +
j_loop_count) % i_count) == 0)
                    {
                         b_check = true;
                         break;
                    }
                    else
                         b_check = false;
                }
                if(!b_check)
                {
                    printf("%d, ", *(*(ip_storage +
i_loop_count) + j_loop_count));
                }
         }
}




M.Tech – CSE – Weekend (2012 – 2015)                       Page 75
Programming in C of College days

   24.     WAP to display array element in row-major and column-mjor
      technique.

Program:

#include <stdio.h>
#include <stdbool.h>

void row_major_tech(int** item, int i_row, int i_col);
void col_major_tech(int** item, int i_row, int i_col);

int main()
{
    printf("WAP to display array element in row-major and
column-mjor technique.n");
    printf("nnn");

     int i_row, i_col;

     printf("Please enter the Order of Mtrix.");
     printf("nEnter number of Rows: ");
     scanf("%d", &i_row);
     printf("nEnter number of Columns: ");
     scanf("%d", &i_col);

     int** ip_storage = malloc(sizeof(int) * i_row * i_col);

     int i_count;
     for (i_count = 0; i_count < i_row; ++i_count)
         *(ip_storage + i_count) = malloc(sizeof(int) * i_col);

    printf("Please enter %d items for corresponding rows and
columns of storage: nn", (i_row * i_col));

    int i_loop_count, j_loop_count;
    for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count)
        for (j_loop_count = 0; j_loop_count < i_col;
++j_loop_count)
        {
            printf("Enter item for array[%d][%d]:
",i_loop_count, j_loop_count);
            scanf("%d", &(*(*(ip_storage + i_loop_count) +
j_loop_count)));
        }

     printf("nnn");
     row_major_tech(ip_storage, i_row, i_col);

M.Tech – CSE – Weekend (2012 – 2015)                            Page 76
Programming in C of College days

     printf("nnn");
     col_major_tech(ip_storage, i_row, i_col);

     printf("nnn");
     system("pause");
     return 0;
}

void row_major_tech(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of items as Row-Major technique:n");

     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
     {
         printf("n");
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             printf("%d ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
     }
}

void col_major_tech(int** ip_storage, int i_row, int i_col)
{
     int i_loop_count, j_loop_count;

      printf("List of items as Column-Major technique:n");

     for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
     {
         printf("n");
         for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         {
             printf("%d ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
     }
}


M.Tech – CSE – Weekend (2012 – 2015)                          Page 77
Programming in C of College days

   25.       WAP to transpose a given matrix using pointer and function.

Program:

#include <stdio.h>
#include <stdbool.h>

#define SIZE_ARRAY 3

void print_trans_matrix(int** item, int i_row, int i_col);

int main()
{
    printf("WAP to transpose a given using pointer and
function.n");
    printf("nnn");

     int i_row, i_col;

     printf("Please enter the Order of Mtrix.");
     printf("nEnter number of Rows: ");
     scanf("%d", &i_row);
     printf("nEnter number of Columns: ");
     scanf("%d", &i_col);

     int** ip_storage = malloc(sizeof(int) * i_row * i_col);

     int i_count;
     for (i_count = 0; i_count < i_row; ++i_count)
         *(ip_storage + i_count) = malloc(sizeof(int) * i_col);

    printf("Please enter %d items for corresponding rows and
columns of storage: nn", (i_row * i_col));

    int i_loop_count, j_loop_count;
    for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count)
        for (j_loop_count = 0; j_loop_count < i_col;
++j_loop_count)
        {
            printf("Enter item for array[%d][%d]:
",i_loop_count, j_loop_count);
            scanf("%d", &(*(*(ip_storage + i_loop_count) +
j_loop_count)));
        }

     printf("nnn");

M.Tech – CSE – Weekend (2012 – 2015)                                 Page 78
Programming in C of College days

     print_trans_matrix(ip_storage, i_row, i_col);

     printf("nnn");
     system("pause");
     return 0;
}

void print_trans_matrix(int** ip_storage, int i_row, int i_col)
{
    int i_loop_count, j_loop_count;
    int** trans_mat = malloc(sizeof(int) * i_row * i_col);

     int i_count;
     for (i_count = 0; i_count < i_row; ++i_count)
         *(trans_mat + i_count) = malloc(sizeof(int) * i_col);

     printf("nMatrix You entered:");
     for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
     {
         printf("n");
         for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
         {
             printf("%d ", *(*(ip_storage + i_loop_count) +
j_loop_count));
         }
     }

      printf("nMatrix after Transpose:");

     for (j_loop_count = 0; j_loop_count < i_row;
++j_loop_count)
     {
         printf("n");
         for (i_loop_count = 0; i_loop_count < i_row;
++i_loop_count)
         {
             *(*(trans_mat + j_loop_count) + i_loop_count) =
*(*(ip_storage + i_loop_count) + j_loop_count);
             printf("%d ", *(*(trans_mat + j_loop_count) +
i_loop_count));
         }
     }
}



M.Tech – CSE – Weekend (2012 – 2015)                          Page 79
Programming in C of College days

    26.      WAP to display the mathematical table of given number.

Program:

#include <stdio.h>

void disp_mult_table_range(int i_lr, int i_hr);

int main()
{
    printf("WAP to display the mathematical table of given range
of numbers.n");
    printf("nnn");

     int i_lr, i_hr;
     printf("Please Enter lower limit: ");
     scanf("%d", &i_lr);
     printf("Please Enter higher limit: ");
     scanf("%d", &i_hr);

     disp_mult_table_range(i_lr, i_hr);

     printf("nnn");
     system("pause");
     return 0;
}

void disp_mult_table_range(int i_lr, int i_hr)
{
     int i_multiplicand, i_multiplier;

     for (i_multiplier = i_lr; i_multiplier <= i_hr;
++i_multiplier)
     {
         printf("nnn");
         printf("Multiplication Table of %d:n", i_multiplier);
         for (i_multiplicand = 1; i_multiplicand <= 10;
++i_multiplicand)
         {
             printf("t%d * %d = %dn", i_multiplier,
i_multiplicand, (i_multiplier * i_multiplicand));
         }
     }
}




M.Tech – CSE – Weekend (2012 – 2015)                                  Page 80
Programming in C of College days

    27.      WAP to display the mathematical table of given number.

Program:

#include <stdio.h>

void disp_mult_table(int i_num);

int main()
{
    printf("WAP to display the mathematical table of given
number.n");
    printf("nnn");

     int i_num;
     printf("Please Enter number: ");
     scanf("%d", &i_num);

     disp_mult_table(i_num);

     printf("nnn");
     system("pause");
     return 0;
}

void disp_mult_table(int i_num)
{
     int i_multiplicand;
     for (i_multiplicand = 1; i_multiplicand <= 10;
++i_multiplicand)
     {
         printf("t%d * %d = %dn", i_num, i_multiplicand,
(i_num * i_multiplicand));
     }
}




M.Tech – CSE – Weekend (2012 – 2015)                                  Page 81
Programming in C of College days

    28.      WAP to display alphabets „a‟ to „z‟ and “A” to “Z”
          a. Using pointers.
          b. Without using pointers.

Program:

          a. Using pointers.

#include <stdio.h>
#include <stdbool.h>

void disp_lower_case_alphabets_wp(void);
void disp_upper_case_alphabets_wp(void);

int main()
{
    printf("WAP to display a to z and A to Z by using
pointer.n");
    printf("nnn");

     disp_lower_case_alphabets_wp();
     printf("nnn");
     disp_upper_case_alphabets_wp();

     printf("nnn");
     system("pause");
     return 0;
}

void disp_lower_case_alphabets_wp(void)
{
    printf("Displaying lower - case alphabets:-n");
    char* chp_var = "a";
    unsigned int ui_loop_count;
    for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count)
    {
        if (ui_loop_count%10 == 0)
           printf("n");
        printf("%ct", *chp_var + ui_loop_count);
    }
}




M.Tech – CSE – Weekend (2012 – 2015)                              Page 82
Programming in C of College days

void disp_upper_case_alphabets_wp(void)
{
     printf("Displaying lower - case alphabets:-n");
     char* chp_var = "A";
     unsigned int ui_loop_count;
     for (ui_loop_count = 0; ui_loop_count < 26;
++ui_loop_count)
     {
         if (ui_loop_count%10 == 0)
         printf("n");
         printf("%ct", *chp_var + ui_loop_count);
     }
}


    ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~


         a. Without using pointers.

#include <stdio.h>
#include <stdbool.h>

void disp_lower_case_alphabets(void);
void disp_upper_case_alphabets(void);

int main()
{
    printf("WAP to display a to z and A to Z by using
pointer.n");
    printf("nnn");

     disp_lower_case_alphabets();
     printf("nnn");
     disp_upper_case_alphabets();

     printf("nnn");
     system("pause");
     return 0;
}

void disp_lower_case_alphabets(void)
{
    printf("Displaying lower - case alphabets:-n");
    char chp_var = 'a';
    unsigned int ui_loop_count;
    for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count)

M.Tech – CSE – Weekend (2012 – 2015)                       Page 83
Programming in C of College days

     {
          if (ui_loop_count%10 == 0)
             printf("n");
          printf("%ct", chp_var + ui_loop_count);
     }
}


void disp_upper_case_alphabets(void)
{
     printf("Displaying lower - case alphabets:-n");
     char chp_var = 'A';
     unsigned int ui_loop_count;
     for (ui_loop_count = 0; ui_loop_count < 26;
++ui_loop_count)
     {
         if (ui_loop_count%10 == 0)
            printf("n");
         printf("%ct", chp_var + ui_loop_count);
     }
}




M.Tech – CSE – Weekend (2012 – 2015)                    Page 84
Programming in C of College days

      29. WAP to take character sequence (string) and diplay it, using
      pointer and functions.

Program:

#include <stdio.h>
#include <stdbool.h>

#define MAX_SEQL_SIZE 1024

char* get_char_seql(void);
void disp_char_seql(char*);

int main()
{
    printf("WAP to take character sequence (string) and diplay
it, using pointer and functions.n");
    printf("nnn");

     printf("Please press 'dot'(.) as terminating character.");
     printf("nnn");

     char* chp_seql;
     chp_seql = get_char_seql();
     printf("nnn");
     disp_char_seql(chp_seql);

     printf("nnn");
     system("pause");
     return 0;
}

char* get_char_seql(void)
{
    printf("Enter your character sequel:- ");
    char* chp_var = (char*)malloc(sizeof(char) * MAX_SEQL_SIZE);

     char ch_temp;
     unsigned int ui_count = 0;
     while((ch_temp = getchar()) != '.')
     {
         *(chp_var + ui_count) = ch_temp;
         ui_count++;
     }
     *(chp_var + ui_count) = '0';


M.Tech – CSE – Weekend (2012 – 2015)                               Page 85
Programming in C of College days

     return chp_var;
}

void disp_char_seql(char* chp_seql)
{
     printf("Character sequel you entered:- %s", chp_seql);
}




M.Tech – CSE – Weekend (2012 – 2015)                          Page 86
Programming in C of College days

Note:
    For Visual Studio 2008 and Qt (version- 4.2) you need to format the code as, make
     all the initialization throughout the function to very initial steps after opening
     braces.




M.Tech – CSE – Weekend (2012 – 2015)                                             Page 87

More Related Content

What's hot

Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomeshpraveensomesh
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentalsZaibi Gondal
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 

What's hot (20)

Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
 
C questions
C questionsC questions
C questions
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Ansi c
Ansi cAnsi c
Ansi c
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C Programming
C ProgrammingC Programming
C Programming
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 

Similar to Lab. Programs in C

C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13alish sha
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2Koshy Geoji
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-yearAMIT SINGH
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using cArghodeepPaul
 

Similar to Lab. Programs in C (20)

C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
week-3x
week-3xweek-3x
week-3x
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
C Programming
C ProgrammingC Programming
C Programming
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
 
C lab
C labC lab
C lab
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 

More from Saket Pathak

Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important QuestionsSaket Pathak
 
A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STLSaket Pathak
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?Saket Pathak
 
Malloc, calloc, realloc
Malloc, calloc, reallocMalloc, calloc, realloc
Malloc, calloc, reallocSaket Pathak
 
C++ diamond problem
C++ diamond problemC++ diamond problem
C++ diamond problemSaket Pathak
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++Saket Pathak
 

More from Saket Pathak (12)

Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important Questions
 
Wan notes
Wan notesWan notes
Wan notes
 
A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STL
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
 
C++ friendship
C++ friendshipC++ friendship
C++ friendship
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Copy constructor
Copy constructorCopy constructor
Copy constructor
 
Malloc, calloc, realloc
Malloc, calloc, reallocMalloc, calloc, realloc
Malloc, calloc, realloc
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C++ diamond problem
C++ diamond problemC++ diamond problem
C++ diamond problem
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

Lab. Programs in C

  • 1. 2012 Saket Kumar Pathak Software Developer 3D Graphics [Programming in C of College days ] Programs are tested with compiler and respective IDEs Bloodshed-DevC++, Visual Studio 2008, Qt 4.2. These are running successfully with console in windows platform. So just enjoy coding. For Visual Studio 2008 and Qt 4.2, please notice the note at the end.
  • 2. Programming in C of College days 1. WAP to convert a. Binary to Decimal b. Decimal to Binary Program: Binary to Decimal: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define DATA_SIZE 32 char* rev_data_seq(char* chp_data_seq); void bin_to_dec() { printf("WAP to convert :nt-> Binary to Decimal"); printf("nLimitation: nt-> Fractional Numbers will not considered. nt-> Binary format is limited to 32 bit. nt-> Input will considered in Unsigned format."); printf("nnn"); int i_res, i_num, i_count; char ch_digit; char* chp_data_seq = (char*)malloc(sizeof(char) * DATA_SIZE); i_res = i_num = i_count = 0; printf("Please Enter any number (Binary in base 2): "); while((ch_digit = getchar()) != 'n') { if((ch_digit == '0')||(ch_digit == '1')) { *(chp_data_seq + i_count) = ch_digit; i_count++; } else if(i_count > 32) { i_res = 0; printf("nInvalid Input.n"); break; } else M.Tech – CSE – Weekend (2012 – 2015) Page 2
  • 3. Programming in C of College days { i_res = 0; printf("nInvalid Input.n"); break; } } *(chp_data_seq + i_count) = '0'; chp_data_seq = rev_data_seq(chp_data_seq); i_count = 0; while (*(chp_data_seq + i_count) != '0') { ch_digit = *(chp_data_seq + i_count); if(ch_digit == '0') i_num = (int)(0 * pow(2.0f, i_count)); else if(ch_digit == '1') i_num = (int)(1 * pow(2.0f, i_count)); i_res += i_num; i_count++; } printf("nResultant: %d", i_res); } char* rev_data_seq(char* chp_data_seq) { char* temp_seq = (char*)malloc(sizeof(char) * (strlen(chp_data_seq) + 1)); int i_count = 0; int i_rev = (strlen(chp_data_seq)-1); while (*(chp_data_seq + i_count) != '0') { *(temp_seq + i_rev) = *(chp_data_seq + i_count); i_count++; i_rev--; } *(temp_seq + i_count) = '0'; return temp_seq; } M.Tech – CSE – Weekend (2012 – 2015) Page 3
  • 4. Programming in C of College days int main() { dec_to_bin(); getch(); return 0; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Decimal to Binary: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define DATA_SIZE 32 void dec_to_bin() { printf("WAP to convert :nt-> Decimal to Binary"); printf("nLimitation: nt-> Fractional Numbers will not considered. nt-> Binary format is limited to 32 bit. nt-> Generated result will be in Unsigned format."); printf("nnn"); int i_num; printf("Please Enter any number (Decimal in base 10): "); scanf("%d", &i_num); int* i_res = (int*)malloc(sizeof(int) * DATA_SIZE); int i_count = 0; while(i_count < DATA_SIZE) { *(i_res + i_count) = 0; i_count++; } printf("n"); i_count = (DATA_SIZE-1); int i_quotent, i_remainder; do { i_quotent = i_num / 2; M.Tech – CSE – Weekend (2012 – 2015) Page 4
  • 5. Programming in C of College days i_remainder = i_num % 2; i_num = i_quotent; *(i_res + i_count) = i_remainder; i_count--; } while(i_quotent > 0); printf("nResultant: "); i_count = 0; while(i_count < DATA_SIZE) { printf("%d", *(i_res + i_count)); i_count++; } } int main() { dec_to_bin(); getch(); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 5
  • 6. Programming in C of College days 2. WAP to generate prime numbers within a given range. Program: #include <stdio.h> #include <stdbool.h> int i_lr, i_hr, i_count_prime; int *i_collector; bool b_check; void set_argument() { printf("Please Enter Number to for lower limit of Range: "); scanf("%d", &i_lr); printf("Please Enter Number to for Upper limit of Range: "); scanf("%d", &i_hr); printf("nnn"); } void find_numbers() { printf(" Given Range to find prime numbers is %d - %d.n", i_lr, i_hr); i_collector = (int*)malloc(sizeof(int) * 1024); *i_collector = 1; { int i_count; for(i_count = i_lr; i_count <= i_hr; ++i_count) { if(i_count > 3) { int i_loopcount; for(i_loopcount = 2; i_loopcount <= (i_count/2); ++i_loopcount) { if((i_count % i_loopcount) == 0) { b_check = true; break; } else b_check = false; } if(!b_check) M.Tech – CSE – Weekend (2012 – 2015) Page 6
  • 7. Programming in C of College days { *i_collector = i_count; i_collector+=sizeof(int); i_count_prime++; } } else { *i_collector = i_count; i_collector+=sizeof(int); printf("n The Number 2 and 3 are prime."); } } } printf("nnn"); } void show_result() { i_collector-=sizeof(int); printf("Prime Number So obtained: n"); printf("Index t PrimeNumbern"); int i_idx = 1; while(i_count_prime > 0) { printf(" %dt %dn", i_idx, *i_collector); i_collector = i_collector - sizeof(int); --i_count_prime; ++i_idx; } } int main() { printf("WAP to Find Prime numbers with a given range."); printf("nnn"); set_argument(); find_numbers(); show_result(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 7
  • 8. Programming in C of College days 3. WAP to generate following patterns, if line =5 (a) 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 (b) * *** * **** ******* ********* Program: (a) #include <stdio.h> #include <stdbool.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t1n"); printf("t01n"); printf("t101n"); printf("t0101n"); printf("t10101n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } M.Tech – CSE – Weekend (2012 – 2015) Page 8
  • 9. Programming in C of College days void draw_pattern() { int container[2] = {1,0}; unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= (unsigned int)i_number; ++ui_row) { printf("nt"); int count; bool b_flag = true; for(ui_col = 1; ui_col <= ui_row; ++ui_col) { if(b_flag) { if((ui_row%2)==0) count = 1; else count = 0; b_flag = false; } printf("%d", container[count]); if(count==1) count = 0; else count = 1; } } } int main() { set_argument(); draw_pattern(); getch(); return 0; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ M.Tech – CSE – Weekend (2012 – 2015) Page 9
  • 10. Programming in C of College days (b) #include <stdio.h> #include <stdbool.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t *n"); printf("t ***n"); printf("t *****n"); printf("t *******n"); printf("t*********n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_blank, ui_col; for(ui_row = 0; ui_row <= (unsigned int)i_number; ++ui_row) { printf("nt"); for(ui_blank = 1; ui_blank <= (i_number-ui_row); ++ui_blank) printf(" "); for(ui_col = 1; ui_col <= (2*ui_row + 1); ++ui_col) printf("*"); } } M.Tech – CSE – Weekend (2012 – 2015) Page 10
  • 11. Programming in C of College days int main() { set_argument(); draw_pattern(); getch(); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 11
  • 12. Programming in C of College days 4. WAP to generate recursive Fibonacci series. Program: #include <stdio.h> #include <stdbool.h> void create_fibonacci(bool flag, unsigned int i_count); int main() { printf("WAP to generate Fibonacci series using recursion.n"); printf("nnn"); int i_term, i_rec; unsigned int ui_count; printf("Enter a term to create fibonacci: "); scanf("%d", &i_term); printf("Fibonacci: 0 1"); create_fibonacci(true, (i_term-2)); system("pause"); return 0; } void create_fibonacci(bool flag, unsigned int i_count) { static int i_num_1, i_num_2, i_num_3; if (flag) { i_num_1 = 0; i_num_2 = 1; flag = false; } if (i_count > 0) { i_num_3 = i_num_1 + i_num_2; printf(" %d", i_num_3); i_num_1 = i_num_2; i_num_2 = i_num_3; create_fibonacci(false, --i_count); } M.Tech – CSE – Weekend (2012 – 2015) Page 12
  • 13. Programming in C of College days printf("n"); } M.Tech – CSE – Weekend (2012 – 2015) Page 13
  • 14. Programming in C of College days 5. WAP to perform binary search by using a. Recursion. b. Non recursion. Program: a. Recursion #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <math.h> bool check_item_order(int* i_storage) { //Function to check the Item - List is in Sorted order. return true; } void recursive_bin_search(int* i_storage, int i_srch_item, int i_low, int i_mid, int i_hi) { if(i_low < i_hi) { if(i_storage[i_mid] == i_srch_item) { printf("Item (%d) is found at position: %d", i_srch_item, i_mid); } else if(i_storage[i_mid] > i_srch_item) { i_hi = i_mid; i_mid = (i_low + i_hi)/2; recursive_bin_search(i_storage, i_srch_item, i_low, i_mid, i_hi); } else if(i_storage[i_mid] < i_srch_item) { i_low = i_mid; i_mid = (i_low + i_hi)/2; recursive_bin_search(i_storage, i_srch_item, i_low, i_mid, i_hi); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 14
  • 15. Programming in C of College days void recursive_binary_search(int* i_storage, int i_num_item) { bool b_check = check_item_order(i_storage); printf("Your Storage Items are: nt"); int i_loop_count; for(i_loop_count = 0; i_loop_count < i_num_item; ++i_loop_count) { printf("%d, ", i_storage[i_loop_count]); } printf("nnn"); int i_srch_item; printf("Please Enter the item (number) to search: "); scanf("%d", &i_srch_item); printf("nnn"); int i_low = 0; int i_hi = i_num_item; int i_mid = (i_low + i_hi)/2; if(b_check) recursive_bin_search(i_storage, i_srch_item, i_low, i_mid, i_hi); else printf("nItems are not in Order.n"); } void binary_search(void) { printf("WAP for Binary - Search."); printf("nLimitation: nt-> Items are restrickted with integer number.nt-> Starting index of storage is 0."); printf("nnn"); int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; recursive_binary_search(i_storage, (sizeof(i_storage)/sizeof(int))); } M.Tech – CSE – Weekend (2012 – 2015) Page 15
  • 16. Programming in C of College days int main() { binary_search(); printf("nnn"); getch(); return 0; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ b. Non recursion #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <math.h> bool check_item_order(int* i_storage) { return true; } void iterative_binary_search(int* i_storage, int i_num_item) { bool b_check = check_item_order(i_storage); printf("Your Storage Items are: nt"); int i_loop_count; for(i_loop_count = 0; i_loop_count < i_num_item; ++i_loop_count) { printf("%d, ", i_storage[i_loop_count]); } printf("nnn"); int i_srch_item; printf("Please Enter the item (number) to search: "); scanf("%d", &i_srch_item); printf("nnn"); M.Tech – CSE – Weekend (2012 – 2015) Page 16
  • 17. Programming in C of College days printf("Result: "); int i_low = 0; int i_hi = i_num_item; int i_mid = (i_low + i_hi)/2; if (b_check) { while(i_low < i_hi) { if(i_storage[i_mid] == i_srch_item) { printf("Item (%d) is found at position: %d", i_srch_item, i_mid); break; } else if(i_storage[i_mid] > i_srch_item) { i_hi = i_mid; i_mid = (i_low + i_hi)/2; } else if(i_storage[i_mid] < i_srch_item) { i_low = i_mid; i_mid = (i_low + i_hi)/2; } } } } void binary_search(void) { printf("WAP for Binary - Search."); printf("nLimitation: nt-> Items are restrickted with integer number.nt-> Starting index of storage is 0."); printf("nnn"); int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; iterative_binary_search(i_storage, (sizeof(i_storage)/sizeof(int))); } M.Tech – CSE – Weekend (2012 – 2015) Page 17
  • 18. Programming in C of College days int main() { binary_search(); printf("nnn"); getch(); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 18
  • 19. Programming in C of College days 6. WAP for Tower of Hanoi. Also draw the recursive tree for n=4 disks. Program: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include <math.h> void generate_moves_toh(int i_disk_num, char from_peg, char to_peg, char aux_peg) { /* If only 1 disk, make the move and return */ if(i_disk_num == 1) { printf("nMove disk 1 from peg %c to peg %c", from_peg, to_peg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ generate_moves_toh(i_disk_num-1, from_peg, aux_peg, to_peg); printf("nMove disk %d from peg %c to peg %c", i_disk_num, from_peg, to_peg); /* Move n-1 disks from B to C using A as auxiliary */ generate_moves_toh(i_disk_num-1, aux_peg, to_peg, from_peg); } void gen_tow_of_honoi() { printf("WAP to display the steps required in solving 'Tower of Hanoi' for 'n' number of disks."); printf("nLimitation: nt-> Disks are represented as integral numbers in assending order."); printf("nnn"); int i_num_disk; printf("Enter the number of disks : "); scanf("%d",&i_num_disk); printf("The Tower of Hanoi involves the moves :nn"); generate_moves_toh(i_num_disk, 'A', 'C', 'B'); M.Tech – CSE – Weekend (2012 – 2015) Page 19
  • 20. Programming in C of College days } int main() { gen_tow_of_honoi(); printf("nnn"); getch(); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 20
  • 21. Programming in C of College days 7. WAP to generate Pascal triangle viz. if line =5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Program: #include <stdio.h> #include <stdbool.h> int main() { printf("Pascal's Seriesn"); printf("nnn"); int i_num_row; printf("Enter number of rows: "); scanf("%d", &i_num_row); int arr[6], arr_temp[6]; static bool flag = true; int i_row_count, i_col_count, i_count, i_counter; for (i_row_count = 1; i_row_count <= i_num_row; ++i_row_count) { for (i_col_count = 1; i_col_count <= i_num_row; ++i_col_count) { for (i_count = 0; i_count <= i_row_count; ++i_count) { if (flag) { arr[i_count] = i_count; if (arr[i_count] != 0) printf("t%d", arr[i_count]); } else if(i_count > 0) { arr_temp[i_count] = arr[i_count-1] + arr[i_count]; printf("t%d", arr_temp[i_count]); } M.Tech – CSE – Weekend (2012 – 2015) Page 21
  • 22. Programming in C of College days else { arr_temp[i_count] = i_count; } } for (i_counter = i_count; i_counter <= (i_num_row+1); ++i_counter) { if (flag) { arr[i_counter] = 0; } else if(i_counter > 0) { arr_temp[i_counter] = arr[i_counter-1] + arr[i_counter]; } } flag = false; break; } if (i_row_count > 1) for (i_count = 1; i_count <= i_num_row; ++i_count) { arr[i_count] = arr_temp[i_count]; } printf("n"); } system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 22
  • 23. Programming in C of College days 8. Write following programs by using passing array to function : a) Bubble sort b) Insertion sort c) Selection sort Program: a) Bubble sort #include <stdio.h> int* bubble_sort(int i_store[], int i_size); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("WAP of Bubble sort by using array."); printf("nnn"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = bubble_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("nnn"); system("pause"); return 0; } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store; M.Tech – CSE – Weekend (2012 – 2015) Page 23
  • 24. Programming in C of College days } int* bubble_sort(int i_store[], int i_size) { int i_temp; int i_count_0, i_count_1; for (i_count_0 = (i_size - 1); i_count_0 > 0; --i_count_0) { for (i_count_1 = 1; i_count_1 <= i_count_0; ++i_count_1) { if (i_store[i_count_1 - 1] > i_store[i_count_1]) { i_temp = i_store[i_count_1 - 1]; i_store[i_count_1 - 1] = i_store[i_count_1]; i_store[i_count_1] = i_temp; } } } return i_store; } void disp_elem(int i_store[], int i_size) { printf("nDisplaying Elements of store: n"); int i_ndx = 0; while (i_size) { printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ M.Tech – CSE – Weekend (2012 – 2015) Page 24
  • 25. Programming in C of College days Insertion sort #include <stdio.h> int* insertion_sort(int i_store[], int i_size); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("WAP of Insertion sort by using array."); printf("nnn"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = insertion_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("nnn"); system("pause"); return 0; } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store; } int* insertion_sort(int i_store[], int i_size) { int i_temp; int i_count_0, i_count_1, i_count_2; for (i_count_0 = 1; i_count_0 <= (i_size-1); ++i_count_0) { M.Tech – CSE – Weekend (2012 – 2015) Page 25
  • 26. Programming in C of College days for (i_count_1 = 0; i_count_1 < i_count_0; ++i_count_1) { if (i_store[i_count_1] > i_store[i_count_0]) { i_temp = i_store[i_count_1] ; i_store[i_count_1] = i_store[i_count_0] ; for (i_count_2 = i_count_0; i_count_2 > i_count_1; i_count_2--) i_store[i_count_2] = i_store[i_count_2 - 1] ; i_store[i_count_2 + 1] = i_temp ; } } } return i_store; } void disp_elem(int i_store[], int i_size) { printf("nDisplaying Elements of store: n"); int i_ndx = 0; while (i_size) { printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ M.Tech – CSE – Weekend (2012 – 2015) Page 26
  • 27. Programming in C of College days Selection sort #include <stdio.h> int* selection_sort(int i_store[], int i_size); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("WAP of Selection sort by using array."); printf("nnn"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = selection_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("nnn"); system("pause"); return 0; } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store; } int* selection_sort(int i_store[], int i_size) { int i_temp; int i_count_0, i_count_1; for (i_count_0 = 0; i_count_0 < (i_size-1); ++i_count_0) { M.Tech – CSE – Weekend (2012 – 2015) Page 27
  • 28. Programming in C of College days for (i_count_1 = (i_count_0+1); i_count_1 < i_size; ++i_count_1) { if (i_store[i_count_0] > i_store[i_count_1]) { i_temp = i_store[i_count_0]; i_store[i_count_0] = i_store[i_count_1]; i_store[i_count_1] = i_temp; } } } return i_store; } void disp_elem(int i_store[], int i_size) { printf("nDisplaying Elements of store: n"); int i_ndx = 0; while (i_size) { printf("nIndex(%d) : %d", i_ndx, i_store[i_ndx]); i_ndx++; i_size--; } } M.Tech – CSE – Weekend (2012 – 2015) Page 28
  • 29. Programming in C of College days 9. WAP for matrix multiplication of given order. Program: #include <stdio.h> #include <stdbool.h> void get_matrices(void); void mult_matrices(void); void disp_matrix(void); int **mat_1; int **mat_2; int **res_mat; int i_m1_row, i_m1_col; int i_m2_row, i_m2_col; int main() { printf("WAP for matrix multiplication of given order.n"); printf("nnn"); get_matrices(); mult_matrices(); disp_matrix(); system("pause"); return 0; } void get_matrices() { printf("Enter number of rows for Matrix-1: "); scanf("%d", &i_m1_row); printf("Enter number of columbs for Matrix-1: "); scanf("%d", &i_m1_col); mat_1 = malloc(sizeof(int) * i_m1_row); int i_row_count, i_col_count; printf("Enter elements for Matrix-1:n"); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { *(mat_1+i_row_count) = malloc(sizeof(int) * i_m1_col); M.Tech – CSE – Weekend (2012 – 2015) Page 29
  • 30. Programming in C of College days for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d]: ", i_row_count, i_col_count); scanf("%d",&(*(*(mat_1+i_row_count)+i_col_count))); *(mat_1+i_row_count)+i_col_count; } } printf("nnn"); printf("Enter number of rows for Matrix-2: "); scanf("%d", &i_m2_row); printf("Enter number of columbs for Matrix-2: "); scanf("%d", &i_m2_col); mat_2 = malloc(sizeof(int) * i_m2_row); printf("Enter elements for Matrix-2:n"); for (i_row_count = 0; i_row_count < i_m2_row; ++i_row_count) { *(mat_2+i_row_count) = malloc(sizeof(int) * i_m2_col); for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { printf("Element[%d][%d]: ", i_row_count, i_col_count); scanf("%d",&(*(*(mat_2+i_row_count)+i_col_count))); *(mat_2+i_row_count)+i_col_count; } } printf("nnn"); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d] - Mat-1: %dn", i_row_count, i_col_count, mat_1[i_row_count][i_col_count]); } } printf("nnn"); M.Tech – CSE – Weekend (2012 – 2015) Page 30
  • 31. Programming in C of College days for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m1_col; ++i_col_count) { printf("Element[%d][%d] - Mat-2: %dn", i_row_count, i_col_count, mat_2[i_row_count][i_col_count]); } } printf("n"); } void mult_matrices(void) { printf("nnn"); int i_row_count, i_col_count, i_count; res_mat = malloc(sizeof(int) * i_m1_row); printf("Resultant Matrix: Row- %dt Column- %dn", i_m1_row, i_m2_col); for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { *(res_mat+i_row_count) = malloc(sizeof(int) * i_m2_col); for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { *(*(res_mat+i_row_count)+i_col_count) = 0; for (i_count = 0; i_count < i_m1_row; ++i_count) { *(*(res_mat+i_row_count)+i_col_count) += (*(*(mat_1 + i_row_count)+i_count)) * (*(*(mat_2 + i_count)+i_col_count)); } } } printf("nnn"); } M.Tech – CSE – Weekend (2012 – 2015) Page 31
  • 32. Programming in C of College days void disp_matrix(void) { int i_row_count, i_col_count; for (i_row_count = 0; i_row_count < i_m1_row; ++i_row_count) { for (i_col_count = 0; i_col_count < i_m2_col; ++i_col_count) { printf("Resultant[%d][%d] - Res-2: %dn", i_row_count, i_col_count, *(*(res_mat+i_row_count)+i_col_count)); } } printf("nnn"); } M.Tech – CSE – Weekend (2012 – 2015) Page 32
  • 33. Programming in C of College days 10. WAP without using string‟s function for : a. Copy of one string in another b. Concatenation of string c. Comparison of strings d. Reverse the string Program: a. Copy of one string in another #include <stdio.h> #define STR_SIZE 1024 char* get_string(void); char* str_copy(char* gvn_str); int main() { printf("WAP without using string's function to Copy one string into another."); printf("nnn"); char* temp = malloc(sizeof(char) * STR_SIZE); temp = get_string(); printf("nnn"); printf("Copied String: %s", str_copy(temp)); printf("nnn"); system("pause"); return 0; } char* get_string(void) { char* chp_var1; char ch_temp; int i_num = 0; chp_var1 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string to copy ('n' is terminating character): "); while ((ch_temp = getchar()) != 'n') { M.Tech – CSE – Weekend (2012 – 2015) Page 33
  • 34. Programming in C of College days *(chp_var1 + i_num) = ch_temp; i_num++; } *(chp_var1 + i_num) = '0'; return chp_var1; } char* str_copy(char* gvn_str) { char* chp_var; int i_num = 0; chp_var = malloc(sizeof(char) * STR_SIZE); while (*(gvn_str + i_num) != '0') { *(chp_var + i_num) = *(gvn_str + i_num); i_num++; } *(chp_var + i_num) = '0'; return chp_var; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ b. Concatenation of string #include <stdio.h> #define STR_SIZE 1024 char* concat_str(char* chp_var1, char* chp_var2); unsigned int get_str_len(char* chp_var); int main() { printf("WAP without using string's function for Concatenating of two strings. "); char* chp_var1; char* chp_var2; char* chp_res; M.Tech – CSE – Weekend (2012 – 2015) Page 34
  • 35. Programming in C of College days printf("nnn"); chp_var1 = malloc(sizeof(char) * STR_SIZE); chp_var2 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); printf("nn"); printf("Please Enter another string to concatenate: "); scanf("%s", chp_var2); chp_res = concat_str(chp_var1, chp_var2); printf("nResultant String (after Copying): %s", chp_res); printf("n"); system("pause"); return 0; } char* concat_str(char* chp_var1, char* chp_var2) { size_t size = get_str_len(chp_var1) + get_str_len(chp_var2); chp_var1 = (char*)realloc(chp_var1, (size+1)); unsigned int ui_count = 0; while (*(chp_var1+ui_count) != '0') ui_count++; while (*chp_var2 != '0') { *(chp_var1+ui_count) = *chp_var2; ui_count++; chp_var2++; } *(chp_var1+ui_count) = '0'; return chp_var1; } unsigned int get_str_len(char* chp_var) { M.Tech – CSE – Weekend (2012 – 2015) Page 35
  • 36. Programming in C of College days unsigned int ui_count = 0; while (*chp_var != '0') { ui_count++; chp_var++; } return ui_count; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ c. Comparison of strings #include <stdio.h> #include <stdbool.h> #define STR_SIZE 1024 bool comp_str(char* chp_var1, char* chp_var2); int main() { printf("WAP without using string's function to Comparison of strings."); char* chp_var1; char* chp_var2; bool b_res; printf("nnn"); chp_var1 = malloc(sizeof(char) * STR_SIZE); chp_var2 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); printf("nn"); printf("Please Enter another string to compare: "); scanf("%s", chp_var2); b_res = comp_str(chp_var1, chp_var2); if (b_res) printf("nStrings are same."); else printf("nStrings are not same."); M.Tech – CSE – Weekend (2012 – 2015) Page 36
  • 37. Programming in C of College days printf("n"); system("pause"); return 0; } bool comp_str(char* chp_var1, char* chp_var2) { unsigned int ui_count = 0; while (*(chp_var1+ui_count)== *(chp_var2+ui_count)) { if (*(chp_var1+ui_count) == '0') return true; ui_count++; } return false; } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ d. Reverse the string #include <stdio.h> #include <stdbool.h> #define STR_SIZE 1024 char* comp_str(char* chp_var1); unsigned int get_str_len(char* chp_var); int main() { printf("WAP without using string's function to Reverse the string."); char* chp_var1; char* chp_res; printf("nnn"); chp_var1 = malloc(sizeof(char) * STR_SIZE); printf("Please Enter an string: "); scanf("%s", chp_var1); chp_res = comp_str(chp_var1); M.Tech – CSE – Weekend (2012 – 2015) Page 37
  • 38. Programming in C of College days printf("nResultant Strings (after reverse): %s", chp_res); printf("n"); system("pause"); return 0; } char* comp_str(char* chp_var1) { size_t size = get_str_len(chp_var1); char* chp_str = malloc(size+1); unsigned int ui_rev_idx = (size - 1); unsigned int ui_count = 0; while (*(chp_var1+ui_count) != '0') { *(chp_str+ui_rev_idx) = *(chp_var1+ui_count); ui_rev_idx--; ui_count++; } *(chp_str+size) = '0'; return chp_str; } unsigned int get_str_len(char* chp_var) { unsigned int ui_count = 0; while (*chp_var != '0') { ui_count++; chp_var++; } return ui_count; } M.Tech – CSE – Weekend (2012 – 2015) Page 38
  • 39. Programming in C of College days 11. WAP to check that given string is palindrome or not? Program: #include <stdio.h> #include <stdbool.h> bool check(char* usr_str, char* chk_str); int main() { printf("WAP to check given string is Palindrome.n"); printf("nnn"); bool b_check; char* chp_var = malloc(sizeof(char) * 1024); char* chk_var = malloc(sizeof(char) * 1024); char* temp = chp_var; unsigned int ui_no_char = 0; printf("Enter string to check palindrome: "); while ((*chp_var = getchar())!='n') { *chk_var = *chp_var; chk_var++; chp_var++; ui_no_char++; } *chp_var = '0'; chp_var = temp; chk_var--; b_check = check(chp_var, chk_var); if (b_check) printf("nResult: %s, is palindrome.n", chp_var); else printf("nResult: %s, isn't palindrome.n", chp_var); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 39
  • 40. Programming in C of College days bool check(char* usr_str, char* chk_str) { unsigned int ui_count, ui_chk; ui_chk = ui_count = 0; while (*usr_str != '0') { ui_count++; if (*usr_str != *chk_str) { return false; } else { ui_chk++; } usr_str++; chk_str--; } if (ui_count == ui_chk) return true; } M.Tech – CSE – Weekend (2012 – 2015) Page 40
  • 41. Programming in C of College days 12. WAP to store the information about book‟s Title, Author, Page, & Price. Display the information by using pointer & structure with following restriction : a. Display elements by passing one element at time. b. By passing all elements at time. Program: #include <stdio.h> #include <stdbool.h> #include <ctype.h> #include <string.h> #define HEAD_SIZE 9 #define DIGIT_SIZE 9 #define STRING_LEN 30 void get_choice(void); bool store_info(void); bool disp_one_elem(void); bool disp_all_elem(void); char* int_to_alpha(int i_num); struct bk_detail { char* chp_stitle; char* chp_sname; int i_spage; int i_sprice; }; int main() { printf("WAP to store the information about book's Title, Author, Page, & Price. nDisplay the information by using pointer & structure with following restriction :"); printf("nt-> Display elements by passing one element at time. nt-> By passing all elements at time."); printf("nnn"); char ch_temp; FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); ch_temp = fgetc(fp); fclose(fp); if (ch_temp == -1) M.Tech – CSE – Weekend (2012 – 2015) Page 41
  • 42. Programming in C of College days { printf("No record inserted.n"); get_choice(); } else { printf("nBook Index File is ready for use.n"); get_choice(); } fclose(fp); printf("nnn"); system("pause"); return 0; } char* int_to_alpha(int i_num) { int i_count, i_sign, i_rev_count; char* chp_num = malloc(sizeof(char) * DIGIT_SIZE); char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE); i_sign = 0; i_count = i_rev_count = 0; if (i_num == 0) { *(chp_num + i_count) = '0'; i_count++; } else if (i_num < 0) { i_sign = -1; i_num *= -1; } while (i_num > 0) { *(chp_num + i_count) = i_num % 10 + '0'; i_count++; i_num /= 10; } if (i_sign < 0) { *(chp_num + i_count) = '-'; i_count++; } M.Tech – CSE – Weekend (2012 – 2015) Page 42
  • 43. Programming in C of College days *(chp_num + i_count) = '0'; i_count = 0; i_rev_count = strlen(chp_num)-1; while (*(chp_num + i_count) != '0') { *(chp_rev_num + i_count) = *(chp_num + i_rev_count); i_count++; i_rev_count--; } *(chp_rev_num + i_count) = '0'; return chp_rev_num; } void get_choice(void) { bool (*pfunc_seq[3])(); pfunc_seq[0] = store_info; pfunc_seq[1] = disp_one_elem; pfunc_seq[2] = disp_all_elem; printf("nYour choices for operation:n"); printf("nt a. To insert record (press) - 1n"); printf("nt b. To display complete info of the nt book (your selected name of book) (press) - 2n"); printf("nt a. To display all records (press) - 3n"); printf("nPlease Enter your choice: "); char ch_choice; if ((ch_choice = getchar()) == '1') { (*pfunc_seq[0])(); } else if (ch_choice == '2') { (*pfunc_seq[1])(); } else if (ch_choice == '3') { (*pfunc_seq[2])(); } else printf("nChoice is invalid.n"); } M.Tech – CSE – Weekend (2012 – 2015) Page 43
  • 44. Programming in C of College days bool store_info() { FILE* fp; fp = fopen("book_idx_mgr.txt", "a+"); printf("nTo store - records.n"); char* chp_bk_title = malloc(sizeof(char) * STRING_LEN); char* chp_bk_author = malloc(sizeof(char) * STRING_LEN); char* chp_bk_page = malloc(sizeof(char) * STRING_LEN); char* chp_bk_price = malloc(sizeof(char) * STRING_LEN); int i_temp; printf("Please Enter: n"); printf("tTitle: "); scanf("%s", chp_bk_title); fputs("Title: ", fp); fputs(chp_bk_title, fp); printf("tAuthor: "); scanf("%s", chp_bk_author); fputs("nAuthor: ", fp); fputs(chp_bk_author, fp); printf("tPage: "); scanf("%d", &i_temp); chp_bk_page = int_to_alpha(i_temp); fputs("nPage: ", fp); fputs(chp_bk_page, fp); printf("tPrice(only Integer): "); scanf("%d", &i_temp); chp_bk_price = int_to_alpha(i_temp); fputs("nPrice: ", fp); fputs(chp_bk_price, fp); char* chp_item_separator = "n----------------------------- ---------------n"; fputs(chp_item_separator, fp); fclose(fp); return true; } bool disp_one_elem() { M.Tech – CSE – Weekend (2012 – 2015) Page 44
  • 45. Programming in C of College days printf("nTo display the details of record as per your choice.n"); struct bk_detail s_bk_detail; s_bk_detail.chp_sname = malloc(sizeof(char) * STRING_LEN); s_bk_detail.chp_stitle = malloc(sizeof(char) * STRING_LEN); s_bk_detail.i_spage = 0; s_bk_detail.i_sprice = 0; char* chp_usr_book = malloc(sizeof(char) * STRING_LEN); printf("Please Enter the title of the book: "); scanf("%s", chp_usr_book); FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); char* chp_str = malloc(sizeof(char) * HEAD_SIZE); char* chp_prev_str = malloc(sizeof(char) * HEAD_SIZE); char ch_temp; int i_ch_count = 0; bool b_store = false; bool b_assign = false; while ((ch_temp = fgetc(fp))!= EOF) { if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp == '_')) { *(chp_str + i_ch_count) = ch_temp; i_ch_count++; } else if ((ch_temp == ':')||(ch_temp == 'n')) { *(chp_str + i_ch_count) = '0'; i_ch_count = 0; if (ch_temp == ':') { b_store = false; } else if (ch_temp == 'n') b_store = true; if (b_store) { if (strcmp(chp_str, chp_usr_book) == 0) { b_assign = true; M.Tech – CSE – Weekend (2012 – 2015) Page 45
  • 46. Programming in C of College days } if (b_assign) { if (strcmp(chp_prev_str, "Title") == 0) { s_bk_detail.chp_stitle = strcpy(s_bk_detail.chp_stitle, chp_str); printf("nTitle - %s", s_bk_detail.chp_stitle); } else if (strcmp(chp_prev_str, "Author") == 0) { s_bk_detail.chp_sname = strcpy(s_bk_detail.chp_sname, chp_str); printf("nAuthor - %s", s_bk_detail.chp_sname); } else if (strcmp(chp_prev_str, "Page") == 0) { int i_temp = atoi(chp_str); s_bk_detail.i_spage = i_temp; printf("nPage - %d", s_bk_detail.i_spage); } else if (strcmp(chp_prev_str, "Price") == 0) { int i_temp = atoi(chp_str); s_bk_detail.i_sprice = i_temp; printf("nPrice - %d", s_bk_detail.i_sprice); b_assign = false; } } } else { chp_prev_str = strcpy(chp_prev_str, chp_str); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 46
  • 47. Programming in C of College days fclose(fp); return true; } bool disp_all_elem() { printf("nTo display the details of record.n"); FILE* fp; fp = fopen("book_idx_mgr.txt", "r+"); char* chp_str = malloc(sizeof(char) * HEAD_SIZE); char ch_temp; int i_ch_count = 0; while ((ch_temp = fgetc(fp))!= EOF) { if ((isalpha(ch_temp))||(isdigit(ch_temp))||(ch_temp == ':')||(ch_temp == ' ')||(ch_temp == '_')) { *(chp_str + i_ch_count) = ch_temp; i_ch_count++; } else if (ch_temp == 'n') { *(chp_str + i_ch_count) = '0'; i_ch_count = 0; fputs(chp_str, stdout); fputs("n", stdout); } } fclose(fp); return true; } M.Tech – CSE – Weekend (2012 – 2015) Page 47
  • 48. Programming in C of College days 13. WAP to display the sum of entered number by writing on & reading from file. Program: #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <stdbool.h> #define DIGIT_SIZE 9 void write_file(FILE* fp); void read_file(FILE* fp); int main() { printf("WAP to write integer numbers to files, then separate even & odd numbers among them & store & display them in separate files."); printf("nnn"); FILE* fp; printf("Please Note: nt->Comma(','), nt->Blank-Space (' '), nt->Tab ('t') nt->and Enter ('n') are separators"); printf("nt->whereas Dot ('.') is terminator, nLimitation: Program will not accept any number more than 9 digits."); printf("nPlease use any of spearator before terminator."); printf("nnn"); printf("Please Enter numbers: "); write_file(fp); printf("nnn"); read_file(fp); system("pause"); return 0; } void write_file(FILE* fp) { fp = fopen("Integer_data.txt", "w+"); int* ip_usr_data; M.Tech – CSE – Weekend (2012 – 2015) Page 48
  • 49. Programming in C of College days ip_usr_data = malloc(sizeof(int)); char ch_temp_data; char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; bool b_check = false; while ((ch_temp_data = getchar()) != '.') { if (((ch_temp_data == ',')||(ch_temp_data == ' ')||(ch_temp_data == 't')||(ch_temp_data == 'n'))&&(b_check)) { b_check = false; *(ch_num + i_count) = '0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (*ip_usr_data) { fputs(ch_num, fp); fputs(", ", fp); } } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } else if (isalpha(ch_temp_data)) { printf("Invalid input."); } } fclose(fp); } void read_file(FILE* fp) { fp = fopen("Integer_data.txt", "r+"); FILE* fp_even; fp_even = fopen("Even_data.txt", "w+"); FILE* fp_odd; fp_odd = fopen("Odd_data.txt", "w+"); int* ip_usr_data; M.Tech – CSE – Weekend (2012 – 2015) Page 49
  • 50. Programming in C of College days ip_usr_data = malloc(sizeof(int)); char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; bool b_check = false; char ch_temp_data; while ((ch_temp_data = fgetc(fp)) != EOF) { if (((ch_temp_data == ',')||(ch_temp_data == ' ')||(ch_temp_data == 't')||(ch_temp_data == 'n'))&&(b_check)) { b_check = false; *(ch_num + i_count) = '0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (*ip_usr_data) { if ((*ip_usr_data % 2) == 0) { fputs(ch_num, fp_even); fputs(", ", fp_even); } else { fputs(ch_num, fp_odd); fputs(", ", fp_odd); } } } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } } if (fclose(fp_even) == 0) if (fclose(fp_odd) == 0) if (fclose(fp) == 0) printf("Normal termination.n"); else printf("Abnormal termination.n"); else printf("Abnormal termination.n"); M.Tech – CSE – Weekend (2012 – 2015) Page 50
  • 51. Programming in C of College days else printf("Abnormal termination.n"); } M.Tech – CSE – Weekend (2012 – 2015) Page 51
  • 52. Programming in C of College days 14. Write integer numbers to files, then separate even & odd numbers among them & store & display them in separate files. Program: #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <stdbool.h> #define DIGIT_SIZE 9 char* int_to_alpha(int i_num); void write_file(FILE* fp, int i_addend, int i_augend); void read_file(FILE* fp); int main() { printf("WAP to display the sum of entered number by writing on & reading from file."); printf("nnn"); printf("Limitation: Program will only accept intergral number of size 4 bytes."); printf("nnn"); int i_addend, i_augend; printf("Please Enter numbers: "); scanf("%d", &i_addend); printf("Please Enter numbers: "); scanf("%d", &i_augend); FILE* fp; write_file(fp, i_addend, i_augend); read_file(fp); system("pause"); return 0; } char* int_to_alpha(int i_num) { int i_count, i_sign, i_rev_count; char* chp_num = malloc(sizeof(char) * DIGIT_SIZE); char* chp_rev_num = malloc(sizeof(char) * DIGIT_SIZE); M.Tech – CSE – Weekend (2012 – 2015) Page 52
  • 53. Programming in C of College days i_sign = 0; i_count = i_rev_count = 0; if (i_num == 0) { *(chp_num + i_count) = '0'; i_count++; } else if (i_num < 0) { i_sign = -1; i_num *= -1; } while (i_num > 0) { *(chp_num + i_count) = i_num % 10 + '0'; i_count++; i_num /= 10; } if (i_sign < 0) { *(chp_num + i_count) = '-'; i_count++; } *(chp_num + i_count) = '0'; i_count = 0; i_rev_count = strlen(chp_num)-1; while (*(chp_num + i_count) != '0') { *(chp_rev_num + i_count) = *(chp_num + i_rev_count); i_count++; i_rev_count--; } *(chp_rev_num + i_count) = '0'; return chp_rev_num; } void write_file(FILE* fp, int i_addend, int i_augend) { fp = fopen("Summation.txt", "w+"); char* chp_addend = int_to_alpha(i_addend); M.Tech – CSE – Weekend (2012 – 2015) Page 53
  • 54. Programming in C of College days char* chp_augend = int_to_alpha(i_augend); fputs(chp_addend, fp); fputs(" + ", fp); fputs(chp_augend, fp); fputs(" = ", fp); fclose(fp); } void read_file(FILE* fp) { int i_addend, i_augend, i_result; fp = fopen("Summation.txt", "r+"); int* ip_usr_data; ip_usr_data = malloc(sizeof(int)); char* chp_result; char* ch_num = malloc(sizeof(char) * DIGIT_SIZE); int i_count = 0; int i_sign = 0; bool b_check = false; bool b_flag = false; char ch_temp_data; while ((ch_temp_data = fgetc(fp)) != EOF) { if ((ch_temp_data == ' ')&&(b_check)) { b_check = false; *(ch_num + i_count) = '0'; i_count = 0; *ip_usr_data = atoi(ch_num); if (i_sign < 0) { *ip_usr_data *= -1; i_sign = 0; } if (*ip_usr_data) { if (b_flag) { i_augend = *ip_usr_data; b_flag = false; M.Tech – CSE – Weekend (2012 – 2015) Page 54
  • 55. Programming in C of College days } else if (!b_flag) { i_addend = *ip_usr_data; } } } else if (isdigit(ch_temp_data)) { b_check = true; *(ch_num + i_count) = ch_temp_data; i_count++; } else if (ch_temp_data == '+') { b_flag = true; } else if (ch_temp_data == '-') { i_sign = -1; } else if (ch_temp_data == '=') { i_result = i_addend + i_augend; printf("Addend: %dn", i_addend); printf("Augend: %dn", i_augend); chp_result = int_to_alpha(i_result); printf("Summation-: %sn", chp_result); } } fputs(chp_result, fp); if (fclose(fp) == 0) printf("Normal termination.n"); else printf("Abnormal termination.n"); } M.Tech – CSE – Weekend (2012 – 2015) Page 55
  • 56. Programming in C of College days 15. WAP to draw the given pattern as per user value. * ** *** **** ***** Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t*n"); printf("t**n"); printf("t***n"); printf("t****n"); printf("t*****n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); for(ui_col = 1; ui_col <= ui_row; ++ui_col) printf("*"); } printf("nnn"); } M.Tech – CSE – Weekend (2012 – 2015) Page 56
  • 57. Programming in C of College days int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 57
  • 58. Programming in C of College days 16. WAP to draw the given pattern as per user value. 1 12 123 1234 12345 Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t1n"); printf("t12n"); printf("t123n"); printf("t1234n"); printf("t12345n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); for(ui_col = 1; ui_col <= ui_row; ++ui_col) printf("%d", ui_col); } } int main() { set_argument(); M.Tech – CSE – Weekend (2012 – 2015) Page 58
  • 59. Programming in C of College days draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 59
  • 60. Programming in C of College days 17. WAP to draw the given pattern as per user value. 1 21 321 4321 54321 Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t 1n"); printf("t 21n"); printf("t 321n"); printf("t 4321n"); printf("t54321n"); printf("n"); printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col, ui_count; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col) printf(" "); unsigned int ui_num_count = (i_number - ui_blank_count); for(ui_count = ui_num_count; ui_count >= 1; --ui_count) printf("%d", ui_count); M.Tech – CSE – Weekend (2012 – 2015) Page 60
  • 61. Programming in C of College days } } int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 61
  • 62. Programming in C of College days 18.WAP to draw the given pattern as per user value. 5 54 543 5432 54321 Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t5n"); printf("t54n"); printf("t543n"); printf("t5432n"); printf("t54321n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); int i_count = i_number; for(ui_col = 1; ui_col <= ui_row; ++ui_col, --i_count) printf("%d", i_count); } } M.Tech – CSE – Weekend (2012 – 2015) Page 62
  • 63. Programming in C of College days int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 63
  • 64. Programming in C of College days 19. WAP to draw the given pattern as per user value. 5 45 345 2345 12345 Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t 5n"); printf("t 45n"); printf("t 345n"); printf("t 2345n"); printf("t12345n"); printf("n"); printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col, ui_count; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_col = 1; ui_col <= ui_blank_count; ++ui_col) printf(" "); int i_count = ui_blank_count; for(ui_count = 1; ui_count <= ui_row; ++ui_count, ++i_count) M.Tech – CSE – Weekend (2012 – 2015) Page 64
  • 65. Programming in C of College days printf("%d", (i_count+1)); } } int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 65
  • 66. Programming in C of College days 20. WAP to draw the given pattern as per user value. 54321 5432 543 54 5 Program: #include <stdio.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t54321n"); printf("t5432n"); printf("t543n"); printf("t54n"); printf("t5n"); printf("n"); printf("Please Enter a single digit number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col; for(ui_row = 1; ui_row <= i_number; ++ui_row) { printf("nt"); for(ui_col = i_number; ui_col >= ui_row; --ui_col) printf("%d", ui_col); } } M.Tech – CSE – Weekend (2012 – 2015) Page 66
  • 67. Programming in C of College days int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 67
  • 68. Programming in C of College days 21. WAP to draw the given pattern as per user value. 5 545 54345 5432345 543212345 Program: #include <stdio.h> #include <stdbool.h> int i_number; void set_argument() { printf("***To Draw The given pattern as per user value.***nnn"); printf("n"); printf("t 5n"); printf("t 545n"); printf("t 54345n"); printf("t 5432345n"); printf("t543212345n"); printf("n"); printf("Please Enter a number to draw pattern: "); scanf("%d", &i_number); printf("nnn"); } void draw_pattern() { unsigned int ui_row, ui_col, ui_blank; for(ui_row = 0; ui_row <= i_number; ++ui_row) { printf("nt"); unsigned int ui_blank_count = (i_number - ui_row); for(ui_blank = 1; ui_blank <= ui_blank_count; ++ui_blank) printf(" "); bool b_flag_f = true; bool b_flag_b = true; M.Tech – CSE – Weekend (2012 – 2015) Page 68
  • 69. Programming in C of College days for(ui_col = (2*ui_row + 1); ui_col >= 1; --ui_col) { if (((ui_col%2)==0)&&(b_flag_f)) { unsigned int ui_count; int i_count = i_number; for(ui_count = 1; ui_count <= (ui_col/2); ++ui_count, --i_count) printf("%d", i_count); b_flag_f = false; } else if(((ui_col%2)==0)&&(!b_flag_f)&&(b_flag_b)) { unsigned int ui_count; int i_count = i_number - (ui_col/2) +1; for (ui_count = (ui_col/2); ui_count >= 1; -- ui_count, ++i_count) printf("%d", i_count); b_flag_b = false; } } } } int main() { set_argument(); draw_pattern(); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 69
  • 70. Programming in C of College days 22. WAP to pass array element one by one using pointer and function. Program: #include <stdio.h> #define SIZE_ARRAY 3 void print_item(int* item); int main() { printf("WAP to pass array element one by one using pointer and function.n"); printf("nnn"); int storage[SIZE_ARRAY][SIZE_ARRAY]; printf("Given order of array is %d * %dn", SIZE_ARRAY, SIZE_ARRAY); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < SIZE_ARRAY; ++i_loop_count) for (j_loop_count = 0; j_loop_count < SIZE_ARRAY; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &storage[i_loop_count][j_loop_count]); } printf("nnn"); printf("Output as Array-element:n"); for (i_loop_count = 0; i_loop_count < SIZE_ARRAY; ++i_loop_count) for (j_loop_count = 0; j_loop_count < SIZE_ARRAY; ++j_loop_count) print_item(&storage[i_loop_count][j_loop_count]); printf("nnn"); system("pause"); return 0; } M.Tech – CSE – Weekend (2012 – 2015) Page 70
  • 71. Programming in C of College days void print_item(int* i_item) { static int i_count = 1; printf("%d ", *i_item); if ((i_count%3)==0) printf("n"); i_count++; } M.Tech – CSE – Weekend (2012 – 2015) Page 71
  • 72. Programming in C of College days 23. WAP to find (+)ive, (-)ive, even, odd, prime element from array. Program: #include <stdio.h> #include <stdbool.h> #define SIZE_ARRAY 3 void print_pos_item(int** item, int i_row, int i_col); void print_neg_item(int** item, int i_row, int i_col); void print_evn_item(int** item, int i_row, int i_col); void print_odd_item(int** item, int i_row, int i_col); void print_prime_item(int** item, int i_row, int i_col); int main() { printf("WAP to find (+)ive, (-)ive, even, odd, prime element from array.n"); printf("nnn"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("nEnter number of Rows: "); scanf("%d", &i_row); printf("nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: nn", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); M.Tech – CSE – Weekend (2012 – 2015) Page 72
  • 73. Programming in C of College days } printf("nnn"); print_pos_item(ip_storage, i_row, i_col); printf("nnn"); print_neg_item(ip_storage, i_row, i_col); printf("nnn"); print_evn_item(ip_storage, i_row, i_col); printf("nnn"); print_odd_item(ip_storage, i_row, i_col); printf("nnn"); print_prime_item(ip_storage, i_row, i_col); printf("nnn"); system("pause"); return 0; } void print_pos_item(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of (+)ive items:n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count) >= 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } void print_neg_item(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of (-)ive items:n"); M.Tech – CSE – Weekend (2012 – 2015) Page 73
  • 74. Programming in C of College days for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count) < 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } void print_evn_item(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of even items:n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if (((*(*(ip_storage + i_loop_count) + j_loop_count)%2) == 0)) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } void print_odd_item(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of odd items:n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { if ((*(*(ip_storage + i_loop_count) + j_loop_count)%2) != 0) printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } M.Tech – CSE – Weekend (2012 – 2015) Page 74
  • 75. Programming in C of College days void print_prime_item(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; bool b_check; printf("List of prime items:n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { int i_count; for(i_count = 2; i_count <= (i_count/2); ++i_count) { if((*(*(ip_storage + i_loop_count) + j_loop_count) % i_count) == 0) { b_check = true; break; } else b_check = false; } if(!b_check) { printf("%d, ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 75
  • 76. Programming in C of College days 24. WAP to display array element in row-major and column-mjor technique. Program: #include <stdio.h> #include <stdbool.h> void row_major_tech(int** item, int i_row, int i_col); void col_major_tech(int** item, int i_row, int i_col); int main() { printf("WAP to display array element in row-major and column-mjor technique.n"); printf("nnn"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("nEnter number of Rows: "); scanf("%d", &i_row); printf("nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: nn", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); } printf("nnn"); row_major_tech(ip_storage, i_row, i_col); M.Tech – CSE – Weekend (2012 – 2015) Page 76
  • 77. Programming in C of College days printf("nnn"); col_major_tech(ip_storage, i_row, i_col); printf("nnn"); system("pause"); return 0; } void row_major_tech(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of items as Row-Major technique:n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } } void col_major_tech(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; printf("List of items as Column-Major technique:n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 77
  • 78. Programming in C of College days 25. WAP to transpose a given matrix using pointer and function. Program: #include <stdio.h> #include <stdbool.h> #define SIZE_ARRAY 3 void print_trans_matrix(int** item, int i_row, int i_col); int main() { printf("WAP to transpose a given using pointer and function.n"); printf("nnn"); int i_row, i_col; printf("Please enter the Order of Mtrix."); printf("nEnter number of Rows: "); scanf("%d", &i_row); printf("nEnter number of Columns: "); scanf("%d", &i_col); int** ip_storage = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(ip_storage + i_count) = malloc(sizeof(int) * i_col); printf("Please enter %d items for corresponding rows and columns of storage: nn", (i_row * i_col)); int i_loop_count, j_loop_count; for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) for (j_loop_count = 0; j_loop_count < i_col; ++j_loop_count) { printf("Enter item for array[%d][%d]: ",i_loop_count, j_loop_count); scanf("%d", &(*(*(ip_storage + i_loop_count) + j_loop_count))); } printf("nnn"); M.Tech – CSE – Weekend (2012 – 2015) Page 78
  • 79. Programming in C of College days print_trans_matrix(ip_storage, i_row, i_col); printf("nnn"); system("pause"); return 0; } void print_trans_matrix(int** ip_storage, int i_row, int i_col) { int i_loop_count, j_loop_count; int** trans_mat = malloc(sizeof(int) * i_row * i_col); int i_count; for (i_count = 0; i_count < i_row; ++i_count) *(trans_mat + i_count) = malloc(sizeof(int) * i_col); printf("nMatrix You entered:"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { printf("n"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("%d ", *(*(ip_storage + i_loop_count) + j_loop_count)); } } printf("nMatrix after Transpose:"); for (j_loop_count = 0; j_loop_count < i_row; ++j_loop_count) { printf("n"); for (i_loop_count = 0; i_loop_count < i_row; ++i_loop_count) { *(*(trans_mat + j_loop_count) + i_loop_count) = *(*(ip_storage + i_loop_count) + j_loop_count); printf("%d ", *(*(trans_mat + j_loop_count) + i_loop_count)); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 79
  • 80. Programming in C of College days 26. WAP to display the mathematical table of given number. Program: #include <stdio.h> void disp_mult_table_range(int i_lr, int i_hr); int main() { printf("WAP to display the mathematical table of given range of numbers.n"); printf("nnn"); int i_lr, i_hr; printf("Please Enter lower limit: "); scanf("%d", &i_lr); printf("Please Enter higher limit: "); scanf("%d", &i_hr); disp_mult_table_range(i_lr, i_hr); printf("nnn"); system("pause"); return 0; } void disp_mult_table_range(int i_lr, int i_hr) { int i_multiplicand, i_multiplier; for (i_multiplier = i_lr; i_multiplier <= i_hr; ++i_multiplier) { printf("nnn"); printf("Multiplication Table of %d:n", i_multiplier); for (i_multiplicand = 1; i_multiplicand <= 10; ++i_multiplicand) { printf("t%d * %d = %dn", i_multiplier, i_multiplicand, (i_multiplier * i_multiplicand)); } } } M.Tech – CSE – Weekend (2012 – 2015) Page 80
  • 81. Programming in C of College days 27. WAP to display the mathematical table of given number. Program: #include <stdio.h> void disp_mult_table(int i_num); int main() { printf("WAP to display the mathematical table of given number.n"); printf("nnn"); int i_num; printf("Please Enter number: "); scanf("%d", &i_num); disp_mult_table(i_num); printf("nnn"); system("pause"); return 0; } void disp_mult_table(int i_num) { int i_multiplicand; for (i_multiplicand = 1; i_multiplicand <= 10; ++i_multiplicand) { printf("t%d * %d = %dn", i_num, i_multiplicand, (i_num * i_multiplicand)); } } M.Tech – CSE – Weekend (2012 – 2015) Page 81
  • 82. Programming in C of College days 28. WAP to display alphabets „a‟ to „z‟ and “A” to “Z” a. Using pointers. b. Without using pointers. Program: a. Using pointers. #include <stdio.h> #include <stdbool.h> void disp_lower_case_alphabets_wp(void); void disp_upper_case_alphabets_wp(void); int main() { printf("WAP to display a to z and A to Z by using pointer.n"); printf("nnn"); disp_lower_case_alphabets_wp(); printf("nnn"); disp_upper_case_alphabets_wp(); printf("nnn"); system("pause"); return 0; } void disp_lower_case_alphabets_wp(void) { printf("Displaying lower - case alphabets:-n"); char* chp_var = "a"; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("n"); printf("%ct", *chp_var + ui_loop_count); } } M.Tech – CSE – Weekend (2012 – 2015) Page 82
  • 83. Programming in C of College days void disp_upper_case_alphabets_wp(void) { printf("Displaying lower - case alphabets:-n"); char* chp_var = "A"; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("n"); printf("%ct", *chp_var + ui_loop_count); } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ a. Without using pointers. #include <stdio.h> #include <stdbool.h> void disp_lower_case_alphabets(void); void disp_upper_case_alphabets(void); int main() { printf("WAP to display a to z and A to Z by using pointer.n"); printf("nnn"); disp_lower_case_alphabets(); printf("nnn"); disp_upper_case_alphabets(); printf("nnn"); system("pause"); return 0; } void disp_lower_case_alphabets(void) { printf("Displaying lower - case alphabets:-n"); char chp_var = 'a'; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) M.Tech – CSE – Weekend (2012 – 2015) Page 83
  • 84. Programming in C of College days { if (ui_loop_count%10 == 0) printf("n"); printf("%ct", chp_var + ui_loop_count); } } void disp_upper_case_alphabets(void) { printf("Displaying lower - case alphabets:-n"); char chp_var = 'A'; unsigned int ui_loop_count; for (ui_loop_count = 0; ui_loop_count < 26; ++ui_loop_count) { if (ui_loop_count%10 == 0) printf("n"); printf("%ct", chp_var + ui_loop_count); } } M.Tech – CSE – Weekend (2012 – 2015) Page 84
  • 85. Programming in C of College days 29. WAP to take character sequence (string) and diplay it, using pointer and functions. Program: #include <stdio.h> #include <stdbool.h> #define MAX_SEQL_SIZE 1024 char* get_char_seql(void); void disp_char_seql(char*); int main() { printf("WAP to take character sequence (string) and diplay it, using pointer and functions.n"); printf("nnn"); printf("Please press 'dot'(.) as terminating character."); printf("nnn"); char* chp_seql; chp_seql = get_char_seql(); printf("nnn"); disp_char_seql(chp_seql); printf("nnn"); system("pause"); return 0; } char* get_char_seql(void) { printf("Enter your character sequel:- "); char* chp_var = (char*)malloc(sizeof(char) * MAX_SEQL_SIZE); char ch_temp; unsigned int ui_count = 0; while((ch_temp = getchar()) != '.') { *(chp_var + ui_count) = ch_temp; ui_count++; } *(chp_var + ui_count) = '0'; M.Tech – CSE – Weekend (2012 – 2015) Page 85
  • 86. Programming in C of College days return chp_var; } void disp_char_seql(char* chp_seql) { printf("Character sequel you entered:- %s", chp_seql); } M.Tech – CSE – Weekend (2012 – 2015) Page 86
  • 87. Programming in C of College days Note:  For Visual Studio 2008 and Qt (version- 4.2) you need to format the code as, make all the initialization throughout the function to very initial steps after opening braces. M.Tech – CSE – Weekend (2012 – 2015) Page 87