COS 126

Geometric Patterns
Programming Assignment 1

Due: Wednesday, 11:59pm

Your goal is to write C programs using while loops, for loops, and if-else selection statements.

Part 0. Create a new directory to write your programs, and copy the files you will need into this directory. Use the following command.

yuma.Princeton.EDU% mkdir pattern
yuma.Princeton.EDU% cd pattern
yuma.Princeton.EDU% cp ~cs126/files/pattern/* .

Part 1 (while loop). Write a program line.c that reads in an integer n, and prints out n asterisks. Use a while loop. Your program should work as follows.

yuma.Princeton.EDU% gcc line.c
yuma.Princeton.EDU% a.out
Enter the width:
10
**********

yuma.Princeton.EDU% a.out
Enter the width:
17
*****************

Part 2 (nested while loops). Write a program rectangle.c that reads in two integers m and n, and prints out an m-by-n rectangle of asterisks. Use two while loops.

yuma.Princeton.EDU% gcc rectangle.c
yuma.Princeton.EDU% a.out
Enter the height followed by the width:
3 10
**********
**********
**********

Part 3 (nested for loops). Repeat Parts 1 and 2, but this time use only for loops instead of while loops. Name your programs line2.c and rectangle2.c.

Part 4 (if-else). Write a program triangle.c that reads in an integer n, and prints out the n-by-n pattern below. Use two for loops and one if-else statement.

yuma.Princeton.EDU% gcc rectangle.c
yuma.Princeton.EDU% a.out
Enter the size:
6     
******
.*****
..****
...***
....**
.....*
yuma.Princeton.EDU% a.out
Enter the size:
7   
*******
.******
..*****
...****
....***
.....**
......*

Part 5a (debugging). If you completed Part 0, the file debugme.c should be in your current directory. The program is supposed to read in an integer n and then print all integers between 1 and n, blurting out "7 IS MY FAVORITE NUMBER" before printing 7. Compile with the command

gcc debugme.c
It should compile without errors. However, when you run the program, you will see that there are several flaws. Identify and correct them. Explain in your readme file how you discovered each bug, and how hard it was to find. To test your program, be sure to try one input value less than 7 and one greater than 7.
#include <stdio.h>
int main(void) {
   int n, i;
   scanf("%d", n);
   for (i = 1; i <= n; i++);
      if (i = 7) 
         printf("7 IS MY FAVORITE NUMBER\n");
      printf("%d\n", i);
   return 0;
}

Part 5b (debugging). After you're finished with Part 5a, redo it, but this time with the compilation command

gcc126 debugme.c
This command turns the compiler warnings way up. It will only work if you ran the setup126-f00 command in Assignment 0.

What to submit. Submit a readme file along with the programs from Parts 1-4 and the debugged program from Part 5. Be sure that your code is nicely indented. Use the given filenames, and submit with the command:

submit126 1 readme line.c rectangle.c line2.c rectangle2.c triangle.c debug.c

This assignment was written by Kevin Wayne.
Copyright © 2000 Robert Sedgewick