In this lab, you will practise:
You should also have read the lab assessment guide.
Create a new directory for this lab called lab03
by typing:
mkdir lab03Change to this directory by typing:
cd lab03
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 210Make 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
sumIntegers.c
// Print sums of integers // Written 26/3/2015 // by Andrew Taylor (andrewt@cse.unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> #define MAX_K 20 int main(int argc, char *argv[]){ int i; int sumSoFar; // Print the header printf(" k sum\n\n"); // We want k from 1 to 20, so we start // from 1 and go up to MAX_K inclusive // We also use sumSoFar to keep track of our // running total sumSoFar = 0; i = 1; while (i <= MAX_K) { sumSoFar = sumSoFar + i; printf("%2d %4d\n",i, sumSoFar); i = i + 1; } return 0; }
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 44100Hint: 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
sumCubes.c
// Print sums of cubes // Written 26/3/2015 // by Andrew Taylor (andrewt@cse.unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> #define MAX_K 20 int main(int argc, char *argv[]) { int i; int sumSoFar; // Print the header printf(" k cubes\n\n"); // We want k from 1 to 20, so we start // from 1 and go up to MAX_K inclusive // We also use sumSoFar to keep track of our // running total sumSoFar = 0; i = 1; while (i <= MAX_K) { // How could we make the program more flexible by allowing // it to be raised to an arbitary exponent? sumSoFar = sumSoFar + (i * i * i); printf("%2d %6d\n",i, sumSoFar); i = i + 1; } return 0; }
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.025000When you think you have
circleFacts.c
working use autotest to test it further:
1911 autotest lab03 circleFacts
circleFacts.c
// Alex Linker 2017-08-13 // This program prints out facts about a circle given its radius, // using functions. #include <stdio.h> #include <math.h> double area(double radius); double circumference(double radius); double diameter(double radius); // DO NOT CHANGE THIS MAIN FUNCTION int main (int argc, char *argv[]) { double radius; printf("Enter circle radius: "); scanf("%lf", &radius); printf("Area = %lf\n", area(radius)); printf("Circumference = %lf\n", circumference(radius)); printf("Diameter = %lf\n", diameter(radius)); return 0; } // Calculate the area of a circle, given its radius. double area(double radius) { return M_PI * radius * radius; } // Calculate the circumference of a circle, given its radius. double circumference(double radius) { return M_PI * 2 * radius; } // Calculate the diameter of a circle, given its radius. double diameter(double radius) { return 2 * radius; }
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
plus.c
// Written 14/3/2017 by Andrew Taylor (andrewt@unsw.edu.au) // as a lab example for COMP1511 // Modified by Angela Finlayson for COMP1911 // Print an nxn "plus" pattern of asterisks and dashes // // For example here is the output for n == 9 // // ----*---- // ----*---- // ----*---- // ----*---- // ********* // ----*---- // ----*---- // ----*---- // ----*---- #include <stdio.h> //We assume input is correct and n >=5 and is odd int main(void) { int size, n_numbers_read; int row, column; printf("Enter size: "); scanf("%d", &size); row = 0; while (row < size) { column = 0; while (column < size) { if (row == size/2 || column == size/2) { printf("*"); } else { printf("-"); } column = column + 1; } printf("\n"); row = row + 1; } return 0; }
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
asterisk.c
// Written by Angela Finlayson (angf@cse.unsw.edu.au) // as a lab example for COMP1911 // Print an nxn "asterisk" pattern of asterisks and dashes // // For example here is the output for n == 9 // // *---*---* // -*--*--*- // --*-*-*-- // ---***--- // ********* // ---***--- // --*-*-*-- // -*--*--*- // *---*---* #include <stdio.h> int main(void) { int size, n_numbers_read; int row, column; printf("Enter size: "); scanf("%d", &size); row = 0; while (row < size) { column = 0; while (column < size) { if (row == size/2 || column == size/2 || row == column || (row + column) == (size-1)) { printf("*"); } else { printf("-"); } column = column + 1; } printf("\n"); row = row + 1; } return 0; }
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 primeAutotest is available to help you test your program.
1911 autotest lab03 primeFactors.c
primeFactors.c
// Find prime factors of a number // Written 26/3/2015 // by Andrew Taylor (andrewt@cse.unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> int main(int argc, char *argv[]) { int number, n, nFactors; int i; printf("Enter number: "); scanf("%d", &number); // To find all prime factors of a number, we simply go through all // numbers from 2 to the original number and see if the // original numbers is divisible by any of them. // We also need to take into account that there may be // multiple prime factors of the // same value, e.g. 12 = 2*2*3, so we have to keep dividing a number // by a factor until it is no longer divisible. i = 2; nFactors = 0; n = number; while (i < number) { if (n % i == 0) { if (nFactors == 0) { printf("The prime factorization of %d is:\n", number); } else { printf(" * "); } printf("%d", i); n = n / i; nFactors = nFactors + 1; } else { i = i + 1; } } if (nFactors == 0) { printf("%d is prime\n", number); } else { printf(" = %d\n", number); } return 0; }
lab03
directory):
give cs1911 lab03 sumIntegers.c sumCubes.c circleFacts.c plus.c asterisk.c primeFactors.cSubmit 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