Title | COMP1511 Week 02 Laboratory Sample Solutions |
---|---|
Course | Introduction to Programming |
Institution | University of New South Wales |
Pages | 14 |
File Size | 598.3 KB |
File Type | |
Total Downloads | 113 |
Total Views | 145 |
LAB...
19/09/2019
COMP1511 Week 02 Laboratory Sample Solutions
COMP1511 19t2
Week 02
Laboratory
Sample Solutions
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) This is a pair exercise to complete with your lab partner.
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 number. If the number is positive, the program should print "You have entered a positive number."
19/09/2019
COMP1511 Week 02 Laboratory Sample Solutions
$ 1511 style negative.c
When you think your program is working you can use autotest to run some simple automated te
$ 1511 autotest negative When you are finished on this exercise you and your lab partner must both submit your work by ru
$ give cs1511 lab02_negative negative.c Note, even though this is a pair exercise, you both must run give from your own account before M 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 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; }
Exercise: Icecream Scoops (pair) This is a pair exercise to complete with your lab partner
19/09/2019
COMP1511 Week 02 Laboratory Sample Solutions
How many dollars does each scoop cost? 3 Oh no, you don't have enough money :( You can assume that Matilda will only give you positive integers.
New! You can run an automated code style checker using the following command:
$ 1511 style icecream.c
When you think your program is working you can use autotest to run some simple automated te
$ 1511 autotest icecream When you are finished on this exercise you and your lab partner must both submit your work by ru
$ give cs1511 lab02_icecream icecream.c Note, even though this is a pair exercise, you both must run give from your own account before M obtain the marks for this lab exercise.
Sample solution for icecream.c // icecream: a sample solution. // Tells Matilda whether she has enough money for icecream. #include #define MAXIMUM_COST 10 int main(void) { int cost, scoops; // Read in the number of scoops, and the cost of each scoop. printf("How many scoops? "); scanf("%d", &scoops); printf("How many dollars does each scoop cost? "); scanf("%d", &cost); // Calculate the total cost. int totalCost = scoops * cost; // Does she have enough money? If so, print "You have enough money!" // Otherwise, print "Oh no, you don't have enough money :(". if (totalCost = -10 && sum...