pdfpdf.pdf - lOMoARcPSD|3201619 COMP1511 Week 02 Laboratory Sample Solutions Introduction to Programming(University of New South Wales StuDocu is not

# pdfpdf.pdf - lOMoARcPSD|3201619 COMP1511 Week 02 Laboratory...

• 15

This preview shows page 1 - 4 out of 15 pages.

StuDocu is not sponsored or endorsed by any college or university COMP1511 Week 02 Laboratory Sample Solutions Introduction to Programming (University of New South Wales) Downloaded by control yolo98 ([email protected]) lOMoARcPSD|3201619
19/09/2019 COMP1511 Week 02 Laboratory Sample Solutions 1/14 COMP1511 19t2 Week 02 Laboratory Sample Solutions COMP1511 19t2 Objectives using C input/output (I/O) facilities creating simple arithmetic expressions programming with if statements creating relational expressions displaying varying strings Preparation Before the lab you should re-read the relevant lecture slides and their accompanying examples. Getting Started Create a new directory for this lab called lab02 by typing: \$ mkdir lab02 Change to this directory by typing: \$ cd lab02 Exercise: Don't Be So Negative! (pair) Now create and open a new file called negative.c for this exercise. \$ gedit negative.c & Write a program that uses scanf to get a number from a user and prints "Don't be so negative!" if they entered a negative number. If the number is positive, the program should print "You have entered a positive number." If the user enters the number 0, the program should print "You have entered zero." Note: you can assume that the number will always be a whole number (i.e. an integer) Your program should behave as follows: \$ dcc -o negative negative.c \$ ./negative 3 You have entered a positive number. \$ ./negative -3 Don't be so negative! \$ ./negative 0 You have entered zero. This is a pair exercise to complete with your lab partner. New! You can run an automated code style checker using the following command: Downloaded by control yolo98 ([email protected]) lOMoARcPSD|3201619
19/09/2019 COMP1511 Week 02 Laboratory Sample Solutions 2/14 When you think your program is working you can use autotest to run some simple automated tests: \$ 1511 autotest negative When you are finished on this exercise you and your lab partner must both submit your work by running give : \$ give cs1511 lab02_negative negative.c Note, even though this is a pair exercise, you both must run give from your own account before Monday 17 June 17:00 to obtain the marks for this lab exercise. Sample solution for negative.c // Read in a number, and determine whether it is negative. // If the number is positive, print "You have entered a positive number" // If the number is a zero, print "You have entered zero". // If the number is negative, print "Don't be so negative!" // Sample solution. #include < stdio . h > int main ( void ) { int num ; // Read in a number. scanf ( "%d" , & num ); // Print the relevant message. if ( num > 0 ) { printf ( "You have entered a positive number.\n" ); } else if ( num == 0 ) { printf ( "You have entered zero.\n" ); } else { printf ( "Don't be so negative!\n" ); } return 0 ; } \$ 1511 style negative.c Exercise: Icecream Scoops (pair) Create and open a new file called icecream.c for this exercise.

#### You've reached the end of your free preview.

Want to read all 15 pages?

• Fall '19

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern