At the beginning of the program, the code will open and read values in from a file.
The file will have an unknown number of lines. The program is required to read this data, line by line, into a 2D dynamic array of strings.
The format of the line is :
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.
The 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 of this output is as follows (for 2 orders):
3,cat,dog,mice
4,black,red,blue
#include <iostream>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream file ( "data.txt") ;
if (!file.is_open())
cout<<"\nCannot open the file"<<endl;
char my_array [3][3]={};
my_array[0][0]='1';
my_array[0][1]='c';
my_array[0][2]='d';
my_array[1][0]='2';
my_array[1][1]='m';
my_array[1][2]='b';
my_array[2][0]='3';
my_array[2][1]='q';
my_array[2][2]='r';
cout<<"The array in ascending order is: "<<endl;
for (int i=0; i < 3; ++i) {
for (int j=0; j < 3; ++j) {
file>> my_array[i][j];
}
}
for (int i; i < 3; ++i) {
for (int j=0; j < 3; ++j){
cout<<my_array[i][j]<<"\t";
}
cout<<"\n";
}
return 0;
}
Comments
Leave a comment