Anda di halaman 1dari 6

CMP220 HW1

Pointers

Dara Sakhnini

68368
1. Write and test a C++ main program that:
a. declare an array arr of 6 integers
b. Prompt the user for 6 integer values and store them in arr.
c. Prompt the user for a target integer target.
d. Search for target in arr. If target is found to match an element in the
arr, then the program prints out a message which contains the address
of the found element, otherwise, if no element found then the message
“the element target not found” will be printed out. The program must
use the array name-offset notation to traverse the array.

#include <iostream>
using namespace std;

void main()
{
int arr[6];
int target;

for (int i = 0; i<6; i++)


{
cout<< "Enter the value of integer #" << i + 1<< " ";
cin >> arr[i];
cout<< "\n";
}
cout<< "\n";
cout<< "Enter the value of target integer ";
cin >> target;
int found = 0;
for (int i = 0; i<6; i++){
if (*(arr + i) == target){
found= 1;
cout << "The element Target was found at address =
"<< (arr + i)<< "\n";
}
}
if (found == 0){
cout << "The element Target not found \n";
}
}
2. Write and test a function search( ) that searches for an integer s in an array of n elements. If s is found to match an
element in the array, the function returns the address of that element; otherwise it returns NULL. The function must use the
travelling pointer (version-2) notation to traverse the array. The function has the following prototype.

int* search ( int *p , int s, int n)

#include <iostream>
using namespace std;
int* search ( int *p , int s, int n);

void main()
{
int n;
cout<< "Enter the size of array ";
cin >> n;

int *p = new int[n];

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


cout<< "Enter the value of integer # " << i + 1<< " ";
cin >> p[i];
cout<< "\n";
}

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


cout<< "The address of integer # " << i + 1<< " is ";
cout<< (p + i) << "\n";
}
cout << "\n";

int s;
cout<< "\n";
cout<< "Enter the value of s (target) ";
cin >> s;

cout << search (p ,s, n) << "\n";


}

int* search ( int *p , int s, int n)


{
int *final = p+(n-1);
while (p <= final)
{
if (*p == s){
return (p);
}
p++;
}
return NULL;
}
3. Write and test the function maximum that is passed an array of n integers and returns a pointer to the array cell that has the
maximum value among the n integers. The function must use the travelling pointer (1st version) notation to traverse the
array. The function has the following prototype.

int* maximum ( int *p , int n);

#include <iostream>
using namespace std;
int* maximum ( int *p , int n);

void main()
{
int n;
cout<< "Enter the size of array ";
cin >> n;

int *p = new int[n];

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


cout<< "Enter the value of integer # " << i + 1<< " ";
cin >> p[i];
cout<< "\n";
}

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


cout<< "The address of integer # " << i + 1<< " is ";
cout<< (p + i) << "\n";
}
cout << "\n";

cout << "The address of max value is "<< maximum (p , n) << "\n";
}

int* maximum ( int *p , int n)


{
int *max = p;
for (int i = 0; i<n; i++){
if (*p > *max){
max = p;
}
p++;
}
return (max);
}
4. Write and test a C++ main program that:
a. Prompts the user for an integer value n which represents the number of integers to
be read next.
b. Create a one dimensional dynamic array with size equal to n.
c. Prompt the user to input n integers and place these integers in the array using the
traveling pointer technique (2nd version)
d. Print out the content of the array, in reversed order, using the traveling pointer
technique (1st version).

#include <iostream>
using namespace std;

void main()
{
int n;
cout<< "Enter the size of array ";
cin >> n;

int *p = new int[n];


int *t = p+(n-1);

while(p<=t) {
cout<< "Enter an integer ";
cin >> *p;
p++;
}
p--;
cout << "\n REVERSED \n";
for (int i = 0; i < n ; i++){
cout<< *p << "\n";
p--;
}
}
5. Implement the function psum( ) that is passed an array of n floats and returns a
pointer to the sum of such an array. Print the address of the returned sum and its
value in the main program.

float *psum (float *p, int n); // function prototype


void main ( ){
float pfs[3] = {5.5, 10.5, 20.3};
for (int j = 0; j < 3; j++)
cout << pfs[j] << endl;
float* rp = psum (pfs, 3);
cout << “The address of the sum: “ << rp << endl;
cout << “The value of the sum: “<< (*rp) << endl;
}
float* psum (float* p , int n){ // A function that returns a pointer

#include <iostream>
using namespace std;

float *psum (float *p, int n); // function prototype

void main ( ){
float pfs[3] = {5.5, 10.5, 20.3};

for (int j = 0; j < 3; j++)


cout << pfs[j] << endl;

float* rp = psum (pfs, 3);

cout << "The address of the sum: " << rp << endl;
cout << "The value of the sum: " << (*rp) << endl;
}
float* psum (float* p , int n){
float *sum = p;
for (int i = 1; i < 3; i++){
*sum = *sum + p[i];
}
return (sum);
}

Anda mungkin juga menyukai