Assignment On Tasks of Lab 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

ASSIGNMENT ON TASKS OF LAB-5

CSE 4562

Computer Science & Technology Lab III


Submitted To
Tanjila Alam Sathi
Lecturer
Department of Computer Science and Engineering
Islamic University of Technology

Submitted By
Arman Sarkar
ID 170061038
Section-A
Lab Group- A2

JULY 31, 2020


DEPARTMENT OF BUSINESS & TECHNOLOGY MANAGEMENT
ISLAMIC UNIVERSITY OF TECHNOLOGY
Contents

Introduction
02
Problem Description
04
Source code
05
Output screen
16
Conclusion
20

1|P a g e
INTRODUCTION

A pointer is a variable whose value is the address of another variable. Like any variable or
constant, there is a declaration procedure for a pointer. The general form of a pointer variable
declaration is-
Type *var_name;
Pointer is a very important feature of Object Oriented Programming (C++) like all the other
programming languages i.e. procedural, functional, OOP. There are few important operations,
which we will do with the pointers very frequently.
 We define a pointer variable.
 Assign the address of a variable to a pointer.
 Finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the address
specified by its operand.
Simply we can do all the operation using pointer just like we do using variables in an effective
and an efficient way. Pointer is a big part of memory management. It saves memory unlike
variables.
In C++, Pointers are variables that hold addresses of other variables. Not only can a pointer store
the address of a single variable, it can also store the address of cells of an array.
int *ptr;
int arr[5];
ptr=arr;

In most contexts, array names decay to pointers. In simple words, array names are converted to
pointers. That's the reason why we can use pointers to access elements of arrays.

However, we should remember that pointers and arrays are not the same.

In the C++ Functions, we learned about passing arguments to a function. This method used is
called passing by value because the actual value is passed.

2|P a ge
Figure: Pass value Vs. Pass by reference

However, there is another way of passing arguments to a function where the actual values of
arguments are not passed. Instead, the reference to values is passed.

C++ allows to have pointers to objects. The pointers pointing to objects are referred to as Object
Pointers. Just like other pointers, the object pointers are declared by placing in front of a object
pointer's name. It takes the following general form:
class-name ∗object-pointer;

To access public members using an object the dot operator is used and to access public members
using an object pointer the arrow operator is used. When a pointer is incremented, it points to the
next element of its type. The same is true of pointer to object.

pointer_name  public member;

3|P a ge
PROBLEM DESCRIPTION

There are five lab tasks assigned in lab 5. All of them are explained below:
Task-1:
The first problem is a basic use of built in library function strcmp(). Strcmp() is a function by
which we can compare between two functions. Apart from using strcmp() function we can
compare between two string by using simple array and loop concept.
Array must be character type array and the loop are used here known as while loop and for loop.
Task-2:
The second problem is about determining variable’s value, address and memory size using
pointer concept. The datatypes used here are integer type, double type and character type. After
declaring and initializing a pointer to each of the three variables, this program should print the
address of, and value stored in, and the memory size (in bytes) of each of the six variables. The
addresses that look something like this: "0xbfe55918". The initial characters "0x" indicates that
hexadecimal notation is being used; the remainder of the digits give the address itself. We have
used the sizeof () operator to determine the memory size allocated for each variable.
Task -3:
Task 3 gives the opportunity to learn how to insert and access elements in array using pointer.
This program is about to input elements in an array and sort array using pointers in ascending or
descending order using function pointers in C++ programming. Here we use, pointer as a
function argument known as reference by call. The array is accessed by using pointer also.
Task-4:
Task 4 is a task that indulges about the volume of a simple box using constructor and call by
reference. It is mainly about to calculate the volume of a box (using pointer to object concept).
After creating a class box, declared its constructor function to take its length, breadth, height.
Then after creating two object of this class to pass the arguments. Then used pointer to access the
members of the class.
Task-5:
The task-5 is about writing the clean code in C++ after debugging and running the programming
exercise of 9.5 from referred book. To debug the program, virtual keyword was written in base
class and prototype base class and derived base class of print function was made same. Access
modifier of Class B was also switched from private to protected. Redundant variables were also
screened out. Proper passing of arguments were also rectified so that the program could run.

4|P a g e
SOURCE CODE

Task-1 (without strcmp()):

#include<bits/stdc++.h>
using namespace std;

int stringCompare(char[], char[]);

int main()
{
char arr1[100], arr2[100];
int compare;

cout<<"Input string 1"<<endl;


gets(arr1);

cout<<"Input string 2"<<endl;


gets(arr2);

compare = stringCompare(arr1, arr2);

if(compare == 1)
cout<<"Both strings are equal"<<endl;
else
cout<<"Both the strings are different."<<endl;

5|P a ge
return 0;
}
int stringCompare(char arr3[], char arr4[])
{
int i = 0, flag = 0;
while(arr3[i] != '\0' && arr4[i] != '\0')
{

if(arr3[i] != arr4[i])
{
flag = 1;
break;
}
i++;
}
if(flag == 0 && arr3[i] == '\0' && arr4[i] == '\0')
return 1;
else
return 0;

Task-2 (with strcmp()):

#include<bits/stdc++.h>
using namespace std;

int main()

6|P a ge
{
char str1[100], str2[100];

cout<<"Input string 1"<<endl;


gets(str1);

cout<<"Input string 2"<<endl;


gets(str2);

if (strcmp(str1,str2) == 0)
cout<<"Both strings are equal"<<endl;
else
cout<<"Both the strings are different"<<endl;

return 0;

Task-2:

#include<bits/stdc++.h>
using namespace std;

int main()
{
int i;
cout<< "Enter an Integer Value: ";
cin>>i;

7|P a ge
int *p;
p= &i;

char c;
cout<< "Enter a character : ";
cin>>c;
char *ch;
ch=&c;

double d;
cout<< "Enter a double Value: ";
cin>>d;
double *y;
y=&d;

cout<< endl;
cout<< "The address of int: " << &i << endl;
cout<< "The address of char: " << (void*)&c << endl;
cout<< "The address of double: " << &d << endl;
cout<< endl;

cout<< "The address of int*: " << p << endl;


cout<< "The address of char*: " << (void*)ch << endl;
cout<< "The address of double*: " << y << endl;
cout<< endl;

cout<<"The value of int: " << i << endl;


cout<<"The value of char: " << c << endl;

8|P a ge
cout<<"The value of int: " << d << endl;
cout<< endl;

cout<<"The value of int*: " << *p << endl;


cout<<"The value of char*: " << *ch << endl;
cout<<"The value of double*: " << *y << endl;
cout<< endl;

cout<<"The size of int is " << sizeof(i) << "Bytes" << endl;
cout<<"The size of char is " << sizeof(c) << "Byte" << endl;
cout<<"The size of double is " << sizeof(d) << "Bytes" << endl;
cout<< endl;

cout<<"The size of int* is " << sizeof(*p) << "Bytes" << endl;
cout<<"The size of char* is " << sizeof(*ch) << "Byte" << endl;
cout<<"The size of double* is " << sizeof(*y) << "Bytes" << endl;
return 0;
}

Task-3:

#include<bits/stdc++.h>
using namespace std;

int *sortAsc(int *p, int size);

int *sortDsc(int *q, int size);

9|P a ge
int main()
{
int arr[10];

cout << "Input Array Elements: ";


for(int i = 0; i < 10; ++i)
cin >> arr[i];

int *p=sortAsc(arr,10);

cout<<"Array in ascending order:";


int i;
for(i=0; i<10; i++)
cout<<*(p+i)<<" ";
cout<<endl;

int *q=sortDsc(arr,10);
cout<<"Array in Descending order:";
int a;
for(a=0; a<10; a++)
cout<<*(q+a)<<" ";

return 0;

int *sortAsc(int *p, int k)

10 | P a g e
{
int i,j;
for(i=0; i<k; i++)
for(j=i+1; j<k; j++)
if(*(p+j)<*(p+i))
{
int temp=*(p+j);
*(p+j)=*(p+i);
*(p+i)=temp;
}
return p;
}
int *sortDsc(int *q, int c)
{
int a,b;
for(a=0; a<c; a++)
for(b=a+1; b<c; b++)
if(*(q+b)>*(q+a))
{
int temp=*(q+b);
*(q+b)=*(q+a);
*(q+a)=temp;
}
return q;
}

11 | P a g e
Task-4:

#include<bits/stdc++.h>
using namespace std;

class box
{
public:
float length;
float height;
float width;

box(float x, float y, float z)


{
length=x;
height=y;
width=z;
}

void vol()
{
float volume;
volume= length*height*width;
cout<<"The Volume is: "<<volume<<endl;
}
};
int main()
{

12 | P a g e
box box1(10,10,10),*ptr1;
ptr1= &box1;
cout<< "For object box1, ";
ptr1->vol();

box box2(13.5,6.7,5.5),*ptr2;
ptr2= &box2;
cout<< "For object box2, ";
ptr2->vol();

return 0;
}

Task-5:

#include<bits/stdc++.h>
using namespace std;

class A
{
protected:
int a,b;
public:
A(int x=0,int y=0)
{
a=x;
b=y;
}

13 | P a g e
virtual void print();
};

class B:public A
{
protected:
float p,q;
public:
B(float u, float v)
{
p=u;
q=v;
}
B()
{
p=q=0;
}
void input(float u, float v);
virtual void print();
};

void A::print(void)
{
cout<< "A values: " <<a<< " " <<b<<"\n";
}

void B:: print(void)


{

14 | P a g e
cout<< "B values: " <<p<< " " <<q<<"\n";
}

void B::input(float x, float y)


{
p=x;
q=y;
}

int main()
{
A a1(10,20), *ptr;
B b1;
b1.input(7.5,3.5);
ptr=&a1;
ptr->print();
ptr=&b1;
ptr->print();
return 0;
}

15 | P a g e
OUTPUT SCREEN

Task-1 (without strcmp())

Task-1 (with strcmp())

16 | P a g e
Task-2

17 | P a g e
Task-3

Task-4

18 | P a g e
Task-5

The output has emerged like the given output screen because virtual keyword was written in base
class and prototype base class and derived base class of print function was made same. This was
the main debugging step of this error prone program. Some other minor bugs were eradicated
and those are given in the problem description.

19 | P a g e
CONCLUSION

By solving these lab tasks, we can get a vivid idea on-


 C++ pointers
 C++ pointer and arrays
 C++ pointers and functions
We also can understand how by using it the code became much more simple. Pointer
undoubtedly has many advantages over normal variable.
 Pointers provide direct access to memory
 Pointers provide a way to return more than one value to the functions
 Reduces the storage space and complexity of the program
 Reduces the execution time of the program
 Provides an alternate way to access array elements
 Pointers can be used to pass information back and forth between the calling function and
called function.
 Pointers allows us to perform dynamic memory allocation and deallocation.
 Pointers helps us to build complex data structures like linked list, stack, queues, trees,
graphs etc.
 Pointers allows us to resize the dynamically allocated memory block.
 Addresses of objects can be extracted using pointers

Though pointer is a better solution to memory management, it’s not free from some of the
caveats.

 Uninitialized pointers might cause segmentation fault.


 Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to
memory leak.
 Pointers are slower than normal variables.
 If pointers are updated with incorrect values, it might lead to memory corruption

Basically, pointer bugs are difficult to debug. It is programmer’s responsibility to use pointers
effectively and correctly.
Evaluating every aspects of pointer, it has obviously proven to be an essential topic to learn. In
this quest, the tasks of lab 5 have successfully helped to enrich our skills, techniques and
knowledge about pointer and its different types of usage-
 Passing pointer as an argument
 Using pointer in array and accessing array’s elements through pointer
 Using pointer to object concept

20 | P a g e
Thus the tasks of lab-5 have successfully develop our existing knowledge about Object Oriented
Programming (C++).

21 | P a g e

You might also like