Page 1 of 1

don't use void statements Do not use materials beyond chapter 5. Do not use functions and arrays. Create a new Code Bloc

Posted: Thu Jun 02, 2022 7:48 am
by answerhappygod
don't use void statements
Do not use materials beyond chapter 5.
Do not use functions and arrays.
Create a new Code Blocks project "Homework
9"
Download the “numbers-test.zip” file, save it, and unzip
it in the same folder as your CodeBlocks project
folder.
For example, if your Codeblocks project folder
is
C:\Users\XXXXX\Documents\cis22A\Homework 7
It contains the main.cpp file and all numbers file as
follows:
The first line in each file numbersXXX.txt contains a
string that can contain spaces. The rest of the file contains
a bunch of integer numbers separated by one or more spaces. You
should write the code to read the file data, then calculate and
display the average, the smallest, and the largest number. The code
also displays the count of numbers that are larger than the average
number and displays the count of numbers that are smaller than the
average number.
Notes:
1- Do NOT hard-code any absolute file location with
folder name for the input file inside your code like
this
“C:\John Smith\My
Documents\CIS22A\HW\numbers1.txt”
because your code will fail on any other PC different
than yours.
Put all the test input files in the same place as the
code that opens the file. This is the concept of "current working
directory" (CWD). When you are running Code Blocks with an existing
project, the current working directory is the Codeblocks project
folder, and CodeBlocks will only find the input file in this
current folder when your code opens a file given a simple file
name.
If you work on the Mac, you have to find out which
folder you should place the file into because the default CWD
folder may not be the same as the Code Blocks project
folder.
2- Do not hard-code the count of numbers in your code.
Assume that an input file can contain any count of numbers after
the first string. If your code is well written, it should work
without modification when the file contains either 1 number or
5000000 numbers. This is the same concept for Microsoft Word that
can process your document no matter how large your document
is.
3- Do not print the numbers to the console because there
can be millions of numbers in the file. Use setw() to format the
results.
4- Do not use an array or anything that was not
presented in class lecture so far because it is either not needed
or bad practice. Do not use "break" and "continue" in
loops.
5- Write code to ask the user to enter a file
name.
6- The input file has to be first opened, checked for
open error. Program should display an error if the file cannot be
opened.
7- The first line in the file should be read with
a single file read statement using getline (chapter 2), but
with the first parameter being the input file variable instead of
"cin". After that, use a "while" loop to read all numbers using the
>> operator, the same way that was shown in Chapter 5 slide
73.
8- While the file is processed with a "while" loop, each
number read is accumulated into a total sum, and the file is
closed. Then the program calculates the average.
9- Then the file is opened again, read the first line
like before, then a loop is used to count the numbers below and
above the average, and the file is closed again.
10- To get the lowest number, define an integer variable
and initialize it to the highest possible integer value using
INT_MAX as this constant is already defined in the climits include.
Then inside the loop that reads numbers, compare this number with a
newly read number and replace it if the number read is
lower.
To get the highest number, define an integer variable
and initialize it to the lowest possible integer value using
INT_MIN as this constant is already defined in the climits include.
Then inside the loop that reads numbers, compare this number with a
newly read number and replace it if the number read is
higher.
11- You can create more test files to test your code. In
the real world, programmers often create their own test data files
without waiting for someone else to give them test
data.
Sample console output:
Test 1
numbers1.txt file
Numbers File
1000 1 2 3
4
Test 2
numbers2.txt file
Numbers File
1 2 2
5
The expected output is
Test 3
numbers3.txt file
Numbers File
5 5 5 5
5
The expected output is
Test 4
The numbers-large.txt file contains large count of
numbers