Download ECE 175: Computer Programming for Engineering Applications

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Factorization of polynomials over finite fields wikipedia , lookup

Algorithm wikipedia , lookup

Computer file wikipedia , lookup

Sieve of Eratosthenes wikipedia , lookup

Transcript
ECE 175: Computer Programming for Engineering Applications
Homework Assignment 4
Due Date: October 1st, 2013 11:59 PM
Conventions: Name your C programs as hwx py.c where x corresponds to the homework number and y corresponds to the problem number. As an example the C program for hw1 problem 1 should be named as hw1 p1.c.
Submission Instructions: Use the dropbox on D2L to submit only the .c files.
Problem 1: Chapter 5, All sections
Problem 2: Write a program that creates histograms. Your program should read the histogram values from a
file named “hist.txt” and printout the histogram in a horizontal form. The format of the histogram file is as follows.
1
2
3
4
5
6
10
8
5
7
9
12
Your program should print in a file named “graph.txt” the histogram as follows:
1
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
2
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
3
∗ ∗ ∗ ∗ ∗
4
∗ ∗ ∗ ∗ ∗ ∗ ∗
5
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
6
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
Problem 3: Write a program that finds all prime numbers contained in file ”numbers.txt” and places them in file
“primes.txt”. Your program should call a user-defined function called “primes” that returns 1 if a number is prime
and zero otherwise. The function prototype should be as follows:
int prime(int p);
A prime number (or a prime) is a natural number that has exactly two distinct divisors: 1 and itself. For primality
testing use the following algorithm.
1
√
Algorithm: Let n be the number tested for primality. If n is not divisible by any number m smaller than d ne
then n is a prime. Else n is not a prime.
Example: 13 is a prime because no number less than 4 divides it.
The rationale behind this simple algorithm is the fact if a number n can be factored√(analyzed to factors other
than 1 and itself), at least one of the factors has to be less√or equal to√
its square root n. This is easily shown via
contradiction. If n could be
analyzed
to
two
factors
a
>
n
and
b
>
n, then ab > n. Hence, at least one factor
√
has to be less or equal to n.
Prime numbers used for cryptographic applications are typically very large (in the order of thousand bits long).
Submit your .c files named hw4 p2.c, hw4 p3.c via D2L dropbox
2