Question

(a) Write a function in C++ called readNumbers() to read data into an array from a...

(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters:

(1) a reference to an ifstream object

(2) the number of rows in the file

(3) a pointer to an array of doubles

The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded.

(b) Write a program with the main() function is designed to use the readNumbers() function from the previous part of this question to read the data from a file called inputData.txt to the array.

Homework Answers

Answer #1

The following C++ program does all the required tasks:(Read the comments). The number of rows to be read from the file is passed to the function.

#include <iostream>
#include <fstream>

using namespace std;
//The required function
int read_numbers(ifstream &f, int r, double *p) {
    int i=0;
    //To read each double
    double x=0.0;
    while(i<r) {
        //if the file exhausts
        if(!(f >> x)) break;
        //for negative numbers 
        if(x<0) break;
        //read the number into array
        p[i] = x;
        //increment i
        i++;
    }
    return i;
}
int main()
{
    ifstream f1;
    f1.open("inputData.txt");
    //rows to be read from the file
    // this can be calculated as well from the file
    int rows = 6;
    double p[rows];
    int nums = read_numbers(f1, rows, p);
    cout << nums << endl;
    f1.close();
}

Create an input file -- inputData.txt (in the same folder as the above cpp program)

3.5
4.7
6
56
-1
76

Output:

4

As 4 lines are read successfully into the array. The 5th line contains a negative number and the loop breaks at this line.

Note(Optional):- The number of lines in the file can also be precalculated in main() if required:

double x=0.0;

int rows  = 0;

while(f1>>x) rows++;

//but then clear  the state and move f1 again to the start of the file

f1.clear();

f1.seekg(0);
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
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen
Write a function called read floats(int n) that reads n float values from the user into...
Write a function called read floats(int n) that reads n float values from the user into an array and return the array back to the caller. Recall that a regular array will get deallocated when the function returns. (C++ the simplier the better)
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code without testing it. I will downvote your answer and mark as spam.** I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!! In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT