Question

In: Computer Science

Instructions: Submit to this page (from Part I) with your answers and the C++ program from...

Instructions:

Submit to this page (from Part I) with your answers and the C++ program from Part II to the Blackboard.

Type all answers in this page in RED. Part I Pointers to variables or places in RAM (Random Access Memory) are memory addresses – the address of the byte of memory to which the pointer is referring (or “referencing”).

Some programming languages use pointers to increase efficiency, to make changes to variables indirectly, etc.

In C++, if Z is an integer variable, then &Z is the memory address of that variable.

For example if we have: int Z = 100; cout << &Z << endl; The output may look like 0x38ff64. “38ff64” would be the byte address in hexadecimal form.

Hexadecimal is just a base 16 integer with 16 digits: 0 through 9 and ‘a’ through ‘f’ all to represent 0 through 15.

Pointer variables in C++ are variables that hold memory addresses.

Example int X = 1000; //line 1 int A = 200; //line 2 int B = 300; //line 3 int C = 100; //line 4 int *p = &B; //line 5 p is a pointer variable cout << p << endl; //line 6 p++; //line 7 cout << p << endl; //line 8 This means that p is a pointer originally initialized to hold the memory address of B.

(10 points)

1. What compiler are you using? Circle one DEV C++, Visual, XCODE, some other (10 points)

2. Place these eight lines of code into a new program. What is your output? _______________ (10 points)

3. By how much did p change from the first to the second output? lines 6 and 8: ________________ (NOTE: In Hexadecimal 0 is the first digit and f is the last; so, f – 3 is d and f+3 is 2.) (10 points)

4. Is p now the memory address of A or B or C? (which one or none) _____________________ [2]

Let’s try accessing a memory location indirectly – by pointers.

Consider the following code: int *q; q = new int; q will now point to a piece of memory with no actual name. The location of that memory location is in the value of q.

That memory can be manipulated by “dereferencing” q like the following: *q = -200; That line of code assigned -200 to the piece of memory that has its address stored in q.

That piece of memory can be changed like (*q)++ will increment the value by one. (10 points)

5. What is the new value of *q? _____________________ This means, if I have the following code, there will be some changes to A. int A = 650; int * p = &A; *p = *p – 200; cout << *p << endl; (10 points)

6. What is the value of variable A + *p? _________________ Pointers and structs are very important in C++ and many other languages. The following example defines a data type “PAIR” containing a double field called D and an int field called I. struct PAIR { double D; int I; }; PAIR two; //two is just a variable of type PAIR PAIR * pairptr = &two; Variable two is of type PAIR. pairptr is a pointer to that variable. *pairptr refers to the entire variable two. (*pairptr).D refers only to the double field D. So, you could assign a value to D by either two.D = 100.11 or (*pairptr).D = 100.11. Often in programming the pointer is only pointing to a memory location without a variable name. So the dereferencing (*pairptr).D = 100.11; would have to be used. This is a bit clumsy in C++; so, the language uses an arrow notation like “pairptr -> D = 100.11;” (10 points) 7. How can you assign the “int I” field to -5555 in two different ways? a._____________________ b._____________________ [3] Part II – (30 points) Design a program to calculate the cost of gas for a trip. The user will be prompted for the cost of a gallon of gas, the number of miles of the trip, and the number of miles per gallon the car gets. The program should use pointers, the address operator, and the dereferencing operator for ALL input, calculations, and output. Enter, compile, link, and execute Lab7GasTripFirstInitialLastName.cpp The following is a sample output that might appear after running your program. Input by the user is shown in bold. This program calculates the cost of gas for a trip when the user enters the cost of gas, the number of miles, and the number of miles per gallon the vehicle gets. Enter the number of miles in your trip: 890 Enter the mpg your car gets: 23 Enter the cost of gasoline: 1.79

Your trip will cost $68.02

Hint: the trip cost = cost of gasoline * (number of miles / mpg your car gets)

Solutions

Expert Solution

Code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main(){

//Declare the pointer variables
float miles,mpg,cost;
float result;
float *m,*mp,*c;

//reading input from user
cout<<"Enter the number of miles in your trip: ";
cin>>miles;

//assign value to a pointer
m = &miles;

cout<<"Enter the mpg your car gets: ";
cin>>mpg;

//assign value to a pointer
mp = &mpg;

cout<<"Enter the cost of gasoline: ";
cin>>cost;

//assign value to a pointer
c = &cost;

//calculate the result and print it
result = *c * (*m/ *mp);

cout<<fixed<<setprecision(2)<<"Your trip will cost $"<<result;
return 0;
}



Related Solutions

Instructions: Format each of your Answers as follows: (A) Definition (quote from the Textbook, with page...
Instructions: Format each of your Answers as follows: (A) Definition (quote from the Textbook, with page number, OR quote from another permitted source -- i.e. an online law dictionary, with proper source citation ) (B) Explanation in your own words (C) Example (either your own, OR one that is in the Text, OR one that is in another permitted source, with proper source citation) (D) Find a website that supports and further explains your answer (not one that I have...
Instructions: Read the problemn and complete all the questions included below. Submit your answers on a...
Instructions: Read the problemn and complete all the questions included below. Submit your answers on a Word (.doc) or Excel (.xls) document Check the Rubric As a consultant for Acme Engineering you have been able to establish the following parameters from their Financial Statements: Item Amount Cash $200,000 Securities $90,000 Accounts Receivable $300,000 Inventories $400,000 Prepaid Expenses $16,000 Accounts Payable $630,000 Other Liabilities $180,000 Calculate the following parameters: Total Assets Total Liabilities Working Capital Current Ratio Acid Test Ratio
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
INSTRUCTIONS Read the following case study about Amazon, and then submit a posting with answers to...
INSTRUCTIONS Read the following case study about Amazon, and then submit a posting with answers to the 5 questions below. Your answers will be graded primarily on content, but grammar, spelling, syntax, etc will also count. Please make sure that your answers are labeled to match the Question numbers so that I can easily read (and grade) your submissions CASE STUDY: THE AMAZON OF INNOVATION On December 2, 2013, Amazon.com customers ordered 36.8 million items worldwide, an average of 426...
** I NEED THE ANSWERS BASED ON THE SCENARIO*** Instructions: Review your required readings related to...
** I NEED THE ANSWERS BASED ON THE SCENARIO*** Instructions: Review your required readings related to CHF and answer the following questions. Read the following scenario and answer the following questons, Please submit it in the course shell under this assignment See rubric in a separate file Scenario. ***I NEED THE ANSWERS BASED ON THE SCENARIO*** You are caring for a 78-year-old woman, Ms. Peterson who is admitted to the hospital with shortness of breath. She has a history of...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1: Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today. Option...
You are to prepare and submit a two-three-page paper that will critique the in-service program and...
You are to prepare and submit a two-three-page paper that will critique the in-service program and process based on the following headings: Can some help me start a paper Your Presentation : HOW COVID 19 AFFECT PEOPLE WITHDISABILITIES Your Group - registration The In-Service as a Whole Recommendations for Class 2021 based upon lessons learned Creation of a Dashboard based upon the totality of the experience. This dashboard should include key indicators of the in-service preparation process, risk areas and...
a) Submit a copy of your dataset along with a file that contains your answers to...
a) Submit a copy of your dataset along with a file that contains your answers to all of the following questions. b) What the mean and Standard Deviation (SD) of the Close column in your data set? c) If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed at less than the mean for that year? Hint: You do not want to calculate the mean to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT