Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. Homework – Topic 7 cont. – Count Zeros:
  2. Write a complete program to do the following:
  3.  
  4. (a) Write a method called readData() which receives two parameters, an array of integers
  5. called vals, which is initially uninitialized, and a reference to an output file. The method
  6. reads integers from an input file until it reaches EOF, storing the data values in the array
  7. vals. The method counts how many integers were read into the array. The method prints
  8. the data values read in and the count to an output file. The method returns the filled-in
  9. array and the total number of data values read in.
  10. (b) Write a method called countZeros() which receives two parameters, an integer n and
  11. an array vals. The method counts and returns how many of the first n elements of the
  12. vals array are 0.
  13. For example, if the array holds 66 0 -4 0 4 31 with n = 6, it has two 0 values.
  14. (c) Write a method called append() which reads in several new values into an array,
  15. appending them at the end of the array. As a result, it must change both the array and the
  16. value of n. The method receives the same two parameters as countZeros(). The method
  17. returns the new total number of data values stored within the array.
  18. For example, Assume the array initially holds 66 0 -4 0 4 31 with n = 6; after the
  19. method call, the array might hold 66 0 -4 0 4 31 22 0 49 with n = 9. (Make sure
  20. that several new 0 values are added to the array.)
  21. (d) Write a main program which calls these methods. First, the main program calls
  22. readData() to read a set of data into an array called numbers, which contains no more
  23. than 100 integers. The number of elements actually read is returned by the method and
  24. stored in a variable called size. Then, the main program calls the method countZeros() to
  25. find out how many of the size array elements are 0. The main program prints this value to
  26. the output file. Next, the main program calls append() to modify the numbers array and
  27. n. The append() method reads in new values from a file until input failure, adding the
  28. new values to the array and incrementing n. The new values in the array and new total
  29. count are printed (in either the main program or the method) to the output file. Finally,
  30. the main program calls the method countZeros() again to determine how many elements
  31. in the new array are 0 and prints the result to the output file.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement