Question

In: Computer Science

Using C++, Write a program that will use pointer syntax to access variables, dynamically allocate memories,...

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

Solutions

Expert Solution

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:


Related Solutions

Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory. Help ASAP!!!
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from the .txt file and sort the names in lexical order Grumpy Dopey Doc Happy Bashful Sneezy Sleepy
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer...
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays. 1) Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision...
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT