Question

In: Computer Science

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.

Solutions

Expert Solution

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

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int numbers[300]; // create an array of size 300

// n stores the number of whole numbers read from file
int n = 0, key, count;

ifstream fin;
// open the input file
fin.open("Labs4-7Mu.dat"); // provide full path to file

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

fin.close(); // close the file

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

count = 0; // initialize count to 0

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

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

return 0;
}

//end of program

Output:

Input file:

Output:


Related Solutions

C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int...
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int one double. They should have 10 elements each Ask the user how many random numbers to generate. Then generate that many random numbers between 1 and 10. Use a for loop. Keep track of how many of each number in the range was generated (1 to 10) in the int array. Be sure to initialize the array to all 0's to start. Then, send...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user. ** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. ** Input: After all numbers are read, what number would you like to multiply the...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT