INS KURSURA SUBMARINE MUSEUM.



INS KURSURA SUBMARINE MUSEUM visakapatnam at RK Beach, Inside and the out side pictures of SUBMARINE

Case Studies of C-prog

Building a Sorted List

The program in fig. 13.11 can be used to create a sorted list.  This is possible by creating ‘one item’ list using the create function and then inserting the remaining items one after another using insert function.

A new program that would build a sorted list from a given list of numbers is shown in Fig. 13.12.  The main function creates a ‘base node’ using the first number in the list and then calls the function insert_sort repeatedly to build the entire sorted list.  It uses the same sorting algorithm discussed above but does not use any dummy node.  Note that the last item points to NULL.


CREATION OF SORTED LIST FROM A GIVEN LIST OF NUMBERS

Program

#include <stdio.h>
#include <stdlib.h>
#define NULL 0

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

main ()
{
   int n;
   node *head = NULL;
   void print(node *p);
   node *insert_Sort(node *p, int n);

   printf(“Input the list of numbers.\n”);
   printf(“At end, type –999.\n”);
   scanf(“%d”,&n);

   while(n != -999)
   {
     if(head == NULL)     /* create ‘base’ node */
     {
           head = (node *)malloc(sizeof(node));
           head ->number = n;
           head->next = NULL;
    
     }

     else                 /* insert next item */
     {
           head = insert_sort(head,n);
     }
       scanf(“%d”, &n);
   }
   printf(“\n”);
   print(head);
   print(“\n”);
}
node *insert_sort(node *list, int x)
{
   node *p1, *p2, *p;
   p1 = NULL;
   p2 = list; /* p2 points to first node */

   for( ; p2->number < x ; p2 = p2->next)
   {
     p1 = p2;

     if(p2->next == NULL)
     {
     p2 = p2->next;                  /* p2 set to NULL */
     break;                  /* insert new node at end */
   }
 }

/* key node found */
 p = (node *)malloc(sizeof(node)); /* space for new node */
 p->number = x;      /* place value in the new node */
 p->next = p2;       /* link new node to key node */
if (p1 == NULL)
   list = p;    /* new node becomes the first node */
 else
   p1->next = p;  /* new node inserted after 1st node */
 return (list);
}
void print(node *list)
{
   if (list == NULL)
     printf(“NULL”);
   else
   {
     printf(“%d-->”,list->number);
     print(list->next;
   }
   return;
}


Output
Input the list of number.
At end, type – 999.
80 70 50 40 60 –999
40-->50-->60-->70-->80 -->NULL
Input the list of number.
At end, type –999.
40 70 50 60 80 –999

40-->50-->60-->70-->80-->NULL

Insertion in a Sorted List

The task of inserting a value into the current location in a sorted linked list involves two operations:

1.     Finding the node before which the new node has to be inserted.   We call this node as ‘Key node’.
2.     Creating a new node with the value to be inserted and inserting the new node by manipulating pointers appropriately.

In order to illustrate the process of insertion, we use a sorted linked list created by the create function discussed in Example 13.3.  Figure 13.11 shows a complete program that creates a list (using sorted input data) and then inserts a given value into the correct place using function insert.

INSERTING A NUMBER IN A SORTED LIST
Program

#include <stdio.h>
#include<stdio.h>
#define NULL 0

struct linked_list
{
   int number;
   struct linked-list *next;
};
typedef struct linked_lit node;

main()
{
   int n;
   node *head;
   void create(node *p);
   node *insert(node *p, int n);
   void print(node *p);
   head = (node *)malloc(sizeof(node));
   create(head);
   printf(“\n”);
   printf(“Original list: “);
   print(head);
   printf(“\n\n”);
   printf(“Input number to be inserted: “);
   scanf(“%d”, &n);

   head = inert(head,n);
   printf(“\n”);
   printf(“New list:  “);
   print(head);
}
void create(node *list)
{
   printf(“Input a number \n”);
   printf(“(type –999 at end): “);
   scanf(“%d”, &list->number);

   if(list->number == -999)
   {
     list->next = NULL;
   }
   else    /* create next node */
   {
     list->next = (node *)malloc(sizeof(node));
     create(list->next);
   }
   return:
}

void print(node *list)
{
   if(list->next != NULL)
   {
     printf(“%d -->”, list->number);

   if(list ->next->next = = NULL)
     printf(“%d”, list->next->number);

   print(list->next);
   }
   return:
}

node *insert(node *head, int x)
{
   node *p1, *p2, *p;
   p1 = NULL;
   p2 = head;  /* p2 points to first node */

   for( ; p2->number < x; p2 = p2->next)
   {
     p1 = p2;

     if(p2->next->next == NULL)
        {
           p2 = p2->next;   /* insertion at end */
           break;
        }
    }

    /*key node found and insert new node */

     p = (node )malloc(sizeof(node));  / space for new node */

     p->number = x; /* place value in the new node */

     p->next = p2; /*link new node to key node */

     if (p1 == NULL)
           head = p; /* new node becomes the first node */
     else
           p1->next = p; /* new node inserted in middle */

     return (head);
}


Output
Input a number
(type –999 at end ); 10

Input a number
(type –999 at end ); 20

Input a number
(type –999 at end ); 30

Input a number
(type –999 at end ); 40

Input a number
(type –999 at end ); -999

Original list:      10 -->20-->30-->40-->-999

Input number to be inserted: 25

New list: 10-->20-->25-->30-->40-->-999

Inventory Updating

The price and quantity of items stocked in a store changes every day.  They may either increase or decrease.  The program in Fig.11.15 reads the incremental values of price and quantity and computes the total value of the items in stock.

The program illustrates the use of structure pointers as function parameters.  &item, the address of the structure item, is passed to the functions update() and mul().  The formal arguments product and stock, which receive the value of &item, are declared as pointers of type struct stores.

STRUCTURES AS FUNCTION PARAMETERS
Using structure pointers
Program
   struct stores                                                   
   {                                                               
        char  name[20];                                            
        float price;                                               
        int   quantity;                                            
   };                                                               
   main()                                                          
   {                                                               
        void update(struct stores *, float, int);                                             
        float         p_increment, value;                   
        int           q_increment;                                 
                                                                   
        struct stores item = {"XYZ", 25.75, 12};            
        struct stores *ptr = &item;                                
                                                                   
        printf("\nInput increment values:");                       
        printf(" price increment and quantity increment\n");       
        scanf("%f %d", &p_increment, &q_increment);                
                                                                   
   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - */     
        update(&item, p_increment, q_increment);                   
   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - */     
        printf("Updated values of item\n\n");                      
        printf("Name      : %s\n",ptr->name);                      
        printf("Price     : %f\n",ptr->price);                     
        printf("Quantity  : %d\n",ptr->quantity);                  
                                                                   
   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - */     
        value  = mul(&item);                                       
   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - */     
        printf("\nValue of the item  =  %f\n", value);             
   }

  void update(struct stores *product, float p, int q)                                      
   {                                                               
        product->price += p;                                       
        product->quantity += q;                                     
   }                                                               
   float mul(struct stores *stock)                   
   {                                                               
        return(stock->price * stock->quantity);                    
   }                                                               
                                                                   
Output                                                          

   Input increment values: price increment and quantity increment  
   10 12                                                           
   Updated values of item                                          
                                                                   
   Name      : XYZ                                                 
   Price     : 35.750000                                           
   Quantity  : 24                                                  
                                                                    
   Value of the item  =  858.000000         

Processing of Examination Marks

Marks obtained by a batch of students in the Annual Examination are tabulated as follows:

         Student name                       Marks obtained

           S.Laxmi                           45  67  38  55     
           V.S.Rao                           77  89  56  69
            - -                                     -   -   -   -

It is required to compute the total marks obtained by each student and print the rank list based on the total marks.

The program in Fig.11.14 stores the student names in the array name and the marks in the array marks.  After computing the total marks obtained by all the students, the program prepares and prints the rank list.  The declaration

    int  marks[STUDENTS][SUBJECTS+1];

defines marks as a pointer to the array's first row.  We use rowptr as the pointer to the row of marks.  The rowptr is initialized as follows:

    int (*rowptr)[SUBJECTS+1] = array;

Note that array is the formal argument whose values are replaced by the values of the actual argument marks.  The parentheses around *rowptr makes the rowptr as a pointer to an array of SUBJECTS+1 integers.  Remember, the statement

         int  *rowptr[SUBJECTS+1];

would declare rowptr as an array   of  SUBJECTS+1 elements.

When we increment the rowptr (by rowptr+1), the incrementing is done in units of the size of each row of array, making rowptr point to the next row.  Since rowptr points to a particular row, (*rowptr)[x] points to the xth element in the row.

                POINTERS AND TWO-DIMENSIONAL ARRAYS          
Program
  #define  STUDENTS  5                                            
  #define  SUBJECTS  4                                            
  #include <string.h>                                              
                                                                   
  main()                                                          
  {                                                               
    char name[STUDENTS][20];                                    
    int  marks[STUDENTS][SUBJECTS+1];                   
                                                                   
    printf("Input students names & their marks in four subjects\n");
    get_list(name, marks, STUDENTS, SUBJECTS);                  
    get_sum(marks, STUDENTS, SUBJECTS+1);                       
    printf("\n");                                               
    print_list(name,marks,STUDENTS,SUBJECTS+1);                 
    get_rank_list(name, marks, STUDENTS, SUBJECTS+1);           
    printf("\nRanked List\n\n");                                
    print_list(name,marks,STUDENTS,SUBJECTS+1); 
   }                                                               
/*   Input student name and marks        */                     
   get_list(char *string[ ],                                  
            int array [ ] [SUBJECTS +1], int m, int n)                                      
   {
       int   i, j, (*rowptr)[SUBJECTS+1] = array;                  
       for(i = 0; i < m; i++)                                      
       {                                                           
          scanf("%s", string[i]);                                  
          for(j = 0; j < SUBJECTS; j++)                            
             scanf("%d", &(*(rowptr + i))[j]);                     
       }                                                           
   }
   /*    Compute total marks obtained by each student   */          
   get_sum(int array [ ] [SUBJECTS +1], int m, int n)            
   {                                                               
       int   i, j, (*rowptr)[SUBJECTS+1] = array;                  
       for(i = 0; i < m; i++)                                       
       {
          (*(rowptr + i))[n-1] = 0;                                
          for(j =0; j < n-1; j++)                                  
             (*(rowptr + i))[n-1] += (*(rowptr + i))[j];           
       }                                                            
   }                                                               
                                                                   
                                                                   
   /*    Prepare rank list based on total marks      */            
                                                                   
   get_rank_list(char *string [ ],                                 
                 int array [ ] [SUBJECTS + 1]                      
                 int m,                                          
                 int n)                                   
   {                                                               
     int i, j, k, (*rowptr)[SUBJECTS+1] = array;                 
     char *temp;                                                 
                                                                   
     for(i = 1; i <= m-1; i++)                                   
        for(j = 1; j <= m-i; j++)                                
           if( (*(rowptr + j-1))[n-1] < (*(rowptr + j))[n-1])    
           {                                                     
            swap_string(string[j-1], string[j]);                
                                                                    
            for(k = 0; k < n; k++)                              
            swap_int(&(*(rowptr + j-1))[k],&(*(rowptr+j))[k]);
             }                                                     
   }                                                               
   /*      Print out the ranked list            */                 
   print_list(char *string[ ],                                  
              int array [] [SUBJECTS + 1],                       
              int m,                                              
              int n)                                      
   {                                                               
       int  i, j, (*rowptr)[SUBJECTS+1] = array;                    
       for(i = 0; i < m; i++)                                      
       {                                                           
          printf("%-20s", string[i]);                              
          for(j = 0; j < n; j++)                                   
             printf("%5d", (*(rowptr + i))[j]);                    
             printf("\n");                                         
       }                                                           
   }                                                                
   /*     Exchange of integer values              */               
   swap_int(int *p, int *q)                                
   {                                                                
       int  temp;                                                  
       temp = *p;                                                  
       *p   = *q;                                                  
       *q   = temp;                                                 
   }   




   /*     Exchange of strings         */                           
   swap_string(char s1[ ], char s2[ ])                    
   {                                                               
       char  swaparea[256];                                        
       int   i;                                                    
       for(i = 0; i < 256; i++)                                    
          swaparea[i] = '\0';                                      
       i = 0;                                                      
       while(s1[i] != '\0' && i < 256)                             
       {                                                           
          swaparea[i] = s1[i];                                      
          i++;                                                     
       }                                                           
       i = 0;                                                      
       while(s2[i] != '\0' && i < 256)                             
       {                                                           
          s1[i] = s2[i];                                           
          s1[++i] = '\0';                                          
       }                                                            
       i = 0;                                                      
       while(swaparea[i] != '\0')                                  
       {                                                            
          s2[i] = swaparea[i];                                     
          s2[++i] = '\0';                                          
       }                                                           
   }                                                                
                                                                   
Output                                                          
                                                                   
   Input students names & their marks in four subjects             
   S.Laxmi 45 67 38 55                                             
   V.S.Rao 77 89 56 69                                             
   A.Gupta 66 78 98 45                                             
   S.Mani 86 72 0 25                                               
   R.Daniel 44 55 66 77                                            
   S.Laxmi                45   67   38   55  205                   
   V.S.Rao                77   89   56   69  291                    
   A.Gupta                66   78   98   45  287                   
   S.Mani                 86   72    0   25  183                   
   R.Daniel               44   55   66   77  242                   
                                                                    
   Ranked List                                                     
   V.S.Rao                77   89   56   69  291                   
   A.Gupta                66   78   98   45  287                   
   R.Daniel               44   55   66   77  242                   
   S.Laxmi                45   67   38   55  205                   

   S.Mani                 86   72    0   25  183 

Copyright © EduRAR @ www.edurar.blogspot.in