CS 202 Homework 0

Dr. Lawlor, lawlor@alaska.edu, 2010-01-21

This document also available electronically at the course website.

HINT: Running both my code, and your new code is highly encouraged!

Turn in a paper copy Thursday 2010-01-28 at the beginning of class.

0.) Your UAF email: ____________@alaska.edu


1.) Why does this code print "3"!?

int a=2, b=3;

if (a=b) { cout<<a; }



2.) Which of these user input loops sometimes skips the cin entirely?

a.) int i; do { cin>>i; } while (i<0 || i>=10);

b.) int i; while (i<0 || i>=10) { cin>>i; }

c.) int i; for (i=-1;i<0||i>10;cin>>i) {}


Why? __________________


3.) This user-input program goes into an infinite loop if you enter "a". Please fix it.

int i=-1; while (i<0 || i>=10) { cout<<"An int?\n"; cin>>i; }




4.) Which implementation is better?

a.) char name[10]; cin>>name;

b.) string name; cin>>name;


Why? __________________


5.) Make this program print the same things, but without ever using "string":

string name; cin>>name;

string sirname="my good friend "+name+", Esq.";

cout<<"Greetings, "+sirname+"! How long have we known each other, "+sirname+"?\n";









6.) Write a function to return the j'th string's k'th letter. Return '@' if j or k is invalid.

struct triString {

string s0;

string s1;

string s2;

};

char extractLetter(triString t,int j,int k) {











}

(For example, "extractLetter(t,2,7)" would return t.s2[7].)


7.) What does this code print? _________

const char *str="mtzlpvk"; const char *ptr=&str[4];

cout<<*ptr++; cout<<*ptr++;



8.) Please rewrite "mystrup" to use simple array indexing, not this weird pointer arithmetic:

void mystrup(char *dest,const char *src) {

while (0!= (*dest++ = toupper(*src++))) {}

}













9.) Which compiler/IDE do you usually use?

__ Visual C++ __ Dev-Cpp __ Code::Blocks __ XCode __ make/g++ __ _________

Which OS do you usually use? __ XP __ Vista __ Windows 7 __ OS X __ Linux

Which languages are you comfortable writing in? (check all that apply)

__ C++ __ C# __ Java __ Plain C __ Perl __ PHP __ Python __ Ruby __ ____________

(Score: 0 points. Pure curiosity on my part, although it does determine what I'll cover...)

Page 2 of 2