CPSC 152 Homework Assignments #1
Submission: Hard copy @ 9:30 AM, 1/14/2013
1. Write outputs in the corresponding box for each code segment.
a) [6 points]
int x = 50, y = 60, z = 70;
int *ptr;
cout << x << " " << y << " " << z << endl;
ptr = &x;
*ptr *= 10;
ptr = &y;
*ptr *= 5;
ptr = &z;
*ptr *= 2;
cout << x << " " << y << " " << z << endl;
b) [3 points]
int A[] = {2, 4, 6, 8, 10};
cout << *(A + 3) << endl;
int* ptr = A;
cout << *ptr << endl;
ptr += 4;
cout << *ptr << endl;
ptr -= 2;
cout << *ptr << endl;
50 60 70
500 300 140
8
2
10
6
2. Complete the following program skeleton. When finished, the program will ask the user for a length
(in inches), convert that value to centimeters, and display the result. You are to write the function
convert. (Note: 1 inch = 2.54 cm. Do not modify function main.) [5 points]
#include <iostream>
#include <iomanip>
using namespace std;
//Write your funciton prototype here
void convert(double* x);
void convert(double*); //both are correct
int main() {
double mea;
cout << "Enter a length in inches\n";
cin >> mea;
convert(&mea);
cout << fixed << setprecision(4);
cout << "Length in cenitmeters: " << mea << endl;
return 0;
}
//Write the function convert here.
void convert(double* x) {
const double a = 2.54;
*x *= a;
}
© Copyright 2026 Paperzz