Download COMP-255 C++ Additional Exercises

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

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
COMP-113 Programming II
Instructor: Dr. A. Stassopoulou
EXERCISE SHEET 3
COMP-113 Programming II Exercises on Arrays
1) Copying an array: Write a program that reads a sequence of positive numbers from
the user (-1 to signal end of input) and copies it in another array. The program should
output the copied array.
2) Reverse: Write a program that reads a sequence of positive integers from the user (-1
to signal end of input), stores the numbers in an array and then creates a new array to
store the numbers in reverse order. The program should output the two arrays. For
example, assume the user entered the following integers at the prompt: 10 4 33 25 1. The output should be:
Example run (user response in bold):
Please enter a sequence of positive integers (-1 to finish):
10
7
33
25
-1
You have entered 4 integers.
Initial array: 10 7 33 25
Reversed array: 25 33 7 10
3) Ratings: Some students were asked to rate the quality of food in the cafeteria on a
scale 0 to 20 (0 for awful and 20 for excellent). Write a program which will
summarize their responses in the following form: Assume, as an example, that out of
the 13 students we asked, 3 students gave it a “0” rating , 5 students gave it a rating
“1” and 5 students gave it a “4” rating. Write a program that would give the
following output in case of the above scenario:
Rating: 0
Rating: 1
Rating: 2
Rating: 3
Rating: 4
………
Rating: 20
Frequency: ***
Frequency: *****
Frequency:
Frequency:
Frequency: *****
Frequency:
Note that absence of stars means 0 students gave that rating. Your program should
allow entering of ratings as integers form 0 to 20. The number of ratings to be
entered should be defined by the user. Input ratings should be validated so that they
do not fall outside the valid range.
4) Sieve of Eratosthenes: Write a program that finds and prints all the prime numbers
between 1 and 500. A prime integer is an integer greater than 1 that is divisible only
by 1 and itself (e.g. 2, 3, 5, 7, 11, 13 etc). The Sieve of Eratosthenes is a method of
finding prime numbers. It operates as follows:
a) Create an array with all elements initialized to 1 (true). Array elements with prime
subscripts will remain 1 and all other (non-prime) elements will be eventually set
to 0 (false).
b) Starting with array subscript 2, 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
subscript is a multiple of the subscript for the element with value 1. For example,
for array subscript 2, all elements beyond 2 in the array that are multiples of 2 will
be set to 0 (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3
in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc);
and so on.
When this process is complete, the array elements that are still set to 1 indicate that the
subscript is a prime number. These subscripts can then be printed out.
Output (all prime numbers between 1 and 500):
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433
439 443 449 457 461 463 467 479 487 491 499