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
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...
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...
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...
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.
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
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...
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
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
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...
Movie Structure File Write a program that will read in a CSV file with information regarding...
Movie Structure File Write a program that will read in a CSV file with information regarding movies. The file will contain a movie name, MPAA coding (G, PG, PG-13, R), number of minutes, and ratings (1-10) from the three top critics. Your program will read in the data into an array of structures (No more than 20 movies) and send the array to a function that will produce a professional report file (MovieReport.txt) which includes all of the information, an...