Week 04 Laboratory Exercises

Objectives

  • using arrays
  • using struct arrays
  • creating functions
  • using while loops for repetition

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth one mark in total:

  • print_pi
  • circle_facts
  • painterbot
  • hotel_rooms

Worth half a mark in total:

  • cs_calculator
  • reverse_array

Worth half a mark in total:

  • going_electric

For your interest, but not for marks:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Exercise
(●◌◌)
:

Print out pi to a certain number of digits

Write a C program, print_pi.c, which prints out the first n digits of pi, where n is specified by the user.

Download print_pi.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/21T3/activities/print_pi/print_pi.c .

The output from your program should look exactly like this:

dcc print_pi.c -o print_pi
./print_pi
How many digits of pi would you like to print? 3
3.14
./print_pi
How many digits of pi would you like to print? 5
3.1415
./print_pi
How many digits of pi would you like to print? 10
3.141592653

You can assume that n >= 2 AND n <= 10, however, do not use this as a reason to avoid while loops.

Need a Hint?

The digits of pi are stored in the array called pi. While loops are very handy for traversing arrays.

You can run an automated code style checker using the following command:
1511 style print_pi.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest print_pi

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_print_pi print_pi.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Fun facts about circles

In this exercises you will add code to a program to calculate fun facts about circles.

Download circle_facts.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/21T3/activities/circle_facts/circle_facts.c .

Your task is to add code to these functions in circle_facts.c:

// Calculate the area of the given circle and return it.
double area(struct circle circ) {
    // TODO: complete this function.
    return M_PI;
}
// Calculate the circumference of the given circle and return it.
double circumference(struct circle circ) {
    // TODO: complete this function.
    return M_PI;
}
// Add the given coordinates to those in the given circle. Return the circle
// after this addition.
struct circle add_position(struct circle circ, int x, int y) {
    // TODO: complete this function.
    return circ;
}

Its main function is complete. Do not change the main function. Complete these three functions:

double area(struct circle circ);
double circumference(struct circle circ);
struct circle add_position(struct circle circ, int x, int y);
dcc -o circle_facts  circle_facts.c 
./circle_facts 
Enter circle radius: 1
Enter circle position (x, y): 0 0
Enter an offset for the circle (x, y): 0 0

Area             = 3.141593
Circumference    = 6.283185
Initial position = (0, 0)
Offset position  = (0, 0)
./circle_facts
Enter circle radius: 3
Enter circle position (x, y): 0 0
Enter an offset for the circle (x, y): 0 0

Area             = 28.274334
Circumference    = 18.849556
Initial position = (0, 0)
Offset position  = (0, 0)
./circle_facts
Enter circle radius: 1
Enter circle position (x, y): 3 5
Enter an offset for the circle (x, y): 2 3

Area             = 3.141593
Circumference    = 6.283185
Initial position = (3, 5)
Offset position  = (5, 8)
./circle_facts
Enter circle radius: 5.2
Enter circle position (x, y): 2 5
Enter an offset for the circle (x, y): 0 2

Area             = 84.948665
Circumference    = 32.672564
Initial position = (2, 5)
Offset position  = (2, 7)
./circle_facts
Enter circle radius: 7
Enter circle position (x, y): 3 -3
Enter an offset for the circle (x, y): -2 5

Area             = 153.938040
Circumference    = 43.982297
Initial position = (3, -3)
Offset position  = (1, 2)
You can run an automated code style checker using the following command:
1511 style circle_facts.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest circle_facts

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_circle_facts circle_facts.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Scan in indices and replace corresponding elements

For this exercise, make a program called painterbot.c which will scan in indices until EOF and then print out a one dimensional array which has 36 '0's, except at the given indices it has a '1'.

You can assume that every number you are given will be between 0 and 35. You can assume you will never get the same number twice.

./painterbot
5
2
4

0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
./painterbot
0
1
2
3
4
5
6
7
8
9

1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Need a Hint?

The result from calling scanf can be assigned to an integer, like so:

int scanned_in_value;
int result = scanf("%d", &scanned_in_value);

result will be equal to the number of variables that scanf successfully scanned in.

Explanation
In the code sample above, if the user does enter a number, scanf will be able to read this in to the variable scanned_in_value, and thus it will have successfully scanned in one value (since there is only one %d, so the variable result will contain the value 1.

If the user had not entered a valid number (for example if they typed in a word instead), there would be no integer there for scanf to read into scanned_in_value, so scanf would not successfully scan in any values, and so the variable result would contain a different value.

Detecting the end of input
The variable result can be used to determine whether your program has reached the end of the input (hint: if there aren't any values left for scanf to scan in, the result variable won't contain the number 1).

If you have a while loop condition that checks the result of scanf to determine whether scanf was able to successfully scan the expected number of variables, you can use this information to keep scanning numbers until the user tells you to stop.

Note: on Mac/Linux, you can signal "end of input" on the command line by pressing Control + D

You can run an automated code style checker using the following command:
1511 style painterbot.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest painterbot

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_painterbot painterbot.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Using struct arrays to help with travel plans

Write a C program hotel_rooms.c which scans in a certain amount of hotel rooms and determines which are suitable according to how many people the user has in their group.

Download hotel_rooms.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/21T3/activities/hotel_rooms/hotel_rooms.c .

First, it scans in the number of rooms in the hotel. Then, it scans in the details of each room: its number, its capacity and its price. Afterward, it scans in how many people the user has in their group. Finally, it prints out all the rooms it scanned in that can fit that many people.

./hotel_rooms
How many rooms? 3
Enter hotel rooms:
101 2 123.45
201 4 399.99
301 8 699.99
How many people? 3
Rooms that fit your group:
Room 201 (4 people) @ $399.99
Room 301 (8 people) @ $699.99
./hotel_rooms
How many rooms? 5
Enter hotel rooms:
101 2 299.99
102 4 399.99
201 2 349.99
202 4 430.0
301 8 667.66
How many people? 4
Rooms that fit your group:
Room 102 (4 people) @ $399.99
Room 202 (4 people) @ $430.00
Room 301 (8 people) @ $667.66
./hotel_rooms
How many rooms? 2
Enter hotel rooms:
101 2 299.99
102 2 349.99
How many people? 3
Rooms that fit your group:

Assumptions/Restrictions/Hints

  • When asked for how many rooms and people, you will be given a non-zero positive integer.
  • All inputs will be the correct type and non-zero positive numbers.
  • The number of rooms in a hotel will never be greater than 50.
  • You will never scan in the same room number twice.
  • The rooms will be always given in ascending order by their room number.
You can run an automated code style checker using the following command:
1511 style hotel_rooms.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest hotel_rooms

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_hotel_rooms hotel_rooms.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Create a simple calculator, reading different numbers of integers

For this exercise, make a program called cs_calculator.c which will scan in instructions until EOF and prints the output as specified below. An instruction is a sequence of positive integers. The first integer identifies what type the instruction is.

  • If the first number in the instruction is 1, then your program should print out the square of the next number in the instruction.
  • If the first number in the instruction is 2, then your program should print out the value of the next number raised to the power of the number after next.

You can assume that the first number in the instruction is only either 1 or 2

You can assume that for each instruction, the correct number of successive positive integers will be given.

./cs_calculator
Enter instruction: 1 2
4
Enter instruction: 2 5 3
125
Enter instruction: 1 4
16
Enter instruction: 2 3 4
81
Enter instruction: 
./cs_calculator
Enter instruction: 2 3 3
27
Enter instruction: 1 10
100
Enter instruction: 

Note: The autotest for this exercise expects your program to end WITHOUT a new line character when the user presses Ctrl+D. This means that the command prompt for the next command should be on the same line as the end of your program.

One major challenge of this exercise is figuring out how to use scanf effectively. The lessons you learn in this exercise regarding scanf will be useful in the first assignment.

Hint: You can scan in the first number initially, and based on the value of that first number then scan in the correct number of additional numbers.

Hint: scanf will jump over and ignore any whitespace characters including newline characters. Be careful not to overrun or underrun your input.

There is no need to print a newline character after the user presses Ctrl+D

You can run an automated code style checker using the following command:
1511 style cs_calculator.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest cs_calculator

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_cs_calculator cs_calculator.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Reverse an array

Write a C program, reverse_array.c, which reads integers line by line, and when it reaches the end of input, prints those integers in reverse order, line by line.

You will never be given more than 100 integers to print out.

The output from your program should look exactly like this:

dcc reverse_array.c -o reverse_array
./reverse_array
Enter numbers forwards:
10
50
20
40 

Reversed:
40
20
50
10
./reverse_array
Enter numbers forwards:
-5
-4
-3
-2
-1 

Reversed:
-1
-2
-3
-4
-5
You can run an automated code style checker using the following command:
1511 style reverse_array.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest reverse_array

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_reverse_array reverse_array.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Help route an electric car along a long road

This challenge does not provide comprehensive test cases

You have been provided some autotests to check your output is sane, and that it is in a correct format. Passing the autotests does not guarantee that you have solved this challenge -- your own testing will be required.

You are in charge of planning a route for an electric car across a long road.

Your electric car takes exactly one unit of charge to travel one kilometer. It has infinite battery capacity, but starts off empty.

Conveniently, every kilometer along this road, there is a charging station, where you can stop to charge your car. These charging stations may have a limited of supply of charge you can use to charge your car. A charging station may have no charge available.

Your job is to determine if it is possible to drive your car to the last charging station on the road, and if so, what the minimum number of stops is to get your car there.

Input Format

You should write a C program, going_electric.c. This program will be provided a series of numbers. Each number represents the charge available from a given charging station. The first number given is the charge of the first station (where your car begins its journey). The second number given is the charge at the second station, and so on.

Output Format

Your program should print a single integer, the minimum number of charging stops required to cross the road. This should include the initial charging stop. If it is not possible to drive all the way along the road, you should print the integer 0.

Examples

dcc going_electric.c -o going_electric
./going_electric
2 0 0

1

In the above example, the car had to charge at the first station. It then had 2 units of charge, which let it reach the final station. Note that even though that final charging station had no charge to give, the car successfully reached it (just), so this journey is possible.

./going_electric
2 0 0 3 0

0

In the above example, the car could not make it to the last charging station -- the two units of charge at the first station aren't enough to drive to the next charging station with more charge.

./going_electric
1 1 1 1 0

4

In the above example, the car charges at each of the four stations it can. In this way, it just makes it to the final charging station.

./going_electric
3 3 2 1 0

2

In the above example, the car must charge twice, but there are three possible ways it could do so -- it must charge at the first station, but then it could charge at any of the others (excluding the last).

Assumptions, Restrictions & Clarifications

  • The road will have no more than 10000 charging stations.
  • The road is longer than 1 kilometer (that is, it has at least two charging stations).
  • A charging station always has a non-negative amount of charge (that is, either a charging station has a positive amount of charge, or no charge at all).

An Extra Challenge (☠)

As backstory, this challenge was written in a single night, after I nerd-sniped myself.

I'm pretty sure the solution I have to this question is correct, but I invite anyone who is really interested in this problem to submit a proof of their code being correct. If doing so interests you, you may find the courses COMP3121, COMP4161, COMP2111 and COMP6721 interesting.

Submit your proof via the cs1511.challenge@cse.unsw.edu.au email (be sure to include your zID).

You can run an automated code style checker using the following command:
1511 style going_electric.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest going_electric

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab04_going_electric going_electric.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 11 October 20:00 to obtain the marks for this lab exercise.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 5 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec