Sunday 19 March 2017

Pointers And Arrays

Data Structures and Algorithms






DEPARTMENT OF COMPUTER SOFTWARE ENGINEERING




PREFACE
This lab manual has been prepared to facilitate the students of software engineering in studying and analyzing data structures and algorithms. The aim of this course is to provide an introduction to computer algorithms and data structures, with an emphasis on practical implementations. Data Structures and Algorithms introduces fundamental techniques for problem solving that are relevant to most areas of computer science, both theoretical and applied. Algorithms and data structures for sorting, searching, graph problems, and geometric problems are covered. The lab manual is designed to help students understand the basic concepts of data structures and implement basic computer science algorithms in object-oriented approach.
PREPARED BY
Lab manual is prepared by Dr. Naima Iltaf and Lab Engr. Misbah Ghalib under the supervision of Head of Department Dr. Naveed Iqbal Rao in year 2014.
GENERAL INSTRUCTIONS
a.       Students are required to maintain the lab manual with them till the end of the semester.
b.      All readings, answers to questions and illustrations must be solved on the place provided. If more space is required then additional sheets may be attached.
c.       It is the responsibility of the student to have the manual graded before deadlines as given by the instructor
d.      Loss of manual will result in re submission of the complete manual.
e.       Students are required to go through the experiment before coming to the lab session. Lab session details will be given in training schedule.
f.       Students must bring the manual in each lab.
g.      Keep the manual neat clean and presentable.
h.      Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized and no re submission will be entertained.
i.        Marks will be deducted for late submission.


VERSION HISTORY
Date
Updated By
Details
September 2012
Asst Prof Bilal Rauf
First Version Created
August 2014
Dr Naima Iltaf
Updated Experiment 1 and Experiment 3
September 2014
Lab Engr. Misbah Ghalib
Added resources and summaries to the experiments. Formatted the manual according to standard.
January 2016
Aamna Mehfooz
All lab sessions are updated with different set of exercises and formatted the manual according to recent Standard.





















MARKS

Exp #
Date Conducted
Experiment Title
Max. Marks
Marks Obtained
Instructor Sign
1





2





3





4





5





6





7





8





9





10





11





12





13





14























Grand Total






EXPERIMENT 1 – REVISION OF C++ POINTERS AND ARRAYS (CLO 1)


1.
Objectives:
       (a).       Revise arrays and use of arrays with pointers
       (b).       To learn basic terminologies and syntax to use pointers in C++

2.
Time Required: 3 hrs

3.
Software Required:
       (a).       Windows OS
       (b).       Borland/Bloodshed DevC++/ Microsoft Visual Studio 2012

4.
Constructors:
It is convenient if an object can initialize itself when it is first created, without need to make a separate call to its member functions. C++ provides a non-static member function called as constructor that is automatically called when object is created. After objects are created, their members can be initialized by using constructors.

·         Important points to be noted while using constructors
·         Constructor name must be the same as that of its class name in which it belongs.
·         Constructors are invoked automatically when objects are created.
·         Constructors cannot specify return types nor return values.
·         Constructors cannot be declared static or const.
·         Constructors should have public access within the class.

5.
Destructors:
A destructor is a member function that is called automatically when an object is destroyed.
~class name ();
Where the class name is the name of the destructor and is preceded by a tilde (~) symbol.

Activity 1.1 : Class with Constructor
# include<iostream.h>
class maths{
  int a; 
  public:
        maths();
        void showdata();
};
maths :: maths(){
        cout<<”\n this is a constructor”;
        a=100;
}
 
void maths :: showdata(){
        cout<<”\n the value of a=”<<a;
}
void main (){
        maths m;
        m.showdata();
}


6.
Arrays:
An array is a collection of individual values, all of the same data type, stored in adjacent memory locations.  Using the array name together with an integral valued index in square brackets refers to the individual values.  Multi-dimensional arrays have additional indices for each additional dimension. The index indicates the position of the individual component value within the collection of values.  The index is also called the subscript.  In C++, the first array element always has subscript 0.  The second array element has subscript 1, etc.  The base address of an array is its beginning address in memory.

Activity 1.2 : Swap 1st & last element of 1-dimensional Array
#include<iostream.h>
#include<conio.h>
 
int main()
{
        int Arr[100],n,temp;
        cout<<"Enter number of elements you want to insert ";
        cin>>n;
 
        for(int i=0;i<n;i++)
        {
               cout<<"Enter element "<<i+1<<":";
               cin>>Arr[i];
        }
 
        temp=Arr[0];
        Arr[0]=Arr[n-1];
        Arr[n-1]=temp;
 
        cout<<"\nArray after swapping"<<endl;
 
        for(i=0;i<n;i++)
               cout<<Arr[i]<<" ";
 
        getch();
        return 0;
}





7.
Pointers:
A pointer is a memory variable that stores a memory address of another variable and can also be defined as it is a variable that holds the address of some other memory location. It is always denoted by ‘*’. Following example describes that how to declare and assign memory address to an integer pointer.


Activity 1.3 : How to use pointer
#include <iostream>
int main ()
int firstvalue = 5, secondvalue = 15; 
int * p1, * p2; 
p1 = &firstvalue;       // p1 = address of firstvalue 
p2 = &secondvalue;     // p2 = address of secondvalue 
*p1 = 10;              // value pointed by p1 = 10 
*p2 = *p1;             // value pointed by p2 = value pointed by p1 
*p1 = 20;              // value pointed by p1 = 20   
cout << "firstvalue is " << firstvalue << endl; 
cout << "secondvalue is " << secondvalue << endl;
return 0;
}


8.
Write a program with constructor function and another for destruction function.


9.
Write a function that returns the minimum value of an array of integers.
                                                      

11.
Introduce int variables x, y, z and int* pointer variables p, q, r.  Set x, y, z to three distinct values.  Set p, q, r to the addresses of x, y, z respectively.
         Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
         Print the message: Swapping values.
         Execute the swap code: z = x; x = y; y = z;
         Print with labels the values of x, y, z, p, q, r, *p, *q, *r.


12
State the problem with the below code if we want to print the next data item i.e 10 using dereference pointer? Suggest correction. Show code and output here. Also mention why using below code we are unable to get the right solution.




13
Using 2 dimensional arrays print below given output “HI”

14.
Web Resources:


15.
Video Resources:


15.
Summary:
This lab session introduces the concepts of constructors and destructors, arrays and pointers in C++. This lab is designed to revise the concepts of arrays and pointers which will be used in implementing the concepts of data structures.



Post Lab Work:
1.      Study about dangling pointers and discuss before the start of next session.
2.      Study and practice 2 Dimentional and MultiDimentional Arrays Submit below question in the next lab.
Exercise:
Write a program to search an element (given by user) in a two dimensional array.



0 comments:

Post a Comment