Assignment Specifications

Extend file hw1.c to create a C program that reads integers from a file and determines the average of the inputs and as well as the two largest integers in the data set. The size of the data set is defined by the first value in the file. Here's an outline of the algorithm to be used:

  • The name of the file is given as a command line argument or (in the case of doing your submission) hw1_file. The code to open the file (but not close it) is already included in hw1.c. Do not modify this code.
  • Read n: the first line of the input file is the number n of integers in the file.
  • Create a dynamic array (using malloc) large enough to hold n integers. Note: different files require different sized arrays depending on the size of the data set.
  • Read the n integers from the file into the array.
  • Find the average of the n integers in the array.
  • Find the two largest integers in the array.
  • To stdout (i.e. the console), output all of the integers in the array and also output the average of the input and the largest integers.

Additional Code Requirements

Your program must include the following functions:

  • The main() function opens the input file, reads the first line, and calls the functions readNumbers(), Average() and LargeNumbers(). Then it prints the output of the functions, as described above.
  • void readNumbers(...) which reads the n integers from the input file. It must have three parameters:
    • the array
    • the input file (already opened in main())
    • the number n of integers in the file (which was already read by main())
  • float Average(...) calculates the average of the values in the array. It must have two parameters:
    • the array
    • the number n of integers in the file (which was read by main()
    • and it must return value the average as a float value.
  • void LargeNumber(...) which computes the largest integers. It must have four parameters:
    • the array
    • number n of integers in the file (which was read by main())
    • the two largest integers

The last parameters are used to "return" the largest integers of the array. Recall that C passes function parameters by value - in order to return the two numbers that are required here, you will need to use addresses and pointers. An example of this is given in the Notes section of this document.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.