Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer science ----------------------------------------------------------------------------------------------------- Part 1 - Passing pointers to functions In pointers.cpp there is a prototype, and a call to a function to initialize the

Computer science

-----------------------------------------------------------------------------------------------------

Part 1 - Passing pointers to functions

In pointers.cpp there is a prototype, and a call to a function to initialize the values in a Student instance. Note that the parameter to this function is the address of a structure instance.

Write the definition for this function at the bottom of the file.

Don't forget to use the member selector operator for pointers (->) when you reference instance members in the function. e.g.:

 cin >> ptr->id; 

Don't worry about being fancy, this is just a simple exercise for you to play with pointers.

You should enter student marks between 0 and 100 (no need to error check).

Compile and run this C++ program.

Write a function that takes a pointer to an instance of the Student structure and displays the contents.

Call it from the main routine.

Don't forget to pass the function the address of the structure instance.

After you've done that, compile, link, and run again.

At this point, your output should look something like this:

Please enter a name: Sam Please enter an id: 195556 Please enter a mark: 87 Please enter a mark: 88 Please enter a mark: 89 Student info: Name: Sam Id: 195556 Mark 0: 87 Mark 1: 88 Mark 2: 89 

Part 2 - Making and Using Dynamically Allocated Arrays.

Let us say that you want to choose how many marks to enter for the student and have the array change size accordingly. This gives you the opportunity to work with dynamic pointers.

You will have to make the following changes to your code:

Change mark within the Student struct to an integer pointer.

Ask the user from main how many marks they would like to enter

Change both the functions to have an additional parameter which is the number of marks.

For the initStudent function, dynamically allocate the marks according to the number required

Add a destructor for the Student structure (~Student). The purpose of this will be to delete any memory inside this structure allocated using new

The run might look like the following:

 How many marks are there? 5 Please enter a name: Sam Please enter an id: 195556 Please enter a mark: 87 Please enter a mark: 98 Please enter a mark: 77 Please enter a mark: 88 Please enter a mark: 78 Student info: Name: Sam Id: 195556 Mark 0: 87 Mark 1: 98 Mark 2: 77 Mark 3: 88 Mark 4: 78 

Part 3 (Just for Fun) - Dynamically Allocated Arrays of Structures

Ask the user from main how many students there are in the class.

In main, dynamically allocate and array of Student's with the input size

Call the initialize function for each student.

Call the print function for each student.

Deallocate the dynamic array of students.

CODE: ----------------------------------------------------------------------------------------------------------------------

// Filename: pointers.cpp

#include

#include

using namespace std;

struct Student

{

string name;

int id;

int mark[3];

};

void initStudent(Student* ptr); // function prototype for initialization

// function prototype for printing

//*********************** Main Function ************************//

int main ()

{

Student stu; // instantiating an STUDENT object

Student* stuPtr = &stu; // defining a pointer for the object

initStudent(&stu); // initializing the object

// printing the object

} // end main

//-----------------Start of functions----------------------------//

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started