Question

C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which...

C++ 29.5 Lab 29: Pointers

Exercise:

Before the main function, declare a struct called Movie which has title as a string and year as the integer as the member variables. Also before the main function, instantiate an object of Movie called m with initial value "Avengers" as title and year as 2019. Note m is the global variable and A in the title is uppercase. Also before the main function, declare another global variable which is a pointer to the movie object m named ptr and make it point to m.

In the main function, output the title and the year of m to the console separated by a space so that the output is "Avengers 2019" with a new line at the end.

Next in the main function, change the value of m so that the title of m is "Shrek" and the year is 2001 by manipulating ptr only. Again you should do this not using the variable m directly but using the pointer ptr instead.

Now output the value of m again by directly using the variable m. Double check the values got changed. Again, the title and the year should be separated by a space and have a new line at the end. So the new output should be "Shrek 2001" with a new line (note that S is the uppercase)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include <iostream>
#include <string>

using namespace std;

//Declaring a structure
struct Movie
{
string title;
int year;
};

//Creating object
struct Movie m = {"Avengers", 2019};

//Creating Pointer
struct Movie *ptr = &m;

//Main function
int main()
{
//Printing Structure values
cout << m.title << " " << m.year << endl;

//Changing values
ptr->title = "Shrek";
ptr->year = 2001;

//Printing values
cout << m.title << " " << m.year << endl;

return 0;
}

____________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT...

    LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT List by creating a list of movies. This assignment will help you practice: multiple file programming, classes, pablic and private methods, dynamie memory, constructors and destructors, arrays and files Implementation the required classes This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of There are two classes to implement: Movie and MovieList. As you can see...

  • IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

    IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will...

  • [C++] Outline: The movie data is read from a file into an array of structs and...

    [C++] Outline: The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.   Requirements: Your...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class. Create a unit1 object and load it with this data: uld – Pallet abbreviation – PAG uldid – PAG32597IB aircraft - 737 weight – 3321...

  • MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use proper rules for...

    MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use proper rules for data types, class definitions, and inheritance (check input/output of data below). You will create a super media class that will define a general form of media. A media properties will consist of the name and price. The media should be able to construct itself out of the arguments sent to it, virtually print both data fields labeled (price to two decimal places) and overload...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT