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...
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...
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...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 4. (40 points) Determine the number of statement executions (precise big-Oh) for each of the following sample code, as described in the lecture. Your answers should be in the form of a Big-Oh polynomial (e.g., O(3N2 + 7N + 6)). Sample #1: for (int...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 1. (12 points) State the order of magnitude for each of the following mathematical functions. (Hint: Find the dominant term and drop its coefficient) 5n2 + 105 nlogn 5n3 – 7n + 30 (n2 / logn)+ 40000n + 1000 (5n2 + 8n + 3n)...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT