Introduction to Programming
Assignment 3 – Arrays
April 7, 2021
Objectives
Practice and get familiar with functions and arrays in C language. In this assignment you will make use
of the subject matters in Chaps 4 and 5.
Problem Description
1. (50%) Write a program using one-dimensional arrays to find the union and intersection of sets.
Read in two sets of numbers, each having 10 random numbers. After reading all values, display
the following numbers. You must use the smallest possible array to solve the problems.
(a) (union) all the unique elements in the collection of both sets of numbers;
(b) (intersection) the unique elements common to both sets of numbers.
Example:
Input Set 1: 1 2 6 8 15 16 17 18 23 24
Input Set 2: 2 5 7 8 12 13 15 16 21 23
-> Union: 1 2 5 6 7 8 12 13 15 16 17 18 21 23 24
-> Intersection: 2 8 15 16 23
2. (50%) Write a program for the Sieve of Eratosthenes.
A prime integer is any integer greater than 1 that can be divided evenly only by itself and 1. The
Sieve of Eratosthenes is a method of finding prime numbers. It works as follows:
(a) Create an array with all elements initialized to 1 (true). Array elements with prime indices
will remain 1. All other array elements will eventually be set to zero.
(b) Starting with array index 2 (index 1 is not prime), every time an array element is found whose
value is 1, loop through the remainder of the array and set to zero every element whose index
is a multiple of the index for the element with value 1. For array index 2, all elements beyond
2 in the array that are multiples of 2 will be set to zero (indices 4, 6, 8, 10, and so on.). For
array index 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero
(indices 6, 9, 12, 15, and so on.).
When this process is complete, the array elements that are still set to 1 indicate that the index is
a prime number. Write a program that uses an array of 1000 elements to determine and print the
prime numbers between 1 and 999. Ignore element 0 of the array.
Example procedure:
Number: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Index: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Index: 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0
Index: 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0
Index: 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0
-> Prime numbers are 2, 3, 5, 7, 11, 13
1
Requirements
You MUST use
1. C style!
2. I/O functions
3. Flow of control
4. Functions
5. Arrays
Evaluation
• Correctness: 90%
• Styling: 10%
Submission
• 2021.04.18 (degrade by 10 points for each day delay)
• Source code (assign1.c, assign2.c)
– Show your information (Name, Student ID, Dept) as comments in the beginning of your code.
– Zip and upload the files to eeclass.