Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31 Discussion 1D Fall18: week 2

Similar presentations


Presentation on theme: "CS31 Discussion 1D Fall18: week 2"— Presentation transcript:

1 CS31 Discussion 1D Fall18: week 2
TA: Behnam Shahbazi Credit to former TAs: Chelsea Ju and Bo-Jhang Ho

2 Slides & Materials? Check my webpage:
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.

3 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

4 Project 1 (with different variable names) – Generate some bugs
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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; }

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

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

7 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.

8 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!

9 Project 1 – Original version
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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; }

10 Bad practice 1: No newlines
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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

11 Bad practice 1: No newlines (improved)
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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

12 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:

13 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

14 Project 1 – Original version
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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; }

15 Bad practice 3: Meaningless variable names
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * b / a; double e = * c / a; cout.setf(ios::fixed); // see pp 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; }

16 Project 1 – Original version
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; cout.setf(ios::fixed); // see pp 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; }

17 Add some comments to make it more clear
// Code for Project 1 // Report poll results #include <iostream> using namespace std; // pp 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 = * numApprove / numSurveyed; double pctDisapprove = * numDisapprove / numSurveyed; // output format setup cout.setf(ios::fixed); // see pp 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; }

18 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 ... }

19 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

20 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.

21 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

22 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.

23 String Input & Output A work-around is to use cin.ignore(10000, ‘\n’);
What does 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; }

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

25 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

26 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?

27 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

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

29 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.

30 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?

31 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


Download ppt "CS31 Discussion 1D Fall18: week 2"

Similar presentations


Ads by Google