COMP1911 23T2 Introduction to Programming

Objectives

In this lab, you will practise:

Preparation

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

You should also have read the lab assessment guide.

Getting Started

Login and run following commands inside a Unix terminal

Create a new directory for this lab called lab03 by typing:

mkdir lab03
Change to this directory by typing:
cd lab03

Sum Integers

Write a program called sumIntegers.c that prints the sum of the integers from 1 to k, for all values of k between 1 and 20.

Your program should print exactly this output:

./sumIntegers
 k  sum

 1    1
 2    3
 3    6
 4   10
 5   15
 6   21
 7   28
 8   36
 9   45
10   55
11   66
12   78
13   91
14  105
15  120
16  136
17  153
18  171
19  190
20  210
Make sure your program matches the above ouput PERFECTLY.

Hint: the printf format "%4d" will print an int right-justified in a field of width 4.

As usual autotest is available to help you test your program.

1911 autotest lab03 sumIntegers.c

Sum Cubes

Write a program called sumCubes.c that prints the sum of the cubes of the integers from 1 to k, for all values of k between 1 and 20.

Your program should print exactly this output:

./sumCubes
 k  cubes

 1      1
 2      9
 3     36
 4    100
 5    225
 6    441
 7    784
 8   1296
 9   2025
10   3025
11   4356
12   6084
13   8281
14  11025
15  14400
16  18496
17  23409
18  29241
19  36100
20  44100
Hint: use sumIntegers.c as a starting point (don't over-write it!).

You'll need to change the field-widths in print formats to match the above output PERFECTLY

Autotest is available to help you test your program.

1911 autotest lab03 sumCubes.c

Circle Facts

Here is a program circleFacts.c which calculates some fun facts about circles.

Unfortunately it is incomplete. Your task is to complete it.

You can copy our template to your current directory by typing

cp ~cs1911/public_html/23T2/tlb/03/circleFacts.c .

Its main function is complete. Do not main function change. The three functions to change are:

double area(double radius);
double circumference(double radius);
double diameter(double radius);

Hint use the constant M_PI defined in math.h

dcc -o circleFacts  circleFacts.c 
./circleFacts 
Enter circle radius: 1
Area          = 3.141593
Circumference = 6.283185
Diameter      = 2.000000
./circleFacts
Enter circle radius: 17
Area          = 907.920277
Circumference = 106.814150
Diameter      = 34.000000
./circleFacts
Enter circle radius: 0.0125
Area          = 0.000491
Circumference = 0.078540
Diameter      = 0.025000

When you think you have circleFacts.c working use autotest to test it further:
1911 autotest lab03 circleFacts

Plus

Write a program called plus.c that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of an "+". You can assume n is odd and >= 5.

Make your program match the examples below exactly.

You are not permitted to use an array in this exercise.


Enter size: 7
---*---
---*---
---*---
*******
---*---
---*---
---*---

Enter size: 13
------*------
------*------
------*------
------*------
------*------
------*------
*************
------*------
------*------
------*------
------*------
------*------
------*------

As usual autotest is available to test your program.

 1911 autotest lab03 plus

Asterisk

Write a program called asterisk.c that reads an integer n from standard input. and prints an nxn pattern of asterisks and dashes in the shape of an "asterisk". You can assume n is odd and >= 5.

Make your program match the examples below exactly.

You may want to use the code from the previous exercise as a starting point. You are not permitted to use an array in this exercise.

Hint: This is basically a cross and a plus sign combined.


Enter size: 7
*--*--*
-*-*-*-
--***--
*******
--***--
-*-*-*-
*--*--*

Enter size: 13
*-----*-----*
-*----*----*-
--*---*---*--
---*--*--*---
----*-*-*----
-----***-----
*************
-----***-----
----*-*-*----
---*--*--*---
--*---*---*--
-*----*----*-
*-----*-----*

As usual autotest is available to test your program.

 1911 autotest lab03 asterisk

(Optional) Challenge Exercise: Prime Factors

Write a program called primeFactors.c that reads an integer n from standard input and prints the decomposition of n into prime factors. If n is prime it instead should print a message indicating this. Make your program match the examples below exactly.
./primeFactors
Enter number: 2048
The prime factorization of 2048 is:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 2048
./primeFactors
Enter number: 22500
The prime factorization of 22500 is:
2 * 2 * 3 * 3 * 5 * 5 * 5 * 5 = 22500
./primeFactors
Enter number: 22501
22501 is prime
Autotest is available to help you test your program.
1911 autotest lab03 primeFactors.c

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command in your lab03 directory):
give cs1911 lab03 sumIntegers.c sumCubes.c circleFacts.c plus.c asterisk.c primeFactors.c
Submit advanced exercises only if you attempt the advanced exercise.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by 19:59:59 Sunday using give and ask ask tutor to assess them at the start of the following lab.

You can also just run the autotests without submitting by typing

1911 autotest lab03