w3bw
DESCRIPTION
wwwwTRANSCRIPT
1
WORKSHOP 3 ~ LoopsCOMP 1117B 510/2015
2
OUTLINE
Assignment 1 Feedback
Loops While For
Exercises
3
ASSIGNMENT 1 TEST CASES
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
5
COMMON MISTAKES
Incorrect Results Example: Set the input to be integer variable
6
COMMON MISTAKES
Incorrect Input/Output format
7
COMMON MISTAKES
Spacing
Poor indentation
8
COMMON MISTAKES
Poor indentation
Missing documentation
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
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;
}
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;
}
12
LOGIC ERROR
13
LOOPS
While loop (Chapter 3 Page 24)
14
LOOPS
For loop (Chapter 3 Page 32)
15
EXAMPLE
Write a program to read 7 integers and then only report the average of the first 3 positive numbers.
16
SAMPLE RUNS
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)
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
19
SAMPLE RUNS
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
21
SAMPLE RUNS
22
THANKS!