IC210 –Project #2 EI Problems

advertisement
IC210 –Project #2 EI Problems
Keep this worksheet! You instructor will ask you to bring a “best effort” solution to one of these problems (the most
relevant problem) to any EI for the project
NOTE: You are permitted to discuss solutions to these practice problems, with anyone as much as you like
(classmates, MGSP leaders, your instructor, or others). However, this does not change the policy regarding the
actual project – you should not discuss the project itself with anyone besides your instructor, nor provide any other
assistance to anyone. If you have any questions about the project itself, ask your instructor.
Problem A: (relates to project step 1) Assume you have a file “data.txt” that looks like this:
4
Cat
Tiger
Lion
Sheep
where the first line of the file specifies the number of entries, then there are a bunch of words.
Write a program that a.) asks the user for the name of a file (such as “data.txt”), b. opens that file, c.) reads all the
words into an array, then d.) prints out each word TWICE. So the output should look like:
Cat Cat Tiger Tiger Lion Lion Sheep Sheep
Assume the file has the format as given above, but do NOT assume that there are always 4 words – instead read this
information from the file. NOTE: you must write a function that creates the array and reads the data, and a separate
function that prints out the words. Call both of these functions from main().
Problem B: (relates to project step 1-2) Write a program that a.) asks the user for a number N, b.) creates a
2D array of size N by N, c.) fills in the array with the correct values to make a Hankel matrix, then d.)
prints out the numbers from the array (nicely spaced). See class 13 for more info on the Hankel matrix
(and maybe some helpful code – but you must use a 2D array rather than immediately printing out the
appropriate values). You must write (and use from main() ) at least two functions: one to create the 2D
array, another to print out the 2D array. Sample:
Enter
1 2
2 3
3 4
4 5
5 6
value
3 4
4 5
5 6
6 7
7 8
N, where n < 50: 5
5
6
7
8
9
Problem C: (relates to project step 4) Write a program that
a.) asks for the user for an integer N
b.) creates an array of doubles of size N
c.) sets every array value to 3.14
d.) asks the users for two integers X and Y
e.) changes every array element at indices [X..Y] (inclusive) to the value 1.5
f.) prints out the contents of the array
Sample:
Enter value N: 10
Enter X Y values: 3 5
Array is: 3.14 3.14 3.14 1.5 1.5 1.5 3.14 3.14 3.14 3.14
Problem D: (relates to project step 5)
1. Write a function mirrorArray() with this prototype:
void mirrorArray(int* data, int size);
that replaces the second half of the array with the “mirror image” of the first half of the array. So for
instance, if data pointed to an array with values 1, 2, 3, 4, 5, 6, 7, 8 (size is 8), then after
calling this function, the array would be changed to have the values 1, 2, 3, 4, 4, 3, 2, 1.
2. Write a new function:
void flipArray(int* data, int size);
that completely flips the array, so that for the same input as before, the new array (after the call) would be
8, 7, 6, 5, 4, 3, 2, 1
Project step 6? Try HW22.
Download