In: Computer Science
Using C++,
Write a program that will use pointer syntax to access variables, dynamically allocate memories, and pass pointers to functions.
1. The program should ask the user to enter a size to the array.
2. The program should dynamically allocate an array with the size.
3. The program should then ask user to input values to the array
4. The program should then find the maximum, display all elements forward and reversed using two different ways of pointer access
5. The program must use functions with pointers as parameters.
6. The manipulation/access of the array must be done through pointer syntax.
7. Your main function should only declare the variables and call the functions.
Variable:
You’ll need the following pointer to be declared in your main function.
float * ptData;
Data Validation:
1. The size of the array cannot be less or equal to 1 (E.g. 2, 5, 100 is fine, but 1 or 0 is no acceptable).
2. Check if pointer has successfully allocated the array.
Functions:
You should have separate functions to handle user input, maximum, and display the array (all the values needs to be returned to the function call, you need to have pointer as the input parameter). Main function should only call other functions.
Function Header |
Explanation |
void getSize(int * ptr) |
This function will ask user to enter a size to the array. The size of the array cannot be less or equal to 1. For example: 2, 5, 100 is fine, but 1 or 0 is no acceptable. |
float * getValues(const int SIZE) |
This function will declare and use a pointer to dynamically allocate an array with the size user has entered. The pointer should be returned to the function’s call. Check if pointer has successfully allocated the array. |
float getMax(const float * ptr, const int SIZE) |
This function will find and return the maximum value of the array using pointer syntax. |
void displayForward(const float * ptr, const int SIZE) |
This function should display all array elements forward (from the first to the last) |
void displayBackward(float * const ptr, const int SIZE) |
This function should display all array elements backward (from last to the first) You are required to use the following pointer and loop to accomplish this task. float * ptr2 = ptr + SIZE; while (ptr < ptr2) |
Sample Output:
Please enter a size to the array: -9 !!!Error: an array’s size cannot be less or equal to 1 Please enter a size to the array: 6 Please enter all values of the array: Value 1: 4 Value 2: 5 Value 3: 7 Value 4: 2 Value 5: 1 Value 6: 8 Displaying all values forward: 4 5 7 2 1 8 Displaying all values backward: 8 1 2 7 5 4 The maximum value of this array is: 8 |
C++ Program:
/* C++ Program that use pointer syntax to access variables, dynamically allocate memories, and pass pointers to functions */
#include <iostream>
using namespace std;
//Function that reads and returns size
void getSize(int * ptr)
{
//Reading size from array
cout << "\n Please enter a size to the
array: ";
cin >> *ptr;
//Loop till user enters a valid value
while(*ptr <= 1)
{
cout << "\n
!!!Error: an array’s size cannot be less or equal to 1 \n";
//Reading value
again
cout << "\n Please
enter a size to the array: ";
cin >> *ptr;
}
}
//Function that reads values from user
float * getValues(const int SIZE)
{
//Allocating memory to array
float *tempArr = new float[SIZE];
cout << "\n\n Please enter all values of the array: \n";
//Reading values from user
for(int i=0; i<SIZE; i++)
{
cout << "\n Value
" << (i+1) << ": ";
cin >>
*(tempArr+i);
}
//Return array
return tempArr;
}
//Function that displays array contents in forward
direction
void displayForward(const float * ptr, const int SIZE)
{
cout << "\n\n Displaying all values
forward: \n\n ";
//Iterating over array
for(int i=0; i<SIZE; i++)
{
//Printing value
cout << *(ptr+i)
<< " ";
}
}
//Function that displays array contents in backward
direction
void displayBackward(float * const ptr, const int SIZE)
{
float *ptr2 = ptr + SIZE;
cout << "\n\n Displaying all values backward: \n\n ";
//Looping backwards
while (ptr < ptr2)
{
//Printing value
cout << *(ptr2-1)
<< " ";
//Decrementing
value
ptr2--;
}
}
//Function that returns max value
float getMax(const float * ptr, const int SIZE)
{
float maxVal;
//Initializing max value
maxVal = *(ptr+0);
//Iterating over array
for(int i=0; i<SIZE; i++)
{
//Finding max
value
if( *(ptr+i) > maxVal
)
maxVal = *(ptr+i);
}
//Return max value
return maxVal;
}
//Main function
int main()
{
float * ptData;
int size;
//Reading size
getSize(&size);
//Reading values
ptData = getValues(size);
//Displaying forward
displayForward(ptData, size);
//Displaying backward
displayBackward(ptData, size);
//Displaying max value
cout << "\n\n The maximum value of this
array is: " << getMax(ptData, size);
cout << endl;
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: