Week 05 Laboratory Exercises

Objectives

  • manipulating 1D and 2D arrays

Activities To Be Completed

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

Worth 0.7 mark(s) in total:

  • print_pi_style
  • scanning_into_array_style
  • points

Worth 0.7 mark(s) in total:

  • savings_analysis
  • array_clamping_max

Worth 0.4 mark(s) in total:

  • detective

Problem sets are capped at 15 marks (there are 4 possible bonus marks from the three-dot exercises that can bring you up to a total of 15 if you missed out on any other marks in the one- or two-dot exercises).

Completing just the one and two-dot exercises every week can give you the full 15 marks needed in this component.

For more details, see the course outline.

Exercise
(●◌◌)
:

Print out pi to a certain number of digits

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1091 style).

If 1091 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

    1091 style 
    

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/print_pi_style/print_pi_style.c .

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

The main function has already been written for you. There's an empty function that has been provided for you called print_pi_style that takes in one integer as an argument.

Place your code (only the printing code) inside this function.

You can NOT put all of your code in main for this exercise, main must remain unmodified.

The output from your program should look exactly like this:

dcc print_pi_style.c -o print_pi_style
./print_pi_style
How many digits of pi would you like to print? 3
3.14
./print_pi_style
How many digits of pi would you like to print? 5
3.1415
./print_pi_style
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.

New! You can run an automated code style checker using the following command:
1091 style print_pi_style.c
    

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

1091 autotest print_pi_style

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

give dp1091 lab05_print_pi_style print_pi_style.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise
(●◌◌)
:

Scanning numbers into an array

Good code style is required for this exercise.

To get full marks in this exercise, you MUST pass the automatic style checker (1091 style).

If 1091 style finds any issues in your code, the maximum mark you can achieve for this exercise is capped at 75%.

To run the automatic style checker on your code, use the following command:

    1091 style 
    

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/scanning_into_array_style/scanning_into_array_style.c .

Write a C program scanning_into_array_style.c which scans in a certain amount of numbers from the user and fills an array with them.

First, it scans in the quantity of numbers from the user.

Then, it scans in that many numbers from the user.

Once the array is filled with the numbers the user input, call a pre-written function that prints the minimum and maximum values in that array

Note: you do NOT need to write the minmax function, it is in the provided code, you are just required to call it properly after filling in the array yourself. This output verifies you scanned in the array correctly.

./scanning_into_array_style
How many numbers: 5
Please enter numbers: 1
2
3
4
5
Minimum: 1
Maximum: 5
./scanning_into_array_style
How many numbers: 4
Please enter numbers: 3
-2
7
1
Minimum: -2
Maximum: 7

Assumptions/Restrictions/Hints

  • The given inputs will always be integers.
  • The given list of numbers will never be more than 100 numbers.
New! You can run an automated code style checker using the following command:
1091 style scanning_into_array_style.c
    

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

1091 autotest scanning_into_array_style

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

give dp1091 lab05_scanning_into_array_style scanning_into_array_style.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise
(●◌◌)
:

Using struct arrays to store points of a shape

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/points/points.c .
Complete the C program points.c, so that it scans in all the points of a shape into an array of structs. It should then print out the points that were given. First, it scans in the number of points that the shape has. Then, it scans in the coordinates of each point. Afterwards, it prints out the points in order. ### Examples
dcc points.c -o points
./points
How many points in the shape? 2
Enter points:
1.231 4.560
7.892 0.12

Shape Points:
 1: x = 1.231, y = 4.560
 2: x = 7.892, y = 0.120
./points
How many points in the shape? 4
Enter points:
6.340 2.781
5.675 2.453
4.893 0.123
0.232 3.349

Shape Points:
 1: x = 6.340, y = 2.781
 2: x = 5.675, y = 2.453
 3: x = 4.893, y = 0.123
 4: x = 0.232, y = 3.349

### Assumptions/Restrictions/Clarifications

When asked for the number of points, you will be given a non-zero positive integer.

All inputs will be the correct type and non-zero positive numbers.

The number of points will not exceed 10.

New! You can run an automated code style checker using the following command:
1091 style points.c
    

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

1091 autotest points

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

give dp1091 lab05_points points.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise
(●●◌)
:

Saving Analysis

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/savings_analysis/savings_analysis.c .

You have been provided with some starter code savings_analysis.c with functions for you to complete: calc_yearly_deposits, calc_yearly_spending, calc_ending_balance and calc_months_saving_goal_met. It is your job to implement the functions and complete the program.

The program reads in the user's starting balance and monthly saving goal for the year. This is followed by 12 months of deposits and 12 months of spending. The program then runs a series of functions to analyse the account over the year. It is your job to complete these functions.

Examples

dcc savings_analysis.c -o savings_analysis
./savings_analysis
Please enter starting balance: 1320 
Please enter monthly saving goal: 60      
Please enter monthly deposits: 208 234 182 182 208 156 156 182 208 208 234 260
Please enter monthly spending: 120 103 150 122 130 372 106 115 127 124 104 200

Yearly deposits = $2418
Yearly spending = $1773
Ending balance  = $1965
You met the monthly saving goal 9 times!
./savings_analysis
Please enter starting balance: 500
Please enter monthly saving goal: 220
Please enter monthly deposits: 800 850 900 870 820 880 890 830 910 850 860 840
Please enter monthly spending: 600 620 610 630 640 615 610 625 600 630 635 670

Yearly deposits = $10300
Yearly spending = $7485
Ending balance  = $3315
You met the monthly saving goal 8 times!

Assumptions/Restrictions/Clarifications

  • The function names in your solution should be the same as the provided starter code.
  • Numbers entered will always be positive integers.
  • The correct amount of input will always be provided. That is, the number of monthly deposits and monthly spending will always be 12.
  • You must implement the functions specified to pass the autotests and recieve marks for this activity.
New! You can run an automated code style checker using the following command:
1091 style savings_analysis.c
    

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

1091 autotest savings_analysis

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

give dp1091 lab05_savings_analysis savings_analysis.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise
(●●◌)
:

Clamp a 2D array

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

cp -n /import/reed/A/dp1091/public_html/24T3/activities/array_clamping_max/array_clamping_max.c .

Given a 2D array of integers and a maximium value, you must make sure all values within the 2D array are less than or equal to that maximium value. If a value is greater than the max value, you should change the value to be equal to the max value.

For example if the given array was as follows, and the max value was set to 10

Then the array should be changed to be

Your function will be called directly in marking, any changes in the main function will not be used. You may use additional functions if you find it helpful.

You can assume the array is always square and the size is always 5.

The array values given can be any valid integer.

You are not required to print the array, this is handled separately.

You are only required to implement the clamp_max function.

Examples

dcc array_clamping_max.c -o array_clamping_max
./array_clamping_max
Before:
  9   3   2   5   2 
  2  12   5   1  11 
  4   4   7   7   6 
 10   0   4  15   0 
  2   9   0   4   0 
After:
  9   3   2   5   2 
  2  10   5   1  10 
  4   4   7   7   6 
 10   0   4  10   0 
  2   9   0   4   0 

Your program must produce this output exactly

New! You can run an automated code style checker using the following command:
1091 style array_clamping_max.c
    

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

1091 autotest array_clamping_max

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

give dp1091 lab05_array_clamping_max array_clamping_max.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise
(●●●)
:

Detective

// Detective
// detective.c
//
// This program was written by YOUR-NAME-HERE (z5555555)
// on INSERT-DATE-HERE
//
// A program to decipher hidden messages in enemy codes.

#include <stdio.h>

#define MAX_SIZE 100

struct code {
    char letter;
    int number;
};

int main(void) {
    //////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE //////////////////
    struct code message[MAX_SIZE];

    // read in message
    printf("Please enter Encrypted Message: ");
    int i = 0;
    char letter;
    scanf("%c", &letter);
    while (letter != '\n') {
        message[i].letter = letter;
        scanf("%c", &letter);
        i++;
    }

    // read in numbers
    printf("Please enter Secret Numbers: ");
    int j = 0;
    int number;
    scanf("%d", &number);
    message[j].number = number;
    j++;
    while (j != i) {
        scanf("-%d", &number);
        message[j].number = number;
        j++;
    }
    //////////////// DO NOT CHANGE ANY OF THE CODE ABOVE HERE //////////////////

    ////////////////////////////////////////////////////////////////////////////
    ///////////////////// ONLY WRITE CODE BELOW HERE ///////////////////////////
    ////////////////////////////////////////////////////////////////////////////

    // TODO: Use the message array to reveal the secret message!

    ////////////////////////////////////////////////////////////////////////////
    ///////////////////// ONLY WRITE CODE ABOVE HERE ///////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    return 0;
}

Your mission (if you choose to accept it) is to complete the program detective.c to decipher encoded messages.

Over the past few months UNSW Security Society has found seemingly normal messages paired with random numbers. To help you solve this challenge we have created the program detective.c that reads in the message and numbers, storing them in a struct array.

We have been lucky enough to find one decoded message:

Encoded Message: muahahaha_we're_so_evil#_GNTY

Secret Numbers: 7-23-3-6-13-22-24-2-16-4-14-18-20-11-9-28-21-1-27-8-0-26-12-5-10-15-25-19-17

Deciphered Message: haha_we_are_TeasiNG_You

Our observations from this are:

  • Messages seem to end with a #.
  • There are always the same number of letters as there are numbers.
  • Letters maintain their case between their encoded and deciphered message.
  • Each number is unique counting from 0 to n - 1 (where n is the number of letters).

We wish you luck detective!

Examples

dcc detective.c -o detective
./detective
Please enter Encrypted Message: muahahaha_we're_so_evil#_GNTY
Please enter Secret Numbers: 7-23-3-6-13-22-24-2-16-4-14-18-20-11-9-28-21-1-27-8-0-26-12-5-10-15-25-19-17
Secret Message is: haha_we_are_TeasiNG_You
./detective
Please enter Encrypted Message: STUDENTS_L0VE_COMP1511_#new_students_aww_:)
Please enter Secret Numbers: 28-12-24-38-6-33-36-27-16-7-19-17-39-2-11-14-32-31-41-23-18-15-0-21-35-9-40-20-1-30-5-42-4-25-13-26-37-34-22-8-29-10-3
Secret Message is: sTEw_MeET_at_Unsw_tuNneLS_11:05

Assumptions/Restrictions/Clarifications

  • The maximum number of characters is 100.
  • The number of characters in the encrypted message and the amount of secret numbers are the same.
  • The case of each letter is preserved in translation.
  • Messages will end with a #.
New! You can run an automated code style checker using the following command:
1091 style detective.c
    

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

1091 autotest detective

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

give dp1091 lab05_detective detective.c
    

You must run give before Monday 30 September 09:00 to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Submission

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

You only need to do this if the exercise specifies a give command, otherwise - the exercise is not worth marks.

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 6 Monday 9:00am 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:

1091 classrun -sturec

Generative AI Permission Level

In completing this assessment, you are permitted to use standard editing and referencing functions in the software you use to complete your assessment. These functions are described below. You must not use any functions that generate or paraphrase passages of text or other media, whether based on your own work or not.

If your Convenor has concerns that your submission contains passages of AI-generated text or media, you may be asked to account for your work. If you are unable to satisfactorily demonstrate your understanding of your submission, you may be referred to UNSW Conduct & Integrity Office for investigation for academic misconduct and possible penalties.

DPST1091/CPTG1391 Specific Information

You are permitted to use the tools dcc-help to help you understand the error messages you may get when compiling the code you have written.

You are permitted to use autotest-help to help you understand why your code may not be passing the automated tests.

You are not permitted to submit code generated by automatic AI tools such as Github Copilot, ChatGPT, Google Bard in DPST1091/CPTG1391/COMP1511 for assignments. Submitting code generated by Github Copilot, ChatGPT, Google Bard and similar tools will be treated as plagiarism.

Our reasoning behind our decisions:

Systems such as Github Copilot and ChatGPT based on large language models or other generative artificial intelligence techniques, look likely to become heavily used by programmers. However, you need a good understanding of the language you are coding in and the systems involved before you can effectively use these tools. Using these tools to generate code for DPST1091/CPTG1391/COMP1511 instead of writing the code yourself will hinder your learning.