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 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 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 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 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...
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...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
• First, create a function called addNumber, which has a formal parameter for an array of...
• First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a function that receives an array of floats of 5 elements. The function (called sqrt_float)...
Write a function that receives an array of floats of 5 elements. The function (called sqrt_float) should return an array of floats that contains the square root of each value in the first array. If the value of the square root is less than 2, then replace it with the value 0. Print the new values back in the main function after calling the function.
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT