Sunteți pe pagina 1din 5

CMP 220L –Programming II

LAB 1: Pointers and Table of Pointers

Question 1: create an array of 10 integers then use pointers to do the following:


a) Use the travelling pointer technique (version 1), to initialize the array to
zero.
b) Use pointer-index notation technique to print out the array.
c) Use pointer-offset notation to find the maximum value of the array
d) Print the address of each cell.

#include <iostream>
using namespace std;
void main()
{
int arr[10];
int *pointer = arr;

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

(*pointer) = i;
pointer++;
}
cout << "Using Pointer Index \n";
pointer = arr;
for (int i = 0; i < 10; i++) {
cout << "Value " << i << " is "<< pointer[i]<< " \n";
}
cout << "Using Pointer offset \n";

int max = *pointer;


for (int i = 0; i < 10; i++) {
if (*((pointer)+i) >= max)
(max = *((pointer)+i));
}
cout << "The Max Value is " << max << "\n";

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


cout << "Address " << i << " is " << pointer + i << " \n";
}
}

1
Question 2 :Write a program to reverse the content of an array of int values using two
traveling pointers. Your program should create an array of 10 elements, read their
values from the user, reverse the array and print the reversed array.

#include <iostream>
using namespace std;
void main()
{
int arr[10];
int *pointer = arr;
int temp = 0;
for (int i = 0; i < 10; i++) {
cin >> (*pointer);
pointer++;
}
int *h = arr;
int *t = arr + 9;
while (h<t) {
temp = *h;
*h = *t;
*t = temp;
h++;
t--;
}

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


cout << "Reversed" << arr[i] << " \n";
}
}

2
Question 3: Write a function myFunction ( ) that takes two parameters, namely, a
pointer to an int array and size as the number of element in that array. Your function
should calculate the following output arguments (as pointers): min, max and avg.
void myFunction(double *arr, int size, double *min, double *max, double
*avg);

In the main program, ask the user for the array size, then call the function to
calculate the output parameters and print them out (printing should be done in the
main).

#include <iostream>
using namespace std;
void myFunction(double *arr, int size, double *min, double *max, double *avg);

void main()
{
int size;
cout << "Enter the array size";
cin >> size;
double *arr = new double[size];

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


cin >> (arr[i]);

3
}
double min;
double max;
double avg;
myFunction(arr, size , &min, &max, &avg);

cout << "Max" << max << " \n";


cout << "Min" << min << " \n";
cout << "Avg" << avg << " \n";
}

void myFunction(double *arr, int size, double *min, double *max, double *avg)
{

*max = arr[0];
for (int i = 0; i < size; i++) {
if (arr[i] >= *max)
(*max = (arr[i]));
}

*min = arr[0];
for (int i = 0; i < size; i++) {
if (arr[i] <= *min)
(*min = (arr[i]));
}
double sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + arr[i];
}
*avg = sum / size;
}

4
Question 4: Write a function that takes a table of pointers to integers and return a
pointer to the largest integer.
int* minimum(int * values[], int size);

Write a main program to test your function, you should define an array of pointers of
5 elements, read their values from the user and then print the minimum value with the
help of your function.

#include <iostream>
using namespace std;
int* minimum(int * values[], int size);

void main()
{
int d[5];
int * values[5] = { &d[0], &d[1], &d[2], &d[3], &d[4] };

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


cin >> (*values[i]);
}
for (int i = 0; i < 5; i++) {
cout<< (values[i]) << "\n";
}
cout << "\n";
cout << minimum(values, 5);
cout << "\n";
}

int* minimum(int * values[], int size)


{
int *min = values[0];
for (int i = 0; i < 5; i++) {
if (*(values[i]) <= *min)
(min = (values[i]));
}
return min;
}

( I displayed the addresses of all inputs before returning the address to the largest
input just to check my work) …

S-ar putea să vă placă și