Lab Journal 2 OOP
Lab Journal 2 OOP
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 will be the memory locations pointed to by the pointers after execution of the
following?
C1 + +;
+ + S1;
−− I 1;
L1 − −;
#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;
min = myArray[i];
cout << "Minimum number is: " << min << endl;
factorial = factorial * a;
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;