Question

In: Computer Science

Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...

Please solve questions in C++ ASAP!! thank you

(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 integers

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.

Solutions

Expert Solution

FUNCTION readNumbers() and C++ CODE with ifstream() as refrence :

//C++ CODE reads data from a text file into an array.

#include <bits/stdc++.h>
#include <fstream>
using namespace std;

//To store elements 
vector<int> numbers;


//Function to read Array from file
void readNumbers(ifstream& inputFile){

    // Check if exists and then open the file.
    if (inputFile.good()) {
        
        // Push items into a vector
        int current_number = 0;
        //Checking if number in file is positive and number exist
        while (inputFile >> current_number and current_number>=0){
            numbers.push_back(current_number);
        }

        // Close the file.
        inputFile.close();

        // Display the count of numbers read.
        cout << "The count of numbers are: ";
        cout<<numbers.size();
        cout << endl;
    }else {
        cout << "Error!"; //If error occurs or file is missing.
    }
}

int main()
{
        ifstream inputFile("inputData.txt");
    readNumbers(inputFile);
    return 0;
}

INPUT FILE :

OUTPUT :


Related Solutions

(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Can you please do this in C asap. Promise to leave a awesome rating! Thank you...
Can you please do this in C asap. Promise to leave a awesome rating! Thank you in advance. Question and template below Instructions- LINKED LIST In C 'objects' => structs (struct - collection of data, not methods) Linked List Collection of objects which are all connected to each other Each Object 'points' to the next item in the list Node (struct) ' O ' - data - nextNode Our linked list will store data(structs containing integers) in order from smallest...
*Please answer questions to case study ASAP. Thank you so much! When you think of the...
*Please answer questions to case study ASAP. Thank you so much! When you think of the most sustainable corporations in the world, Bell Canada may not jump to mind. Nonetheless, Corporate Knights listed Bell as one of only eight Canadian companies to make the Global 100 list of sustainable compa- nies for 2011. Bell Canada Enterprises (BCE) was also named byMaclean’s/Jantzi-Sustainalytics as one of Canada’s Top 50 Socially Responsible Corporations. It also made the prestigious FTSE4Good Global Index. It is...
Can you Please Solve this ASAP 4. Answer the following questions about photoinhibition and the xanthophyll...
Can you Please Solve this ASAP 4. Answer the following questions about photoinhibition and the xanthophyll cycle: a) What is photoinhibition? How are reactive oxygen species (ROS) involved in photoinhibition? b) How can photoinhibition “protect” the photosynthetic apparatus from further damage? c) How does the xanthophyll cycle operate? How do xanthophylls contribute to the defense against excess light?
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use...
Please solve A, B, and C. Please use excel. Please show work. Thank you. A. Use the stocks of Apple, SAP, IBM, Oracle, and Amazon Download the historical data of weekly stock prices and S&P 500 index prices from year 2017-2019 on the website of yahoo finance and save it on an excel file. B. Use a different sheet to save the market adjusted prices of Apple, SAP, IBM, Oracle, and Amazon t and the index. For each stock, compute...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
THANK YOU FOR ANSWERING THE 4 QUESTIONS Q3. Create a function in Python called sum_list_div2 which...
THANK YOU FOR ANSWERING THE 4 QUESTIONS Q3. Create a function in Python called sum_list_div2 which takes as input a list of integers and returns the sum of the elements of the list which divide by 2. Examples of tests in the Python interpreter: >>> sum_list_div2 ([1,4,3,8,5]) 12 >>> sum_list_div2 ([]) 0 >>> sum_list_div2 ([4, -10,7]) -6 -------------------------------------------------------------------------------------------------------------------------------------------------------------- Q5. Imagine a customer at a bank who must enter a number that represents their money. There are people who type spaces...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT