Answer to Question #228783 in C++ for kolobo

Question #228783

For this task you are asked to write a program that consists of a le recap.cpp and a

make le to compile and run it. You will need to include a blank data.txt le in your

submission as well. Your make fille must compile the executable as main. 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. 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,toaster,iron

4,spanner,wrench,hammer

Terminate each line of output with a newline.

The following libraries are allowed:

- iostream

- fstream

- string

- sstream


1
Expert's answer
2021-08-23T13:39:43-0400

Contents of Data File as data.txt


10,a,b,c,d

2,b,b,c,d,hello

5,e,b,c,d

3,toaster,iron

4,spanner,wrench,hammer


#include<iostream>
#include <stdio.h>
#include <cmath>
#include <string>
using namespace std;
#include <omp.h>
#include <stdio.h>


#define NO_OF_STUDENTS	10


//Read text file and split string into numbers
string S;
vector<string> k;
#define MAX_EMP	50
vector<string> splitString(string str, string delimiter)
{
	vector <string> result;
    size_t pos=0;
	string token;


	while((pos = str.find(delimiter))!= std::string::npos)	
	{
		token = str.substr(0,pos);
		result.push_back(token);
		str.erase(0,pos+delimiter.length());
	}
		
	if(!str.empty()) result.push_back(str);
	return(result);
}


main(void)
{
	int r,c,ID[NO_OF_STUDENTS],LineSize[NO_OF_STUDENTS];
	int t,n=0,i,j,temp;
	string Items[NO_OF_STUDENTS][NO_OF_STUDENTS],z,fileName = "data.txt",str;


	std::ifstream in(fileName.c_str());
	
	for(i=0;i<NO_OF_STUDENTS;i++) LineSize[i]=0;
    if(!in)
    {
        std::cerr << "Cannot open the File : "<<endl;
        return false;
    }
    n=0;
    cout<<"\n\nInput File Contents:\n";
    while (std::getline(in, str))
    {
		z = "";
	    if(str.size() > 0)            z = z + str;
		k = splitString(&z[0],",");
		LineSize[n] = k.size();
		for(i=0;i<k.size();i++)
		{
			cout<<"\t"<<k[i];
			if(i==0) ID[n] = atoi(k[i].c_str());
			Items[n][i] = k[i];
		}
		cout<<"\n";
		n++;
	}
    in.close();	


    for (i = 0; i < n; ++i) 
    {
        for (j = i + 1; j < n; ++j) 
        {
            if (ID[i] > ID[j]) 
                {
                    temp = ID[i];
                    ID[i] = ID[j];
                    ID[j] = temp;
                }
            }
    }	
	cout<<"\nSorted Order:\n";
	for (i = 0; i < n; ++i) 
	{
		for(j=0;j<n;j++) 
		{
			if(ID[i] == atoi(Items[j][0].c_str())) 
			{
				for(int u=0;u<LineSize[j];u++) cout<<"\t"<<Items[j][u];
			}
		}
		cout<<"\n";
	}
 
}


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