**Assigned Date**: Friday, Feb. 11, 2011

**Due Date**: Monday, Feb. 21, 2011

**Due Time**: 11:50am

Last modified on February 21, 2011, at 06:57 AM (see updates)

This is a **solo** assignment. You **must** work alone.

## Purpose

This assignment focuses on familiarizing students with C, and in particular:

- structured program development
- various operators (e.g., arithmetic, relational, logical, etc.)
- control structures (e.g., if, switch, for, while, etc.)
- functions (e.g., prototypes, scope rules, storage classes, etc.)

## Assignment

**maxMinInt.c** - Write a program that reads in five integers and then determines and prints the largest and the smallest.

Use the following format precisely (user input in *italics*):

Enter five integers: *3 4 1003 -3 3*

The smallest is -3 and the largest is 1003.

**pythagorean.c** - Write a program that reads in three nonzero integers and determines if they correspond to sides of a right triangle. It keeps looping until the user enters three zero integers.

Use the following format precisely (user input in *italics*):

Enter three integers: *3 4 5*

Integers 3, 4, and 5 are Pythagorean triples.

Enter three integers: *1 2 3*

Integers 1, 2, and 3 are not Pythagorean triples.

Enter three integers: *0 0 0*

**evenSum.c** - Write a program that calculates and prints the sum of all even integers from 2 to X (inclusive), where 'X' is an integer value provided as input. It should keep looping until the user enters 0.

Use the following format precisely (user input in *italics*):

Enter your max number: *6*

The sum of all even numbers from 2 to 6 is 12

Enter your max number: *2*

The sum of all even numbers from 2 to 2 is 2

Enter your max number: *0*

**diamond.c** - Write a program that outputs the following shape. You may use `printf`

statements that print either a single asterisk or a blank. Maximize use of nested `for`

loops and minimize use of `printf`

statements. Define a constant, `MAX_WIDTH`

, to make your code easy to maintain - i.e., for the following output, use `#define MAX_WIDTH 9`

.

*
***
*****
*******
*********
*******
*****
***
*

**hypotenuse.c** - Define a function, called `hypotenuse()`

, which calculates the length of the hypotenuse of a right triangle when the other two sides are given as arguments to the function. Then, use this function in a program that accepts 2 integers as input and outputs the length of the hypotenuse. It should keep looping until the user enters two zero integers.

Use the following format precisely (user input in *italics*):

Enter two triangle sides: *3 4*

The hypotenuse of the right triangle with sides 3 and 4 is: 5

Enter two triangle sides: *0 0*

**prime.c** - An integer is said to be prime if it's divisible only by 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write a function, called `isPrime()`

, which takes a single integer as argument and determines if it is a prime number (should return either 0 or 1). Then, use this function in a program that takes a single integer as input and outputs if the number is prime. It should keep looping until the user enters a zero integer.

Use the following format precisely (user input in *italics*):

Enter a single integer: *6*

The number 6 is not prime.

Enter a single integer: *11*

The number 11 is prime.

Enter a single integer: *0*

**reverse.c** - Write a function that takes a single integer as argument and returns the number with all its digits reversed. Then, use this function in a program that takes a single integer as input and outputs the reverse of the value given. It should keep looping until the user enters zero.

Use the following format precisely (user input in *italics*):

Enter a number: *1234*

The reverse of 1234 is 4321

Enter a number: *8374*

The reverse of 8374 is 4738

Enter a number: *0*

## Details

Your programs should perform reasonably thorough error checking.

## Documentation

Follow the Golden Rule of Style: "A program should be as easy for a human being to read and understand as it is for a computer to execute." [1]

In general, you should comment any variable, obscure statement, block of code, etc. you create.

Also, you should comment **why** something is being done, e.g.,

numStudents += 1; /* we have processed one more student */

as opposed to **how** it is done, e.g.,

numStudents += 1; /* increment numStudents by one */

Finally, your code should always include opening comments as follows.

(*NOTE:* Angle brackets signify information that needs to be filled out. **Remove** the angle brackets!)

/*

Author: <Your Name(s)>

Email: <Your email address(es)>

Class: CSCI 340, Section 1

Assignment: HMWK2

Due Date: <The assignment's due date>

Certification of Authenticity <remove one of the following>:

I certify that this lab is entirely my own work.

I certify that this lab is my own work, but I received

some assistance from: <Name(s)>

TASK: <Provide a simple, yet complete description of the task being

performed by this program. It may be several sentences long.>

INPUT: <Describe the input to this program. Be thorough.>

OUTPUT: <Describe the output to this program. Be thorough.>

*/

## Submissions

You will submit your assignment via the stono `submit`

command, as follows:

% submit csci340 hmwk2 maxMinInt.c pythagorean.c evenSum.c diamond.c hypotenuse.c prime.c reverse.c

(*NOTE:* Make sure you submit **all** your files with a single submit command. Otherwise, the latest submitted files will overwrite any previous ones - even if they have different names!)

No other submission mechanism will be accepted (e.g., email).

For more information on the submit command, see the submit man page.

## Grading

Your assignment will be graded based on the documentation, formatting, and correctness of your source code. Also the completeness / thoroughness of your work, and how well you followed the homework instructions.

## Reference

- Cooper, D. and Clancy, M. (1985) "Oh! Pascal", 2nd ed., W.W. Norton & Company, New York, p. 42.

(This assignment was adapted from Deitel & Deitel, "C - How to Program".)