Download presentation
Presentation is loading. Please wait.
1
CSCE 206 Lab Structured Programming in C
Spring 2019 HW2 COMMON ERRORS
2
File submission Still some files not in a .zip
Still naming issues (like HW instead of HW2)
3
Compilation Warnings DO matter
Just google the warning and figure it out! They provide hints on why your program might not work HW2_Q5.c:14:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=] scanf("%d", number); What is wrong with it?
4
Code mistakes and improvements
if statements ALWAYS use braces { } else or else-if statements should go immediately after the closing brace } (either same or next line) Two good examples: Mistake: Not follow directions Do not ask for input if not specified in the assignment! Stop the loop if you have the information already (e.g. you know it is not prime) Use break and continue! if (year%2 == 0) //comment here { // or comment here // or comment here print("divisible by 2”); } else { print("not divisible by 2”); if (year%2 == 0) { //comment here // or comment here print("divisible by 2”); } else { print("not divisible by 2”); }
5
Code comments As per the instructions in my website you need to put a block comment at the start of the file: Comments are placed on or before the line they are about (never afterwards!) All hardcoded values need to be explained! A good comment includes to explain HOW the algorithm works (e.g. to find the minimum number of 3 numbers I use a one level if- elseif-else structure. When we know the minimum, I find the middle and the max with one nested if-else). WHY you chose that implementation (e.g. why for vs do-while, why you chose that condition to stop the loop)
6
Q5 Test more inputs Look at the definition of a prime number
1 is not a prime number! 2 and 3 are prime numbers! Negative numbers are NOT prime!
7
Q2: example of how to approach
Sort 3 numbers if “a”: if (a < b && a < c) Max? Min? main if “a” if “c” a < b < c else (“b”) a < c < b else if “b” b < a < c b < c < a else (“c”) c < b < a if “b” c < a < b
8
Stuck? ASK!! Try to think on how to solve the algorithm
Sketch the idea before coding like in previous slide Make sure you know how to do the math by hand before trying to code it If you don’t know how to do it by hand don’t expect it will work on the computer! If you don’t know how to approach it, ask! By By Google Hangouts/Chat In Office hours In the lab
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.