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;
#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 s;
cout<< "\n";
cout<< "Enter the value of s (target) ";
cin >> s;
#include <iostream>
using namespace std;
int* maximum ( int *p , int n);
void main()
{
int n;
cout<< "Enter the size of array ";
cin >> n;
cout << "The address of max value is "<< maximum (p , n) << "\n";
}
#include <iostream>
using namespace std;
void main()
{
int n;
cout<< "Enter the size of array ";
cin >> n;
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.
#include <iostream>
using namespace std;
void main ( ){
float pfs[3] = {5.5, 10.5, 20.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);
}