Lecture 11, Part 2: Repetition and
Problem 1: (This
is from Lab 6)
Write a program that plots a
square whose size is determined by user and is larger than 2.
Problem analysis:
Let
us consider square of size square_size = 4:
****
* *
* *
****
We
can conclude the following things:
-
First and last line are the same. They consist of square_size consecutive stars
-
The middle lines are the same. There are square_size-2
of such lines (so, a for
loop can be used here). Each line consists of a single star, followed by square_size-2 stars, and finished by a single star.
-
Writing function printNchars that prints n consecutive characters of desired type will be very
useful for improving readability of the program and reducing typing effort
One possible solution in
C:
/* Program Title:
*
Author, Date: Slobodan Vucetic 10/12/06
*
*
Requirements: Ask a user to enter the square size. If user enters size smaller
* than 3 display an error message.
*
Plot the square of the given size using * symbols.
*
*
Test Plan: Test by entering several choices of square size, including a
non-valid size
*/
/* Preprocessor directives */
#include<stdio.h>
/* Funtion prototypes */
int getSquareSize(void);
void printNchars(int n, char chartype);
void printSquare(int square_size);
/* Main function */
int main(void)
{
int square_size;
/* call getSquareSize
to obtain the square size
square_size = getSquareSize();
if square_size > 2
/* if the square size is valid, call printSquare to print a square */
printSquare(square_size);
else
/* if the square size is not valid, print
the error message */
printf(Invalid entry.
Program terminated. \n);
return(0);
}
/* getSquareSize: obtains the square size
from user */
int getSquareSize(void)
{
int square_size;
printf(Welcome to the
printf(Please enter the
square size (it should be an integer larger than 2)> );
scanf(%d,&square_size);
return(square_size);
}
/* printNchars: prints n consecutive characters of a given
type */
void printNchars(int n, char chartype)
{
int i;
/* the following loop prints n chars of
type chartype */
for (i=1;i<=n; i++)
printf(%c,chartype);
}
/* printSquare:
prints a squre of size square_size
*/
void printSquare(int square_size)
{
int i;
/* print first line */
printNchars(square_size,*)
printf(\n); /*
alternatively: printNchars(1,\n); */
/* print the following square_size-2
lines */
for (i = 1; i <= square_size-2; i++)
{
printNchars(1,*);
printNchars(square_size-2,
);
printNchars(1,*);
printNchars(1,\n);
}
/* print the last line */
printNchars(square_size,*)
printNchars(1,\n);
}
Problem 2: (This
is similar to Lab 7)
Write a function that plots a
filled triangle whose width is determined by user. Assume printNchars function is available. Also assume that provided
width is valid (it should be an odd positive number).
Problem analysis:
Let
us consider square of width w = 5:
*
***
*****
We
can conclude the following things:
-
Height of the triangle is function of the triangle width, h = (w+1)/2
-
The first line always consist of a single star (nstars = 1) and is preceded by several spaces
-
The number of leading spaces (nspace) in the first line is a function of triangle height, nspaces = h-1
- As
compared with the previous line, every following line has nspace-1 leading spaces, and is followed by nstars+2 stars
-
Therefore, the triangle can be plotted by using a single for loop
One possible solution in
C:
/* Function to display an isosceles triangle filled with stars*/
/* The width of the base of the triangle is
given as w */
void PrintTriangle
(int w)
{
int nspaces; /* keeps track of number of leading blanks per
line */
int nstars; /* keeps
track of number of stars needed per line */
int h; /* triangle height */
int i; /* to be used as
loop counter */
h = (w+1)/2; /* number of leading blanks needed on
top line */
for (i = 1; i <= h; i++)
{
/* display nspaces blanks on the same line */
printNchars (nspaces, );
/* display nstars stars on the same line (after
the blanks) */
printNchars (nstars, *);
/* display end of line on this same line */
printNchars(1,\n);
/* update values of nstars and nspaces */
nspaces = nspaces 1;
nstars = nstars + 2;
} /* end of for */
}
Advanced Topics
from Chapter 5
Compound Assignment Operator
variable op = expression;
is the same as
variable = variable op (expression)
where
op is +, -, *, /, %
Example:
i += 3;
is the
same as
i = i
+ 3;
Increment/Decrement Operators
Given integer or double
variable x:
x++;
is the same as
x =
x + 1;
++x;
is the same as
x =
x + 1;
x--;
is the same as
x =
x-1;
--x;
is the same as
x =
x-1;
The difference between x++ and ++x