CS31 Discussion 1D Fall18: week 2

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Computer Science 1620 Programming & Problem Solving.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 CS161 Introduction to Computer Science Topic #8.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Repetitive Structures
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
CS410 – Software Engineering Lecture #2: Hello, C++ World!
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
Jie(Jay) Wang Week1 Sept. 30
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CMPT 201.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 1.2 Introduction to C++ Programming
CMSC 202 Lesson 2 C++ Primer.
Introduction to Python
Chapter 2 Assignment and Interactive Input
Introduction to C++ October 2, 2017.
Quiz Next Monday.
Some Basics for Problem Analysis and Solutions
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Announcements General rules about homeworks
Today in CS161 Week #3 Learn about… Writing our First Program
We’re moving on to more recap from other programming languages
Wednesday 09/23/13.
Chapter 3 Input output.
Computing Fundamentals
Let’s all Repeat Together
Announcements Homework 1 will be assigned this week,
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Announcements General rules about homeworks
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Subject:Object oriented programming
CS31 Discussion 1D Winter19: week 3
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1H Fall18: week 7
CS31 Discussion 1H Fall18: week 3
CS31 Discussion 1H Fall18: week 1
CS31 Discussion 1H Winter19: week 1
Switch Case Structures
Presentation transcript:

CS31 Discussion 1D Fall18: week 2 TA: Behnam Shahbazi bshahbazi@cs.ucla.edu Credit to former TAs: Chelsea Ju and Bo-Jhang Ho

Slides & Materials? Check my webpage: http://web.cs.ucla.edu/~bshahbazi/teachings/cs31/ cs31-Fall2018 Feel free to look at my slides for last quarter cs31-Winter2019 What we use here in this class. Always check CCLE for projects, worksheets, their solutions and any other materials from the instructor and the other TAs.

Today’s Topic Project 1 String input & output If … Else if ... Else quick review Coding style String input & output Getline(cin, s), cin.ignore(1000,’\n’), … If … Else if ... Else Practice in groups (in order): Worksheet 1 Take home example at the end of this slide HW2 Project 2

Project 1 (with different variable names) – Generate some bugs // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

Precision issue (correct version) int numApprove = 2; int numSurveyed = 7; double pctApprove = 100.0 * numApprove / numSurveyed; cout << pctApprove << endl; The result shows 28.5714

Precision issue (wrong version 1) int numApprove = 2; int numSurveyed = 7; int pctApprove = 100.0 * numApprove / numSurveyed; cout << pctApprove << endl; Instead of 28.5714, the result shows 28 now! It is because we try to score a fraction number into an integer variable

Precision issue (wrong version 2) int numApprove = 2; int numSurveyed = 7; double pctApprove = 100 * numApprove / numSurveyed; cout << pctApprove << endl; We still get 28! C++ is a strongly type language. Everything in C++ has a type. When an integer “interacts” with another integer, it still produces an integer.

Tips for good coding styles Put a newline after each statement A newline after each semicolon Indentation One more tab inside a { } block Make good variable names Ideally the name can describe the intention Put “appropriate” comments Write down some comments to guide readers But don’t be too verbose!

Project 1 – Original version // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

Bad practice 1: No newlines // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? ”; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? ”; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; } It compiles But the code readability is horrible

Bad practice 1: No newlines (improved) // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; } Better, but the “tasks” in the code are not clear

An example of printing a rectangle int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; } Execution result:

Bad practice 2: No indents int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; } It’s hard to see the structure of the code

Project 1 – Original version // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

Bad practice 3: Meaningless variable names // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int a; int b; int c; cout << "How many people were surveyed? "; cin >> a; cout << "How many of them approve of the way the president is handling his job? "; cin >> b; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> c; double d = 100.0 * b / a; double e = 100.0 * c / a; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << d << "% say they approve." << endl; cout << e << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

Project 1 – Original version // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { int numSurveyed; int numApprove; int numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

Add some comments to make it more clear // Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp. 38-39 in Savitch 6/e explains this line int main() { // user input int numSurveyed, numApprove, numDisapprove; cout << "How many people were surveyed? "; cin >> numSurveyed; cout << "How many of them approve of the way the president is handling his job? "; cin >> numApprove; cout << "How many of them disapprove of the way the president is handling his job? "; cin >> numDisapprove; // data processing double pctApprove = 100.0 * numApprove / numSurveyed; double pctDisapprove = 100.0 * numDisapprove / numSurveyed; // output format setup cout.setf(ios::fixed); // see pp. 32-33 in Savitch 6/e cout.precision(1); // output results cout << endl; cout << pctApprove << "% say they approve." << endl; cout << pctDisapprove << "% say they disapprove." << endl; if (numApprove > numDisapprove) cout << "More people approve than disapprove." << endl; else cout << "More people disapprove than approve." << endl; }

How about this kind of comments? // Code for Project 1 // Report poll results #include <iostream> using namespace std; int main() { int numSurveyed; // an integer to store number of people in the survey int numApprove; // an integer to store number of people who approve it int numDisapprove; // an integer to store number of people who disprove it cout << "How many people were surveyed? "; // print "How many people were surveyed?" cin >> numSurveyed; // get user input: number of people in the survey cout << "How many of them approve of the way the president is handling his job? "; // print "How many of them approve of the way the president is handling his job?" cin >> numApprove; // get user input: number of people who approve it cout << "How many of them disapprove of the way the president is handling his job? "; // print "How many of them disapprove of the way the president is handling his job?" cin >> numDisapprove; // get user input: number of people who disapprove it ... }

How about this kind of comments? // Code for Project 1 // Report poll results #include <iostream> using namespace std; int main() { int numSurveyed; // an integer to store number of people in the survey int numApprove; // an integer to store number of people who approve it int numDisapprove; // an integer to store number of people who disprove it cout << "How many people were surveyed? "; // print "How many people were surveyed?" cin >> numSurveyed; // get user input: number of people in the survey cout << "How many of them approve of the way the president is handling his job? "; // print "How many of them approve of the way the president is handling his job?" cin >> numApprove; // get user input: number of people who approve it cout << "How many of them disapprove of the way the president is handling his job? "; // print "How many of them disapprove of the way the president is handling his job?" cin >> numDisapprove; // get user input: number of people who disapprove it ... } The comments provide information no more than the code itself

Why keeping good coding styles? Reduce coding mistakes Make the coding process smoother Helps especially when someone needs to work on your code and improve it.

String Input & Output Reading an input string getline(cin, strVariable) For now use getline to read strings. #include <iostream> #include <string> using namespace std; int main(){ string str; cout << "Please enter a string:"; getline(cin, str); cout << "The string you enter is: " << str << endl; return 0; } Q1. What is the output if we type “CS 31” ? The string you enter is CS 31

String Input & Output Mixing cin and getline() #include <iostream> #include <string> using namespace std; int main(){ int number; string str; cout << "Please enter a number:"; cin >> number; cout << "Please enter a string:"; getline(cin, str); cout << number << ", " << str << endl; return 0; } Since cin ignore the newline (\n) at the end (when we hit enter), this newline is picked up by getline(cin, str). str is directly set to be empty.

String Input & Output A work-around is to use cin.ignore(10000, ‘\n’); What does 10000 means? What if we set it to 2? #include <iostream> #include <string> using namespace std; int main(){ int number; string str; cout << "Please enter a number:"; cin >> number; cout << "Please enter a string:"; cin.ignore(10000, '\n'); getline(cin, str); cout << number << ", " << str << endl; return 0; }

If/Else Statements - Example 01: int main() { if (3 + 5 < 7) cout << "Apple" << endl; else cout << "Banana" << endl; return 0; } What will we get?

Example 01: What will we get? Banana int main() { if (3 + 5 < 7) cout << "Apple" << endl; else cout << "Banana" << endl; return 0; } What will we get? Banana

Example 02: What will we get? int main() { if (3 + 5 < 7) cout << "Apple" << endl; else if (2 * 3 == 6) cout << "Banana" << endl; else cout << "Orange" << endl; return 0; } What will we get?

Example 02: What will we get? Banana int main() { if (3 + 5 < 7) cout << "Apple" << endl; else if (2 * 3 == 6) cout << "Banana" << endl; else cout << "Orange" << endl; return 0; } What will we get? Banana

Practice Time Start in the following order: Worksheet 1 Take home example (check out the next slide) HW2 Project 2

Example 03 (Let’s work on worksheet1 first) Write a program which takes a number between 0 and 100 (a student’s score) and return the letter grade for that score. Score >= 90 -> A Score >= 80 -> B Score >= 70 -> C Score < 70 -> Failed If a student fails, your program needs to ask for the grader’s name. Input your name as a grader, and finally print “INPUT_NAME is the grader.” in which input name is the input string you got. Make sure, you check if the score is in range of 0 to 100, and prompt an error message if it is not.

A common mistake: Write down if but really means else if int main() { int score; cout << "Enter a score: "; cin >> score; if (score >= 90) cout << "Grade: A" << endl; if (score >= 80) cout << "Grade: B" << endl; if (score >= 70) cout << "Grade: C" << endl; else cout << "Failed" << endl; return 0; } int main() { int score; cout << "Enter a score: "; cin >> score; if (score >= 90) cout << "Grade: A" << endl; else if (score >= 80) cout << "Grade: B" << endl; else if (score >= 70) cout << "Grade: C" << endl; else cout << "Failed" << endl; return 0; } Which version of grading programs is correct?

A common mistake: Write down if but really means else if int main() { int score; cout << "Enter a score: "; cin >> score; if (score >= 90) cout << "Grade: A" << endl; if (score >= 80) cout << "Grade: B" << endl; if (score >= 70) cout << "Grade: C" << endl; else cout << "Failed" << endl; return 0; } int main() { int score; cout << "Enter a score: "; cin >> score; if (score >= 90) cout << "Grade: A" << endl; else if (score >= 80) cout << "Grade: B" << endl; else if (score >= 70) cout << "Grade: C" << endl; else cout << "Failed" << endl; return 0; } What if we enter 95? The left one will print 3 lines (Grade A, Grade B, and Grade C), which is not desired