Using C++, you are required to use pointers to manipulate an array. You need the
ID: 3786716 • Letter: U
Question
Using C++, you are required to use pointers to manipulate an array.
You need the following variables in main function.
const int SIZE = 6;
int dataArray[SIZE];
int * pData = dataArray;
You are required to use the pointer pData to manipulate the array.
You are also required to use at least 3 different ways of pointer manipulations to the array.
Use the function header provided below, you may not change the function headers
Function header
Explanation
void getAllData(int * pIntArray, const int SIZE)
This function should ask user to enter all values to the array using pIntArray as manipulator
double getAverage(const int * pIntArray, const int SIZE)
This function should calculate and return the average to the array using pIntArray as manipulator
void displayAllValue(const int * pIntArray, const int SIZE)
This function should display all values of the array using pIntArray as manipulator. I used left justified setw(5) for display
void bubbleSort(int * const pIntArray, const int SIZE)
This function should sort the array using bubble sort. Notice the pointer is a constant pointer. You should also add a swap function.
Other Specifications:
There should be no global constant variables at all.
All functions must have prototypes.
Store all values as variables, do not hardcode any values.
Sample Output:
Please enter all values to the array, all values must be more than 0
Value 1: 90
Value 2: -11
!!!Error: values cannot be less than 0, reenter!!!
Value 2: 11
Value 3: 45
Value 4: 0
Value 5: 33
Value 6: 50
The average is: 38.1667
Before bubble sort:
All values:
90 11 45 0 33 50
After bubble sort:
All values:
0 11 33 45 50 90
Function header
Explanation
void getAllData(int * pIntArray, const int SIZE)
This function should ask user to enter all values to the array using pIntArray as manipulator
double getAverage(const int * pIntArray, const int SIZE)
This function should calculate and return the average to the array using pIntArray as manipulator
void displayAllValue(const int * pIntArray, const int SIZE)
This function should display all values of the array using pIntArray as manipulator. I used left justified setw(5) for display
void bubbleSort(int * const pIntArray, const int SIZE)
This function should sort the array using bubble sort. Notice the pointer is a constant pointer. You should also add a swap function.
Explanation / Answer
#include <iostream>
using namespace std;
void getAllData(int * pIntArray, const int SIZE){
cout<<"Please enter all values to the array, all values must be more than 0"<<endl;
for(int i=0; i<SIZE; i++){
cout<<"Value "<<i+1<<": ";
cin >> *(pIntArray + i);
if(*(pIntArray + i) < 0){
cout<<"!!!Error: values cannot be less than 0, reenter!!!"<<endl;
i--;
}
}
}
double getAverage(const int * pIntArray, const int SIZE){
int total = 0;
for(int i=0; i<SIZE; i++){
total = total + *(pIntArray + i);
}
return total/(double)SIZE;
}
void displayAllValue(const int * pIntArray, const int SIZE)
{
cout<<"All values: "<<endl;
for(int i=0; i<SIZE; i++){
cout<<*(pIntArray + i)<<" ";
}
cout<<endl;
}
void bubbleSort(int * const pIntArray, const int SIZE){
int temp = 0;
for(int i=0; i < SIZE; i++){
for(int j=1; j < (SIZE-i); j++){
if(*(pIntArray + j - 1) > *(pIntArray + j )){
//swap the elements!
temp = *(pIntArray + j - 1);
*(pIntArray + j - 1) = *(pIntArray + j );
*(pIntArray + j )= temp;
}
}
}
}
int main()
{
const int SIZE = 6;
int dataArray[SIZE];
int * pData = dataArray;
getAllData(pData, SIZE);
cout<<"Average is "<<getAverage(pData, SIZE)<<endl;
cout<<"Before bubble sort:"<<endl;
displayAllValue(pData, SIZE);
bubbleSort(pData, SIZE);
cout<<"After bubble sort:"<<endl;
displayAllValue(pData, SIZE);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Please enter all values to the array, all values must be more than 0
Value 1: 90
Value 2: -11
!!!Error: values cannot be less than 0, reenter!!!
Value 2: 11
Value 3: 45
Value 4: 0
Value 5: 33
Value 6: 50
Average is 38.1667
Before bubble sort:
All values:
90 11 45 0 33 50
After bubble sort:
All values:
0 11 33 45 50 90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.