assn1 - MIT OpenCourseWare http:/ocw.mit.edu 6.096...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.096: Introduction to C++ IAP 2009 PROBLEM SET 1 Standard namespace: Recall from lecture that after you tell the preprocessor to use functions from the standard input/output library ( #include <iostream> ), you need to tell the compiler that the functions you’ll be using are defined in the “standard namespace” (a language feature we won’t go into in detail), which you do with “ using namespace std; ”. Another way to access the functions in the standard namespace is to prefix each one with the name of the namespace. For instance, you can skip the using statement if you write std::cout and std::cin every time you want to print or input text. Problem 1 Look at the following program and try to guess what it does without running it. #include <iostream> int main() { int x = 5; int y = 7; std::cout << "\n"; std::cout << x + y << " " << x * y; std::cout << "\n"; return 0; } Problem 2 Put the following program in a file called buggy.cpp and compile it. (“.cpp” is the standard suffix for C++ code files.) What errors do you receive? How would you fix them? (You may need to correct and recompile several times before all the problems are fixed.) #include <iostream> int main() { cout < "Hello World\n; return 0 } Problem 3
Background image of page 2
Write a program that writes “I love C++” to the screen. char * data type: One data type that we discussed only implicitly in lecture is strings. Strings are just sequences of characters, which are denoted by quotation marks – e.g. "Hello world!" is a string, as are "\n" , "The Noble Duke of York" , and so on. (Note that strings use double quotes, whereas lone characters, such as '\n'
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

assn1 - MIT OpenCourseWare http:/ocw.mit.edu 6.096...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online