Lab 3 - Pointer
Lab 3 - Pointer
Lab 3: Pointer
Pointer is a variable that stores memory addresses. Unlike normal variables it does not store
user given or processed value, instead it stores valid computer memory address.
Figure 1: pointer
1. Complete the below C++ program, so that it can read the memory address of any
variable.
#include <iostream>
#include <string>
using namespace std;
int main()
{
/* Simple declarations */
string word = "abc";
int integer = 1;
float real = 10.4f;
long long biginteger = 989898989ll;
return 0;
}
Expect Output:
2. Below is a C++ program to create, initialize and use pointer variable. Execute the
program and find its final output.
#include <iostream>
using namespace std;
int main()
{
int num = 10;
int * ptr;
return 0;
}
1. Write a C++ program to read two numbers from user and add them by using the
pointers. Your program output should display the similar content as the output
sample.
Output sample:
/* function prototype */
void swap(int * value1, int * value2);
Output sample:
Part B: Learn how to input and print array elements using pointers in C++
1. Write a C++ program to input and print array elements using pointer. The number of
input should be determined by the user.
Note: It should be a dynamic allocated array.
Output sample:
Part C: Homework.
Submit your answer (in doc / pdf) to the Microsoft Teams. Your answer should include your
code and your program screenshot. Submission due date: 20 October 2019 (11.59pm).
1. Complete the following program skeleton. When finished, the program will ask the user
for a length (in inches), convert that value to centimeters and display result. You are to
write the function convert. (Note: 1 inch = 2.54cm. Do not modify function main.)
#include <iostream>
#include <iomanip>
using namespace std;
void main ()
{
double measurement;
2. Write a program that will ask the user to enter the width and length of a rectangle, and
then display the rectangle’s area. The program calls the following functions:
getLength – this function should ask the user to enter the rectangle’s length, and then
return that value as a double.
getWidth – this function should ask the user to enter the rectangle’s width, and then
return that value as a double.
getArea – this function should accept the rectangle’s length and width as arguments,
and return the rectangle’s area. The area is calculated by multiplying the length by the
width.
displayData - this function should accept the rectangle’s length, width, and area as
reference arguments, and display them in an appropriate message on the screen.
3. Write a program that accept an integer argument indicating the total number of subjects
and the test scores are stored in a dynamically allocated array. The program calls the
following functions:
calcAverage – this function should accept an array argument to calculate the average
of the test scores.
findLowest – this function should find and return the lowest of test scores array
passed to it.
Write a program that grades the written portion of the driver’s license exam. The exam has 20
multiple choice questions. Here are the correct answers:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10. D 15. D 20. A
Your program should store the correct answers shown above in an array. It should ask the
user to enter the answers for each of the 20 questions, and the answers should be stored in
another array. After the student’s answers have been entered, the program should have a
function to display a message indicating whether the student passed or failed the exam.
(Note: A student must correctly answer 15 of the 20 questions to pass the exam.)