In: Computer Science
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)
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;
}