You are on page 1of 10

Fundamentals of Programming Lab

Lab Report 02

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: Section: A Group: 2
SUBMITTED BY
Sr. Lab
No. Names CMSID Report Lab work Total
Lab Tasks
(Theory)
1 Khurram Rouf 367594

School of Mechanical and Manufacturing Engineering


Objectives:
1. Introduction to the pointer

Theory:

Pointer:

In C++, a pointer refers to a variable that holds the address of another variable. Like regular
variables, pointers have a data type. For example, a pointer of type integer can hold the address
of a variable of type integer. A pointer of character type can hold the address of a variable of
character type.

Working of pointer:
Output:

Working Process:

Lab work:

1. Write a program in C++ by using pointer to print out the address of a.

Code:
#include <iostream>
using namespace std;

main()
{
int a=10;
int *a_ptr;
a_ptr=&a;
cout<<"a="<<a<<endl;
cout<<&a;
cout<<"\nBY Khurram Rouf 367594 ";
}

Output:

2. Write a program in C++ by using pointer to print out the address of a and b.

Code:

#include <iostream>
using namespace std;

main()
{
int a=10,b;
int *a_ptr,*b_ptr;
a_ptr=&a;
b_ptr=&b;
cout<<"a="<<a<<endl;
cout<<a_ptr<<endl;
cout<<"Garbage value of b="<<b<<endl;
cout<<b_ptr<<endl;

cout<<"By Khurram Rouf 367594";

}
Output:

3. Write a program in C++ by using pointer to convert lower case word into upper case
word.

Code:

#include <iostream>
#include <cctype>
using namespace std;
void upper (char*);

main()
{
cout<<"Word in lowercase is: "<<endl;
char a1[]="khurram";
char *ptr_a1(a1);
cout<<a1<<endl;
cout<<"Word in uppercase is: "<<endl;
upper(ptr_a1);
cout<<a1<<endl;
cout<<"By Khurram Rouf 367594 ";
return 0;
}
void upper (char*ptr_a)
{
while (*ptr_a)
{
*ptr_a=toupper(*ptr_a);
ptr_a++;
}
}

Output:
4. Write a program in C++ by using pointer to convert upper case word into lower case
word.

Code:

#include <iostream>
#include <cctype>
using namespace std;

main()
{
int i;
char b;
char x[50];
cout<<"Sentence in upper case: "<<endl;
cin.getline(x,50);
cout<<"Sentence in lower case: "<<endl;
for (i=0; i<=16; i++)
{
char b;
b=tolower(x[i]);
cout<<b;
}
cout<<"\nBY Khurram Rouf 367594 ";
}

Output:
Lab Task:

1. Use following pointer increment methods in a program to show the working of each
method.

Code:
#include <iostream>
using namespace std;
main()
{
int i[5]={3,4,5,7,8};
int *iptr=i;

cout<<"The value of *iptr++ is :"<<endl;


cout<<*iptr++<<endl;
cout<<"The value of *++iptr is :"<<endl;
cout<<*++iptr<<endl;
cout<<"The value of ++*iptr++ is :"<<endl;
cout<<++*iptr<<endl;
cout<<"The value of (*iptr)++ is :"<<endl;
cout<<(*iptr)++<<endl;
cout<<"By Khurram Rouf 367594"<<endl;
}
Output:

2. Write a function that receives pointers to two character strings and returns a count of the
number of times that the second character string occurs in the first character string. Do
not allow overlap of the occurrences. Thus, the string “110101” contains only one
occurrence of “101.” Assume that the function has the prototype statement
int overlap(char *ptr1, char *ptr2);

Code:
#include <iostream>
using namespace std;
int overlap (char *ptr1, char *ptr2);
int str = 0;
char a1[50], a2[50];
int
main ()
{
cout << "Enter First String ";
cin.getline (a1, 40);
cout << "Enter Second String ";
cin.getline (a2, 40);
cout << "The number of common occurences is: " << overlap (a1, a2)<<endl;
cout << "By Khurram Rouf 367594";
}
int
overlap (char *ptr1, char *ptr2)
{
for(int i=0;i<=63;i++)

{
if (*ptr1==*ptr2)
{
*ptr1++;
*ptr2++;
if (*ptr1!=*ptr2)
{
str++;
ptr2=a2;
}
if (*ptr1!=*ptr2)
{
*ptr1++;
}
}
}
return (str);
}

Output:

3. Why the following program does not prints the address of the character type variable “a”?

#include<iostream>
using namespace std;
main()
{
char a='b';
char *a_ptr;
a_ptr=&a;
cout<<a_ptr;
}
Investigate to change this program so as it starts giving address of the variable. Also explain
this issue.

Ans:

When this program is run on visual studio code it give a garbage value.

Solution:

#include<iostream>
using namespace std;
main()
{
char a='b';
void *a_ptr;
a_ptr=&a;
cout<<a_ptr;
cout<<"\nBY khurram Rouf 367594 ";
}

Output:

Reasons:
When we execute the program and pointer reach at *a_ptr variable and instead of considering it a
memory variable, it considers it as a char variable. Thus, it returns a garbage value. This can be
solved by changing the data type from char to void.

The End

You might also like