w3bw

22
1 WORKSHOP 3 ~ Loops COMP 1117B 510/2015

Upload: kenneth-wong

Post on 26-Jan-2016

214 views

Category:

Documents


0 download

DESCRIPTION

wwww

TRANSCRIPT

Page 1: w3bw

1

WORKSHOP 3 ~ LoopsCOMP 1117B 510/2015

Page 2: w3bw

2

OUTLINE

Assignment 1 Feedback

Loops While For

Exercises

Page 3: w3bw

3

ASSIGNMENT 1 TEST CASES

Page 4: w3bw

4

COMMON MISTAKES

Incorrect filenames @-2%

Examples: UNo-dive.cpp Uno-lb.cpp dive_.cpp dive (2).cpp dive_assessment.cpp bl.cpp diving.cpp

Page 5: w3bw

5

COMMON MISTAKES

Incorrect Results Example: Set the input to be integer variable

Page 6: w3bw

6

COMMON MISTAKES

Incorrect Input/Output format

Page 7: w3bw

7

COMMON MISTAKES

Spacing

Poor indentation

Page 8: w3bw

8

COMMON MISTAKES

Poor indentation

Missing documentation

Page 9: w3bw

9

CHECK YOUR MARKS AND COMMENTS

In Moodle, choose “Administration” “Grades”

Marks and Comments are given

Comment format like: lb.cpp (xx): Incorrect result for test cases

2, 3, 4. dive.cpp (yy): Incorrect result for test cases 1, 2. Warnings: Poor indentation.

xx – marks for lb.cpp, maximum 30%yy – marks for dive.cpp, maximum 70%Warnings: marks will not be deducted for this first assignment

Page 10: w3bw

10

SAMPLE SOLUTIONS// COMP1117B Assignment 1 Question 1

// Convert kg to lb

#include <iostream>

using namespace std;

int main()

{

double kg = 0.0;

cin >> kg;

cout << kg * 2.2046;

return 0;

}

Page 11: w3bw

11

SAMPLE SOLUTIONS// COMP 1117B Assignment 1 Question 2

// calculating diving score

#include <iostream>

using namespace std;

int main()

{

int m1, m2, m3, m4, m5, h, l;

m1 = m2 = m3 = m4 = m5 = h = l = 0;

cin >> m1 >> m2 >> m3 >> m4 >> m5;

// Discard the highest and lowest scores

h = l = m1;

if (m2 > h)

h = m2;

else if (m2 < l)

l = m2;

if (m3 > h)

h = m3;

else if (m3 < l)

l = m3;

if (m4 > h)

h = m4;

else if (m4 < l)

l = m4;

if (m5 > h)

h = m5;

else if (m5 < l)

l = m5;

cout << m1 + m2 + m3 + m4 + m5 - h - l << endl;

return 0;

}

Page 12: w3bw

12

LOGIC ERROR

Page 13: w3bw

13

LOOPS

While loop (Chapter 3 Page 24)

Page 14: w3bw

14

LOOPS

For loop (Chapter 3 Page 32)

Page 15: w3bw

15

EXAMPLE

Write a program to read 7 integers and then only report the average of the first 3 positive numbers.

Page 16: w3bw

16

SAMPLE RUNS

Page 17: w3bw

17

WRITTEN EXERCISE

What is the output of the following program? int a = 1; int b = 2;

if (a = b) cout << "Same!\n"; else cout << "Different!\n";

if (a == b)

Page 18: w3bw

EXERCISE 1

Write a program (game.cpp) to ask the user to input a character until he/she input ‘!’. Print how many characters did the user input before entering ‘!’. (Ignore the space and newline)

Input: Ask the user to input a character (repeatedly until encountered ‘!’)

Process: Count the number of characters

Output: The count18

Page 19: w3bw

19

SAMPLE RUNS

Page 20: w3bw

20

EXERCISE 2

Let’s implement another game. Let’s set the secret number to be “66”

Write a program to ask the user to input a number between 1-100.

If the input number exactly same as the secret number, then print “Bingo” message.

Else tell the user to try again with hints

Page 21: w3bw

21

SAMPLE RUNS

Page 22: w3bw

22

THANKS!