Answer to Question #230082 in C++ for xobzela

Question #230082

At the start of your program, your code will open and read values in from a le data.txt.

The le will have an unknown number of lines. You are required to read this data, line

by line, into a 2D dynamic array of strings.

The format of the line is as follows:

1,a,b,c,d

Each line starts with an ID, which is an int, and then will be followed by an unknown

number of strings separated by commas. Each string in the line is associated with the ID

of the line.

Your 2D dynamic array should be sized to exactly the size of the number of items in each

line. Each line will have at least the ID and 1 item.

Once the items are stored in the array you need to print out the array in ascending order

based on the order IDs in the following format:

ID,a,b,c

An example:

4,spanner,wrench,hammer

Terminate each line of output with a newline. Remember to deallocate any memory you have used at the end of your program.

The following libraries are allowed: iostream, fstream, string, sstream


1
Expert's answer
2021-08-27T03:25:17-0400
#include <iostream>
#include <fstream>
#include <iostream>


using namespace std;


int main()
{
    ifstream file { "data.txt" };
    if (!file.is_open()) cout<<"\nCannot open the file";
    
    int my_array [3][5]{};
    for (int i{}; i != 3; ++i) {
        for (int j{}; j != 5; ++j) {
            file >> my_array[i][j];
        }
    }
    for (int i{}; i != 3; ++i) {
        for (int j{}; j != 5; ++j){
            cout<<my_array[i][j]<<"\t";
        }
        cout<<"\n";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS