Question

In: Computer Science

Write a program in c++ to do the following: 2. Create an array to hold up...

Write a program in c++ to do the following:

2. Create an array to hold up to 20 integers.

3. Create a data file or download attached text file (twenty_numbers.txt) that
contains UP TO 20 integers.

4. Request the input and output file names from the user. Open the files
being sure to check the file state.

5. Request from the user HOW MANY numbers to read from the data file,
up to twenty. Request the number until the user enters 20 or less, but not less
than 0. The user enters the number of integers to read. The integers are stored
in the file and are to be read from the file.

6. Write a function that reads from the opened data file: the number
of integers the user wants to read, and store the numbers in
the array. For example, if the user wants to read 13 numbers,
read 13 of the 20 that may be in the file.

7. Write a function that writes to the opened output file AND THE
CONSOLE, the numbers stored in the array.

8. NO GLOBAL variables are to be used. The input and output file variables
must be passed into the functions as parameters, the number of integers
to read and output must be passed in to each function, the array must be passed
in to each function.

twenty_numbers.txt

12 15 68 19 49 28 40 46 39 40 1 18 22 22 50 99 100 2 30 19

Solutions

Expert Solution

C++ code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

//function to print values
void print_numbers(int arr[],string str2,int x)
{
    // open a file in write mode.
    ofstream outfile;
    outfile.open(str2.c_str());

    for(int i=0;i<x;i++)
    {
        //console print
        cout<<arr[i]<<" ";
        //file print
        outfile<<arr[i]<<" ";
    }

}

//function to find numbers from file
void find_numbers(string str1,string str2,int x)
{
    // open a file in read mode.
    ifstream infile;
    infile.open(str1.c_str());
    //array to store elements
    int arr[x];
    int i=0;
    while(i<x)
    {
        //read from file and store in array
        infile>>arr[i];
        i++;
    }
    //function call to print arrays in file 2
    print_numbers(arr,str2,x);

}

//main function
int main()
{
    //declaring string variables to store file names
    char str1[100],str2[100];
    //input strings
    cout<<"Enter input and output file names";
    cin>>str1>>str2;

    //variable to store number of elements to read
    int x;
    cout<<"Enter how many numbers to read from data file: ";
    cin>>x;

    //function call
    find_numbers(str1,str2,x);

    return 0;
}

Sample run:

input file:

output file:


Related Solutions

Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
Java program In the main(), create and load a 2-dimentional array to hold the following sales...
Java program In the main(), create and load a 2-dimentional array to hold the following sales data Number of Tacos Sold (hard code the data into the array when you create it) Truck3 Truck4 .    Truck5 Monday 250 334 229 Wednesday   390 145 298 Friday .    434 285 . 156 • Write a method to calculate the grand total of all Tacos sold Write a method to allow the user to enter a truck number and get the total...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT