Lexicographical sort - c++

I have to create program that takes text from file line-by-line, sorts it in lexicographic order and then if any word occured in previous line replace it with "-".
So far I've got sorting figured out, but I feel like I've hit a wall with replacing words that occured before.
My code so far looks like this:
#include <iostream>
#include <list>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
const string A = "abcdefghijklmnopqrstuwvxyz " ;
list<string> lista;
string line;
ifstream myfile("./file.txt");
while(!myfile.eof())
{
getline(myfile, line);
lista.push_back(line);
}
vector<string> l;
lista.sort();
copy(begin(lista), end(lista), back_inserter(l));
for(unsigned int i=0; i<l.size(); i++) // check if sorted properly
cout << l[i] << endl;
for(unsigned int i=0; i<l.size()-1; i++)
{
unsigned int end=0;
unsigned int j=1;
while(l[i][end]==l[j][end])
{
end++;
j++;
}
l[i].erase(0, end);
l[i].insert(0, "- ");
}
for(unsigned int i=0; i<l.size(); i++)
cout << l[i] << endl;
return 0;
}
As you can see, I attempted doing this replacement thing, but to no success.
Any help would be greatly appreciated!

Related

Writing a C++ program to print the letters of a string in a chaotic order

What I'm trying to do is this:
User enters a string (For example: "Hello")
The program returns the same string, but in a random order(It can be "elHlo" or any other order possible)
So far I've written this code, but the problem is that sometimes the randomly generated numbers are the same, so it might print the same indexes(letters) twice or more times:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
cout << "Say something: ";
string text;
getline(cin, text);
cout << "\nChaotic text: ";
srand(time(0));
for(unsigned int j=0; j<text.length(); j++){
int randomLetter = rand()%text.length();
cout << text.at(randomLetter);
}
return 0;
}
Can anyone help me fix it?
You can use std::shuffle (since C++11):
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
using namespace std;
int main(){
cout << "Say something: ";
string text;
getline(cin, text);
cout << "\nChaotic text: ";
std::mt19937 g(time(0));
std::shuffle(text.begin(), text.end(), g);
cout << text;
return 0;
}
Or std::random_shuffle (if you are using old specification):
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main(){
cout << "Say something: ";
string text;
getline(cin, text);
cout << "\nChaotic text: ";
srand(time(0));
std::random_shuffle(text.begin(), text.end());
cout << text;
return 0;
}
Instead of calling rand() one time, which can generate an index you have called before, you can keep generating a new index while keeping tracking of all generated indices in a hashtable.
std::unordered_map<int, bool> done;
for (unsigned int j = 0; j < text.length(); j++) {
int randomLetter = rand() % text.length();
while (done[randomLetter] == true) // while it's been marked as finished, generate a new index.
randomLetter = rand() % text.length();
cout << text.at(randomLetter);
done[randomLetter] = true; // mark it as finished.
}
Alternatively, you can use std::random_shuffle instead, which should save you the hassle.
std::random_shuffle (text.begin(), text.end());
std::cout << text << '\n';

Populating an array from a txt file in C++

I am having difficulty populating an array from a .txt file. I can do it without the while loop if I already know the size of the file. However, once I incorporate a while loop to extract the file size the input odes not configure correctly. Pleas take a look over my code an let me know if you see where I am going wrong.
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
int main()
{
using namespace std;
const char *inName_1 = "Instance_1.txt";
const char *inName_2 = "Instance_2.txt";
int arraySize_1 = 0, arraySize_2 = 0;
int array_1[20];
int array_2[20];
int number;
ifstream A2_file_1(inName_1);
if (A2_file_1.fail())
{
cout << "File 1 not open!" << '\n';
}
while (!A2_file_1.eof())
{
arraySize_1++;
A2_file_1 >> number;
}
if (A2_file_1.is_open())
{
for (int i = 0; i < arraySize_1; i++)
{
A2_file_1 >> array_1[i];
}
A2_file_1.close();
}
cout << "The size of the array 1 is: " << arraySize_1 << endl;
for (int i = 0; i < arraySize_1; i++)
{
cout << array_1[i] << endl;
}
return 0;
}
To read an arbitrary amount of numeric values from a text-file, all you need is an std::vector and a couple of std::istreambuf_iterator objects.
Then is as simple as
std::ifstream input("Instance_1.txt");
std::vector<int> values(std::istreambuf_iterator<int>(input),
std::istreambuf_iterator<int>());
That's it. Those four lines of code (counting the empty line) will read all int values from the text file Instance_1.txt and place them into the vector values.

Reading arrays from file c++

I'm trying to make a program that so far only needs to read a file and saves its content in an array. the cout was a test to see if the words would be saved to the array but it didn't work. When executed all it does is print to screen empty spaces and finally the name of the file.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <streambuf>
#include <ctime>
#include <time.h>
#define MAX 10000
void readFile(fstream& wordFile, string words[], int &wordarrayLength)
{
string word;
int i=0;
while(i < MAX)
{
getline(wordFile,word);
words[i] = word;
i++;
cout << words[i] << endl;
}
wordarrayLength = i;
wordFile.close();
}
int main()
{
string words[MAX];
int arraylength;
fstream file ("words.txt", ios::in);
readFile(file,words,arraylength);
}
I was missing the file the compiler was looking for. This code works fine.
Try something like this:
//since you are updating the array pass it in by reference as well.
void readFile(fstream& wordFile, string &words[], int &wordarrayLength)
{
int i=0;
while(i < MAX)
{
//each item in the array has to be it's own string
//the previous code simply reused the same string each time
string word;
getline(wordFile, word);
words[i] = word;
cout << words[i] << endl;
i++;
}
wordarrayLength = i;
wordFile.close();
}
Unfortunately I don't have your file or compiler so I can't debug it.

Sorting Dynamic Array With Data File

I have an assignment in which I have to read from a file called ("random.txt") output the total and copy the file into an array dynamically. Then sort the values in the file.
Up until line 20 my programs runs fine and outputs my total values as well as all the numbers in file.
Line 21 onward also runs but then it doesn't output the total I had in line 20 when I run it and it also doesn't display the values in order.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
int n=0;
double temp;
fin.open("data.txt");
fin>>temp;
while(fin)
{
n++;
fin>>temp;
cout<<temp<<endl;
}
cout<<"Total:"<<n<<endl;
fin.close(); //Program run fine up to here.
fin.open("data.txt");
double *A;
A=new double[n];
for (int i=0;i<n-1;i++)
for (int j=i;j<n;j++)
if (A[i]>A[j])
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
for (int i=0;i<n;i++)
{
while(fin)
{
n++;
fin>>A[i];
cout<<"Array:"<<A[i]<<endl;//Program runs up to here as well but
//but now doesn't print out the total I
//had in my program above and just
//prints A[i] and its not even sorted.
}
}
fin.close();
}
I know I have a lot of errors, I'm very new to c++ so I'm still trying to learn. To be quite honest I don't know what I'm doing starting from line 32. I understand that I sorted my array from 24-31, but I don't know how to read my file into my array or how to format it.
Your array load is in the wrong location. It should be before the sort, but after the allocation:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("data.txt");
double temp;
size_t n = 0;
while(fin >> temp)
{
std::cout << temp << ' ';
++n;
}
std::cout << "Total: "<< n << endl;
fin.close();
if (n > 0)
{
fin.open("data.txt");
double *A = new double[n];
for (int i=0; i<n && fin >> A[i]; ++i);
fin.close();
for (int i=0;i<n-1;i++)
{
for (int j=i;j<n;j++)
{
if (A[i]>A[j])
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
for (int i=0; i<n; i++)
cout << "Array[" << i << "]: " << A[i] << endl;
delete [] A;
}
}
Honestly, there are much better ways to do this. Using the standard library containers, this entire menagerie is reduced to...
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
int main()
{
std::ifstream fin("data.txt");
std::vector<double> A{
std::istream_iterator<double>(fin),
std::istream_iterator<double>() };
std::sort(std::begin(A), std::end(A));
for (auto x : A)
std::cout << x << '\n';
}
You need to assign values to the elements of A before you can sort or read from A!
Your sort in lines 24-31 is misplaced. A is defined on lines 22 and 23 but never initialized. I think you meant to read it from data.txt after reopening on line 21 but you did not.

array size and split of string

Ok guy i had to make a program to split elements of a string. And after that print those words.
there are some problems i am facing:
1) the array prints more than the size of the words in string i want that it should end printing as soon as last word is printed. i tried to prevent that but it always gives runtime error when i try to break at the last word.
2)is there any other efficient way to split and print ???
#include <sstream>
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include <string>
using namespace std;
int main()
{
std::string line;
std::getline(cin, line);
string arr[1000];
int i = 0;
int l=line.length();
stringstream ssin(line);
while (ssin.good() && i < l)
{
ssin >> arr[i];
++i;
}
int size = sizeof(arr) / sizeof(arr[0]);
for(i = 0; i <size; i++){
cout << arr[i] << endl;
}
return 0;
}
int size = sizeof(arr) / sizeof(arr[0]);
That is a compile time value, and it's always going to be the number of elements in your array (1000). It has no idea how many strings you assigned to in your loop. You stored the number of successfully read strings (plus 1) in the i variable, so you could do this instead:
int size = i - 1;
But if it were up to me, I would just use a growable structure, like vector (#include <vector>)
std::vector<std::string> arr;
std::string temp;
while (ssin >> temp)
{
arr.push_back(temp);
}
for (auto const & str : arr)
{
std::cout << str << std::endl;
}
/* If you're stuck in the past (can't use C++11)
for (std::vector<std::string>::iterator = arr.begin(); i != arr.end(); ++i)
{
std::cout << *i << std::endl;
}
*/
For general purpose character based splitting, I would much prefer boost::split (I know you can't use it, but for future reference)
std::vector<std::string> arr;
boost::split(arr, line, boost::is_any_of(".,;!? "));
Read up on the function strtok. It is old school but very easy to use.
1) there are a couple of changes you should make to your program:
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string line("hello string world\n");
string arr[1000];
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 1000)
{
ssin >> arr[i++];
}
int size = i-1;
for(i = 0; i < size; i++){
cout << i << ": " << arr[i] << endl;
}
return 0;
}
namely, you don't want to print sizeof(arr)/sizeof(arr[0]) (i.e. 1000) elements. There is no point in the condition i < l
2) stringstream is fine if you just want to separate the single strings; if more is needed, use boost/tokenizer for splitting strings. It's modern c++, once you try it you'll never come back!
this is the best method i think no worry now
#include <sstream>
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
std::string str;
std::getline(cin, str);
string arr[100];
int l=0,i;
char * cstr = new char [str.length()+1];
std::strcpy (cstr, str.c_str());
// cstr now contains a c-string copy of str
char * p = std::strtok (cstr,".,;!? ");
while (p!=0)
{
//std::cout << p << '\n';
arr[l++]=p;
p = strtok(NULL,".,;!? ");
}
for(i = 0; i <l; i++)
{
cout << arr[i] << endl;
}
delete[] cstr;
return 0;
}