CS101 Tutorial  
-------------

Topic : Dynamic Memory Allocation and Pointers
TA    : Srinivas Samala
        srinus@cse.iitb.ac.in

1) Write a program to add two  matrices. No. of rows and  No. of columns should be input to the program.
    
    program matrix
        integer,allocatable,dimension(:,:) :: A
        integer,allocatable,dimension(:,:) :: B
        integer istat
        print *, "Enter the order of matrices"
        print *,"No of Rows"
        read *,m
        print *,"No of Columns"
        read *,n

        allocate(A(m,n),stat=istat)
        allocate(B(m,n),stat=istat)
        print *, "enter matrix 1"
        read *, ((A(i,j),j=1,n),i=1,m)
        print *, "enter matrix 2"
        read *, ((B(i,j),j=1,n),i=1,m)

        do i=1,m
                do j=1,n
                A(i,j)=A(i,j)+B(i,j)

                end do
                print *, (A(i,j),j=1,n)
        end do

    end program


2) Can we define a derived type as below? 

        Type ::  struct
                integer :: rollno
                integer,allocatable,dimension(:) :: course_no
        end Type


  Ans:No. Allocatable arrays can't be defined as components of structures.

3) Identify errors  in  the following programs. 

a)    
        Program one
                real,pointer :: ptr
                integer :: a=2
                ptr => a
                print *,ptr
                nullify(ptr)
        end program

  a)      Address of integer variable can't be assigned to a 'real' pointer and the variable shoud be defined as 'target'.



b) 
        program three
                    integer,pointer :: p1,p2
                    integer,target  :: a,b
                    p1 =>a
                    p1 = 20
                    p2 = 30
                       print *,p1,p2
                nullify(p1,p2)
        end program


       Pointer 'p2' is not assigned any address. Segmentation fault occurs.

4) How     allocatable array and linked list differ? 
   We can declare the size of an  allocatable array dynamically at run time
but once the  memory is allocated its size can't be altered. In case of
linked list, the size can be increased/decreased  at any time during the execution.


5) Define a linked list structure containing student rollno, name and marks as components.
    Write subroutines to
 
    a)add a new student information

    b)Find the student with maximum marks

    c)delete an existing entry   

Here is the Program
6) In a  doubly linked list, each node  will have pointers to both its predecessor and successor.

    a) Write a program to create  a doubly linked list.

    b) Write subroutines to  add nodes and delete nodes.

    This is exercise for you.