Question

C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...

C++

Goals:Practicing arrays

Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined.

Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and write a loop that will determine how many of the numbers in the array are evenly divisible by the entered value. This loop should not contain more than 3 arithmetic operators (++ and += count as arithmetic operators, but = does not). A message similar to

35 of the 189 values in the file were evenly divisible by 19.

with the underlined values replaced by your calculated values and the user’s input

Do not use any concepts beyond Chapter 7 of your textbook(e.g. pointers).Remember to write introductory comments for the entire program. If you decided to use functions,your code should contain comments for each function other than main that states the purpose of the function, input to the function (both input from the function call and interactive input), output from the function (both information sent back to the function call and interactive output), and processing performed in the function. Follow the Assignment Guidelines posted on Canvas.

Homework Answers

Answer #1

/* C++ program to read whole numbers from input file into an array and
determine the count of numbers that are evenly divisible by the number entered by the user */

#include <iostream>
#include <fstream>

// function declaration
int readFile(int numbers[]);

using namespace std;

int main()
{
int numbers[300]; // create an array of maximum size 300
int n, divisor, freq;

// read file data into array and return the count of numbers read
n = readFile(numbers);

// input a whole number between 2 and 20
cout<<"Enter a whole number between 2 and 20: ";
cin>>divisor;

freq = 0; // initialize freq to 0

// loop over the array counting the whole numbers that are evenly divisible by divisor
for(int i=0;i<n;i++)
{
if(numbers[i]%divisor == 0) // ith number is evenly divisible by divisor, increment freq by 1
freq++;
}

// display the number of whole numbers in array that are divisible by divisor
cout<<freq<<" of the "<<n<<" values in the file were evenly divisible by "<<divisor<<".\n";

return 0;
}

/*
* Function that reads whole numbers from input file into the array
* and return the count of numbers read from file.
*/
int readFile(int numbers[])
{
int n = 0;
ifstream fin;
// open the input file
fin.open("Labs4-7Mu.dat");

// loop till the end of file reading whole numbers into the array
while(!fin.eof())
{
fin>>numbers[n];
n++; // increment n
}

fin.close(); // close the file

return n;
}

//end of program

Output:

Input file:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum of two preceding: 0, 1, 1, 2, 3, 5, 8, 13, 21 ..... Your program should ask user for the integer, and output the result, which shows Fibonacci number at that position. For example if user enters '5', program should output '3' (fifth position in the series). Your program should use recursive function to compute and return back to the main the Fibonacci number....
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
Create a Python program that populates an array variable whose values(at least 5) are input by...
Create a Python program that populates an array variable whose values(at least 5) are input by the user. It should then perform some modification to each element of array using a loop and then display the modified array in a second loop. Include Header comments in your program that describe what the program does.
create a program that prompts the user for a file name and then provides the user...
create a program that prompts the user for a file name and then provides the user with the number of word in the file. The program should then allow the user to repeatedly specify a number and show that word in the file. (Note: Do Not use an array.) please write in bsic comments so i can more easily understand A test plan for the program. (How would you know the program is actually working properly?) A shell program with...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
You are to write a C program that will read from a file, one or more...
You are to write a C program that will read from a file, one or more sets of x,y coordinates. Each set of coordinates is part of a Cartesian system. A Cartesian coordinate system is a system that specifies each point uniquely in a plane by a pair of numerical coordinates. Your program will determine which quadrant each set belong. - Quadrants are often numbered 1st - 4th and denoted by Roman numerals: I(+,+), II (−,+), III (−,−), and IV...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT