0% found this document useful (0 votes)
52 views9 pages

Lab Journal 2 OOP

The document contains code snippets and questions related to pointers and arrays in C++. The first code snippet demonstrates pointer arithmetic and shows that after ip1 = ip2, ip1 will point to the address of variable j. The second code snippet sets the values of two variables using pointers and pointer arithmetic. It would display the values 10 and 10, showing that *p2 = *p1 copies the value that p1 is pointing to. The third code snippet initializes a 2D array using loops and pointer arithmetic. It would display the values of the 2D array and the value at index [2][1], which is 11. The last few questions and code snippets demonstrate additional pointer and

Uploaded by

haseeb Urehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

Lab Journal 2 OOP

The document contains code snippets and questions related to pointers and arrays in C++. The first code snippet demonstrates pointer arithmetic and shows that after ip1 = ip2, ip1 will point to the address of variable j. The second code snippet sets the values of two variables using pointers and pointer arithmetic. It would display the values 10 and 10, showing that *p2 = *p1 copies the value that p1 is pointing to. The third code snippet initializes a 2D array using loops and pointer arithmetic. It would display the values of the 2D array and the value at index [2][1], which is 11. The last few questions and code snippets demonstrate additional pointer and

Uploaded by

haseeb Urehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Object Oriented Programming

Lab Journal # 02

Which variable does ip1 point to at the end of the following code?
int i , j , * ip1 , * ip2 ;
ip1 = &i ;
ip2 = &j ;
ip1 = ip2 ;
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
int i, j, * ip1, * ip2;
ip1 = &i
ip2 = &j
ip1 = ip2 line of code
of variable j
cout << " i= " << &i << " j= " << &j << endl;
cout << "At the end of the code ip1 points to the address of
variable j, ip1= " << ip1 << " ip2= " << ip2;
_getch();
Return 0;
}
What does the following program display?
int main ( ) {
int value1 = 5 , value2 = 1 5 ;
int * p1 , *p2 ;
p1=\&value1 ;
* p1 = 10;
*p2 = * p1 ;
p1 =p2 ;
* p1 =20;
cout << value1 ;
cout << value2 ;
return 0;
}

#include<iostream>
using namespace std;
int main() {
int value1 = 5, value2 = 15;
int* p1, * p2;
p1 = &value1;
p2 = &value2;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 20;
cout <<"value1= " <<value1;
cout <<" value2= " <<value2;
return 0;
}

What does the following program display?


int main ( )
{
int n [ 5 ] ;
int *p ;
p=n;
*p= 10;
p + +;
*p=20;
p=\&n [2];
*p=30;
p=n + 3;
*p=40;
p=n;
* (p+4) =50;
for ( int m=0;m<5;m+ + )
cout <<n [m] ;
return 0;
}
What does the following program display?
int main ( )
{
int x [ 3 ] [ 4 ] , i , j ;//Declaring 2D array and two variables
for ( i = 0; i < 3; + + i )
{
for ( j =0; j < 4 ; + + j )
{//First the inner for loop will go on increasing than outer
for loop.
x[i][j]=3*i+j;
cout <<x [ i ] [ j ] < < " " ;
}
cout <<endl ;
}
cout < < * ( * ( x + 2 ) + 1 ) ;
}
Given the following 3 pointers.
char * C1;
short * S1 ;
int * I 1 ;
long * L1 ;
And given that they point to memory locations 1000, 2000, 3000, and 4000 respectively.

What will be the memory locations pointed to by the pointers after execution of the
following?
C1 + +;
+ + S1;
−− I 1;
L1 − −;

char *C1;//Pointing to the memory location 1000


short *S1;//Pointing to the memory location 2000
int *I1;//Pointing to the memory location 3000
long *L1;//Pointing to the memory location 4000

//After the pointer arithmatic the memory locations will be:

C++;//Pointing to the memory location 1000


++S1;//Pointing to the memory location 2002
--I1;//Pointing to the memory location 2996
L1--;//Pointing to the memory location 4000
• Declare an array of 10 integers and get user input to fill the array values. Then, find total
number of prime numbers in an array.

#include<iostream>
using namespace std;
int main() {
int myArray[10], i, p, d, count = 0;
for (i = 0; i <= 9; i++) {
cout << i + 1 << " :";
cin >> myArray[i];
//for loop to fill the array values
}
for (i = 0; i <= 9; i++) {
p = 1;
d = 2;
while (d <= myArray[i] / 2)
{
if (myArray[i] % d == 0) {
p = 0;
break;
}
d++;
}
if (p == 1) {
count++;
cout << myArray[i] << "\t";
}
}
cout << "Total number of prime numbers in this array are: " << count;
}

• Declare an array of size taken by user. Get user input to fill the array values. Then, find
the factorial of minimum number in an array.

#include<iostream>
using namespace std;

int main() {

int factorial=1;

int *myArray=NULL,size;

cout << "Enter the size of array: " << endl;

cin >> size;

myArray = new int[size];

for (int i = 0; i <= size; i++) {

cout << i + 1 << " :";

cin >> myArray[i];

int min = myArray[0];

for (int i = 0; i <= size; i++) {

if (myArray[i] < min) {

min = myArray[i];

cout << "Minimum number is: " << min << endl;

for (int a = 1; a <= min;a++) {

factorial = factorial * a;

cout << "Factorial of : " << min <<" is "<< factorial;

return 0;

}
• Write a program to declare an array of user provided size to perform the following
functionality. The program should display the subscript of the cell containing the largest
of the values in the array. Then integer 2 should be displayed as its value. If there is more
than one cell containing the largest of the values in the array, then it should print the
smallest of the subscripts of the cells containing the largest values.

#include <iostream>
#include <istream>
#include <conio.h>
using namespace std;
int main()
{
int i, size;
int index = 0, index1, count = 0, counter = 0;
cout << "Enter size of array" << endl;
cin >> size;
int* arr;
arr = new int[size];
cout << "Enter values in array" << endl;

for (i = 0; i <= size; i++)


{
cin >> arr[i];
}
int max = arr[0];
for (i = 1; i <= size; i++)
{
if (max < arr[i])
{
max = arr[i];
index = i;
counter++;
}
else if (max == arr[i])
{
max = arr[i];
count++;
}
}
if (count > 0)
cout << "The smallest of subscript/index containg the largest value: " << max << " is " << index <<
endl;
else
cout << "The maximum value is at subscript/index : " << index << " and the value is " << max << endl;
_getch();
return 0;
}

You might also like