Introduction to Computer Science

CSCI 151 Introduction to Computer Science
Assignment 3

The due date is Monday, November 9, 2009, 3:50 PM. The printout of the programs and the e-mail with the programs should be received by 3:50 pm on November 9, 2009

Do your own work. Programs which are written in groups are easily identified and will receive grades of 0 for all participants.

Skill set for this assignment:

  • Use if, if-else, if - else - if, while and for statements
  • Use an appropriate variable types and casting
  • Understand and implement the Program Development Cycle
Problem 1 (20 points):
Write a C program that reads two integer values as an input. The program should display whether the inputs are both positive, both negative or one positive and one negative.
If one or two numbers are zero the program should display the appropriate message as well. You should run your program a couple of times and show that your program works properly for all possible cases.

Examples:
1. If your input is 0 and 3 your program will display the following message:
One integer is zero and one integer is positive.

2. If your input is 0 and 0 your program will display the following message:
Two integers are zero

3. If your input is 5 and 6 you will display the following message:
Two integers are positive.

4.If your input is -3 and 0 you will display the following:
One integer is zero and one is negative

Problem 2 (20 points)
Write a C program that reads one integer. The program performs the following, if the integer is positive, the program prints the input number on one line the amount of times that equals to the input number (numbers should be separated by space), if the input number is negative, the program checks if the number is divisible by 5 or not and prints the appropriate message, and if the input number is 0, the program prints Hello 3 times on different lines. You have to use loop while or loop for in your program.

Example:

1. if input is 5, the output should be:
5 5 5 5 5

2. if the input is -7, the output should be: the number -7 is not divisible by 5

3. if the input is 0, the output should be:
Hello
Hello
Hello

Problem 3 (20 points)
Write a program that inputs a series of non-negative integers, the first negative value will terminate the input. The program determines and prints the amount of numbers that are divisible by 6 among input numbers and the average of all numbers that are divisible by 6 among input numbers. If there are no numbers divisible by 6 among the input number, the program prints the following message: there are no numbers divisible by 6. You have to use loop while or loop for in your program

Examples:
1. if the input is 6 7 9 -12 the output should be:
there is 1 number that is divisble by 6 and their average is 6.0

2. if the input is -12 the output should be:
there is no numbers that are divisible by 6 and the average of these numbers is 0.0

3. if the input is: 8 12 3 6 6 18 -3 the output should be:
there are 4 numbers divisible by 6 and the average is 10.5

Problem 4 (20 points)
Write a C program that solving a linear equation ax + b = 0. The program should accept as an input any two integers as the coefficients a and b of the equation. The output will be the solution of the linear equation.

Directions: if a is not 0 the linear equation has a unique solution x = -b/a, if a is 0 and b is not 0 the linear equation doesn't have solutions at all, and if a and b are zeros the linear equation has infinite number of solutions. Your program has to consider all cases that were described above.

Examples:
1. if the input is a = 4 and b = 10, the output should be the equation has a unique solution x = -2.5
2. if the input is a = 0 and b = 0, the output should be there are infinite amount of solutions
3. if the input is a = 0 and b = 6, the output should be there is no solution for the given equation

Problem 5 (20 points):
Write a C program that reads a sequence of integers. Assume that the first integer read with scanf specifies the number of values remaining to be entered. The program finds and prints the amount and the average of the even numbers among the inputs. The first integer should not be taken into account.
Example:
If the input is: 6 6 -8 9 0 1 3
The output should be: There are 3 even numbers and their average is 0.666667
Explanation to the output:since (6+(-8)+0)/2 is 2/3 which is 0.666667. Pay attention, that the first number 6 indicated that six more integers was entered. Also, pay attention, that the first integer WAS NOT TAKEN IN ACCOUNT for the calculation of the amount of even numbers and their average.

GOOD LUCK !

Bonus Problem (20 points):

The ancient Greeks were fascinated with numbers that are equal to the sum of their proper factors; they called such numbers perfect. For example, 6 is perfect because its proper factors (i.e. numbers that are less than 6 and divide 6 evenly) are 1, 2 and 3 with the sum being equal to 6. Another perfect number is 28, since 28 = 1+2+4+7+14 (verify manually that 28 only has these proper factors).

Write a C program that reads in a number N and prints out all those numbers between 1 and N that are perfect numbers. For example, if the prompted input was 30, the program would print out 6 and 28. Your program should be able to accept values of N up to 10000.

The program has to check a validity for the input.

A few remarks and helpful hints:

  • Note that 1 is always included in the sum of proper factors.
  • Use a for loop with index variable i, say, to iterate from 1 to N; within the loop, your program should determine if i is perfect. It will do so by using another loop (nested within the outer loop) in which you should systematically account for all potential proper factors of i.
  • Don't bother trying to do this by hand: there are only 4 perfect numbers between 1 and 10000. This rarity was no doubt part of the appeal for the Greeks!
  • If P divides i evenly, P is greater than 1 and P is less than i, then i/P is also a proper factor of i. For instance, consider i=192 and P=12. P divides i evenly since 12*16=192. But this gives us another factor, 192/12 = 16.