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 Transcribed image text
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
  • Write the following Java code for a file called input.txt that has the below text in...

    Write the following Java code for a file called input.txt that has the below text in it. 5 6 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 2 1 0 3 1 1 1 1 1 2 3 4 5 1 2 0 1 hello 2 3 bye Create the Entry class. · Entry should have 3 data members o int x. o int y. o String name. Create a class called...

  • C++ Write a program that reads from the external file input.txt, counts the letters in every...

    C++ Write a program that reads from the external file input.txt, counts the letters in every word, replaces the word by that number, and then writes the numbers to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Also you may wish to use the strlen( ) function. Output: After the program generates output.txt, the code should display the contents of the file on the screen...

  • 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:...

  • 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

  • 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

  • Suppose we want to read in a file called "input.txt". We can do the following. File...

    Suppose we want to read in a file called "input.txt". We can do the following. File f = new File(input.txt); Scanner s = new Scanner (f); ... We know that Scanner constructor will throw a FileNotFoundException when the file does not exist. Since we already created a file object "f", the file does exist. So why do we need to either throw the exception or do try/catch block to in order to have the program continue? Thanks.

  • 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...

  • Please send this C++ code with ( // ) Comments 1. In main, you have the...

    Please send this C++ code with ( // ) Comments 1. In main, you have the following array declared: int arr[5] = {1, 5, 3, 2} a. What are the elements in the array? Create a function called displayArr that accepts the array and the size of the array, and displays every element in the array. b. Create a function called findAverage that accepts the array and the size of the array as arguments, and returns the average of all...

  • 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

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