Question

C++ CODE NEEDED WITH (//) COMMENTS You are given a file called input.txt with the following...

  1. C++ CODE NEEDED WITH (//) COMMENTS
  2. You are given a file called input.txt with the following data

input.txt    

45

92

34

52

34

Write code that will copy the contents of the file into an array. You can assume that the file will only have 5 data values

0 0
Add a comment Improve this question
Answer #1
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream in("input.txt");   // open file
    if (in.is_open()) { // check if file is open
        int arr[5]; // declare a 5 element array
        for (int i = 0; i < 5; ++i) {   // loop 5 times
            cin >> arr[i];  // read 5 numbers into array
        }
        // print the array
        for (int i = 0; i < 5; ++i) {
            cout << arr[i] << endl;
        }
        in.close();
    } else {
        cout << "input.txt does not exists!" << endl;
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ CODE NEEDED WITH (//) COMMENTS You are given a file called input.txt with the following...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do in java as simply as possible with code available for copy and comments. Provide...

    Please do in java as simply as possible with code available for copy and comments. Provide screenshots of code and how your incorporating the .txt file with the code. Write a program that reads in temperatures from the text file temps.txt (file given in the assignment). Write the minimum temperature, maximum temperature and the new array (temperatures plus 10) to a new file. The temperatures in file are: 83 87 81 88 90 92 95

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...

    Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your program will reverse every line in the input. This program does not have an output file, as it modifies the input file directly. You may assume that your input file has 1000 lines or less, if this helps. sample input: Hello World! I study C++ following output: !dlroW olleH ++C yduts I

  • Write a program that opens a text file called input.txt and reads its contents into a...

    Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length. Input.txt This is a file is for test output.txt tset rof si elif...

  • C++ hashing code must read in an input file "input.txt", which is formatted as such: add...

    C++ hashing code must read in an input file "input.txt", which is formatted as such: add 0x20000000 addi 0x20000000 addiu 0x24000000 addu 0x21000000 and hash from an array with the values "0x2000000, 0x20000000, 0x24000000, 0x2100000" hard coded in. Then the output must read the value and then the item such as add, addi, addiu, addu, making it look like: 0x200000000 add 0x200000000 addi 0x240000000 addiu 0x210000000 addu

  • You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questio...

    You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questions. The text file has 10 numbers. 1. You will scan the 10 values located in “sensor.txt” to an array of variable type double, using a loop of your choice. 2. You will create a user defined function that will take your array as an input and find the average value. (Ex. arr[] = {15, 12, 13, 10}, the sum will be 50 then...

  • use c++13.12 1. Ql.cpp(40%) The questions are listed in the comments of C++ source code file...

    use c++13.12 1. Ql.cpp(40%) The questions are listed in the comments of C++ source code file Ql.cpp. a. Fill the blanks in file Ql.cpp at position or_. b. Follow the instructions in comments of source code file Q1cpp to modify the code at specified line. Compile Q1,cpp and execute it. Except for line 4, the program output will look like the c. following lines, because the random sequence may depend on the system. Errert Hewline charaeter 6556 51 1 53...

  • Answer with simple java code and comments This homework deals with the problem of partitioning an...

    Answer with simple java code and comments This homework deals with the problem of partitioning an array. We'll define partitioning to mean arranging the elements of an array around a certain "pivot, " element such that all elements to the left of the pivot are strictly less than the pivot, and all elements to the right of the pivot are greater than or equal to the pivot. To simplify things a little bit, we'll assume that the pivot element is...

  • + Run C Code IMPORTANT: • Run the following code cell to create the input file,...

    + Run C Code IMPORTANT: • Run the following code cell to create the input file, biostats.csv, which you will be using later. 74, In [ ]: N %%file biostats.csv Name, Sex, Age, Alex, M, 41, Bert, M, 42, Dave, M, 39, Elly, F, 30, Fran, F, 33, Jake, M, F, Luke, M, 34, F Myra, M, M, 38, Ruth, F, 28, 22 22 323 47 47, Height, Weight 170 200 167 70 115 143 139 280 98 75, 350...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT