Embed
Email

PIC 10A Introduction to Programming UCLA Math Department Summer

Document Sample
PIC 10A Introduction to Programming UCLA Math Department Summer
PIC 10A: Introduction to Programming

UCLA Math Department

Summer 2009

Homework 7– Pointers

Due Tuesday, August 11, 2009 4:00 PM Sharp



Implement a class Person with the following variables:

• the name



• a pointer to the person’s best friend (a Person*)



• a popularity counter that indicates how many other people have this person as their best

friend



Write a program that reads in a list of names, creates a Person for each one, and stores them

in vector. Then ask the name of the best friend for each of the Person objects. Locate

the object matching the friend’s name and call a set_best_friend member function to update

the pointer and counter. Finally, print out all Person objects, listing the name, best friend, and

popularity counter for each.

Your .h, .cpp and .txt files must have the following comments at the very top:



//

//PIC 10A Summer 2009

//Homework

//Functionality:



Additional questions

The purpose of these questions is to help you master pointers. You will get the most out of these

questions if you first try to answer these questions by hand (there will be similar problems on the

final exam). Only after that should you attempt to write a program to check your answers.



1. Correct the errors in the following program by writing a fixed version of the code. Please

make as few changes as possible.



(a) This program is supposed to print out 14 7 0, but does not. Find all of the bugs and

write a fixed version of the program.

int main() {

int a[]={0,1,2};

int* ptr = a;



*ptr *= 7; // set a[0] to 0;

*ptr + 1 *= 7 // set a[1] to 7

ptr += 2;

ptr[0] *= 7 // set a[2] to 14

while (ptr >= a) {

--ptr;

cout << *ptr << endl;

}

cout << endl;

}

(b) The match function is supposed to return true if and only if its two C string arguments

have the same text. Write a corrected version of the following program

//return true if two C strings are equal

bool match(const char str1[], const char str2[]) {

while (str1 != 0 && str2 != 0) { // zero bytes at ends

if (str1 != str2) // compare corresponding characters

return false;

str1++; // advance to next character

str2++;

}

return str1 == str2; // both ended at the same time?

}



int main() {

char a[10] = "PIC 10A";

char b[10] = "PIC 10B";



if (match(a,b)) cout << "The two strings are the same\n";

else cout << "The two strings are different\n";



return 0;

}





2. For each of the following parts, write a single C++ statement that performs the indicated

task. For each part, assume that all previous statements have been executed.



(a) Declare teams to be a 4-element array of strings.

(b) Declare a pointer variable named ptr that can point to a variable of type string and

make it point to the last element of teams.

(c) Make the string pointed to by ptr equal to “Lions”, using the * operator.

(d) Without using ptr and without using [] set the element at index 2 of the team array

equal to “Bears”.

(e) Move ptr back by three strings.

(f) Using square brackets, but without using the name teams set the element at index 1 of

the teams array to have the value “Vikings”.

(g) Without using *, but using square backets, set the string pointed to by ptr to have the

value “Cowboys”.

3. (Bonus) Rewrite the following function so that it returns the same result, but does not

increment the variable ptr. Your new program must not use any square brackets, but must

use an integer variable to visit each double in the array. You may eliminate any unneeded

variables.



double average(const double* scores, int num_scores) {

const double* ptr = scores;

double total = 0;



while(ptr != scores + num_scores) {

total += *ptr;

++ptr;

}

return total / num_scores;

}



Materials to Turn in Using Your Submit Folder (Use the exact same filenames!!)

1. hwk7.h contains the declarations of your classes.



2. hwk7.cpp contains the definitions of your classes and main()



3. hwk7 readme.txt contains answers to the questions above, comments on the project, and any

difficulties encountered.



Grading

• Readability – 10%



• Correctness of the program – 40%



• Readme file – 50%



• Bonus – 5%


Related docs
Other docs by rebeccaGerritY
INTRODUCTION TO POLITICS Fall Semester 2009
Views: 2  |  Downloads: 0
Contract of Submission
Views: 9  |  Downloads: 1
Introduction to Hedge Funds
Views: 33  |  Downloads: 2
Annual Report of Caucus Activities
Views: 3  |  Downloads: 0
Introduction to the Earth
Views: 8  |  Downloads: 0
Feedback report on consultation
Views: 6  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!