d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 1000+ Fg For C++ Help
12Next
Add Reply New Topic New Poll
Member
Posts: 13,014
Joined: May 10 2011
Gold: 2,412.00
Apr 25 2014 08:31pm
I need to write a program for my c++ class. It is due at 12 tonight and I really have no idea how to do it.

I don't have any fg at the moment but if someone is willing to do the program for me I can get it quickly I promise. I will pay 1000 fg or more to whomever can do this for me

Description:
The purpose of this program is for you to (1) continue practicing with the use of arrays to manage program data, (2) learn how to declare and properly manage a dynamic array, and (3) to expose you to the use of structs to manage data in "packages."
For this project, you will create an application that does the following:
1. prompt your user for an input file name, and read in this filename
2. open the file, read in the first line, which will be an int, and use this value to size a dynamic array; this value also represents the number of words in the "dictionary" that appears in the file, following immediately after the int
3. read in all of the dictionary words from the file and set the count of each dictionary word to 0; define and use a struct to "package" the string (the word itself) and int (the number of occurrences of that word)
4. display the dictionary list, showing both the words and all counters set to 0
5. immediately after the dictionary words list will be the "data" part of the file that is to be processed; processing of this "data" portion will consist of reading in each line, then reading each word of the line and adding to the count of the dictionary words whenever these words are encountered in the file
6. after reading all lines in the file, close the file
7. display a report, showing the dictionary words and their counts
8. repeat if the user chooses to do so
NOTES
• the dictionary will be case sensitive; "The", for example, will be treated as different from "the"
• the appearance of the word in the dictionary list in the file does not count as an appearance of that word in the "data" part of the file
• there will be no punctuation in the "data" part of the input file

Objectives:
• define and use a struct to manage data
• declare, initialize, use, and delete a dynamic array
• process a text file for program input
• continue practice on the definition and use of functions to organize code
Requirements:
• your program source code must be documented correctly (see the starter.cpp example) [5 points]
• your program source code must be neatly and consistently formatted (see the starter.cpp example) [5 points]
• your program must compile and run without errors [5 points]
• your program cannot have any global variables (with the exception of CinReader reader) [5 points]
• your program must continue running until the user chooses to quit [5 points]
• your program must include and correctly use a dynamic array [10 points]
• your program must include and correctly use a struct defined to address the requirement as described above [10 points]
• your program must prompt for a filename and open and process that file [10 points]
• your program must display the dictionary list, showing all counters set to zero before the "data" portion of the file has been processed [10 points]
• your program must display the final tally, to include each word of the dictionary and a count of the number of times that word appeared in the "data" part of the file [10 points]


Sample file:

myfile.txt

----------------------------------------

3

dog cat eat

Once there was a dog

that liked to eat a cat

instead the cat ate the dog
Member
Posts: 21,407
Joined: Jun 15 2007
Gold: 120,303.00
Apr 25 2014 08:35pm
tl dr,

pm me this in shorter words or atleast brief description

can do this if its not a massive project

if it is i still can but maybe more price
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 25 2014 09:22pm
Do you still need help with this?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 25 2014 11:03pm
e/ shit, didn't realize there are suppose to be multiple words per line. gonna fix the code real quick
Code
#include <iostream>
#include <fstream>
#include <sstream>

/*
* Struct for managing data
*/
struct word_data {
std::string word;
unsigned occurrence;
};

int main(int argc, const char * argv[]) {
while(true) {
std::string filename;

/*
* Get dictionary filename from user.
*/
std::cout << "Enter filename: ";
std::cin >> filename;

/*
* File stream declaration.
* Attempt to open file that was
* specified by the user.
*/
std::ifstream file(filename);

/*
* Array that will store the words
* from our dictionary file.
*/
word_data* word_list = nullptr;

/*
* Make sure the file actually exists
* before continuing.
*/
if(file.is_open()) {
bool first_line = true;
int num_words = 0;
int list_size = 0;
unsigned counter = 0;
/*
* Success!
* Declare a string variable that
* will be used to store the current
* line of the file in iteration.
*/
std::string line;

/*
* Get contents of the file
*/
while(std::getline(file, line)) {
/*
* If first_line, get the specified
* number of words in the dictionary
* list.
*/
if(first_line) {
/*
* Convert string into integer
*/
std::istringstream buffer(line);
buffer >> num_words;

/*
* Exit if specified number of words is 0
*/
if(num_words == 0) {
std::cout << "Error, specified number of words is 0.\n";
return EXIT_FAILURE;
}

std::cout << "Dictionary list:\n\n";

/*
* Allocate space for words
*/
word_list = new word_data[ num_words ];
first_line = false;
continue;
}

/*
* Break out of loop and stop adding
* words if the words array is full.
*/
if(counter > num_words) {
break;
}

/*
* Declare word_data structure.
* Set the word and then occurrence
* to 0.
*/
word_data data;
data.word = line;
data.occurrence = 1;

std::cout << data.word << " : 0" << std::endl;

bool found = false;

/*
* Loop through list and check if
* word exists already. If it does,
* increment the occurrence.
*/
for(unsigned i = 0; i < counter; i++) {
if(line == word_list[ i ].word) {
word_list[ i ].occurrence++;
found = true;
break;
}
}

/*
* If the word doesn't exist in the
* the list, add it.
*/
if(found == false) {
word_list[ list_size ] = data;
list_size++;
}

counter++;
}

std::cout << "\nOccurrence of words in dictionary list:\n\n";

for(unsigned i = 0; i < list_size; i++) {
std::cout << word_list[ i ].word << " : " << word_list[ i ].occurrence << std::endl;
}
}
else {
/*
* Failed to open the file :(
*/
std::cout << "Error, failed to open file: " << filename << std::endl;
}

/*
* Close the file
*/
file.close();

/*
* Deallocate word_list array
*/
delete [ ] word_list;

/*
* Ask user if they want to exit
* or run the program again.
*/
char choice;

do {
std::cout << "Run program again? (Y / N): ";
std::cin >> choice;
} while(std::tolower(choice) != 'y' && std::tolower(choice) != 'n');

if(std::tolower(choice) == 'n') {
break;
}
}

return EXIT_SUCCESS;
}


Code
Enter filename: dictionary
Dictionary list:

this : 0
this : 0
this : 0
is : 0
is : 0
a : 0
a : 0
a : 0
a : 0
dictionary : 0
list : 0

Occurrence of words in dictionary list:

this : 3
is : 2
a : 4
dictionary : 1
list : 1
Run program again? (Y / N):


Dictionary file
Code
11
this
this
this
is
is
a
a
a
a
dictionary
list


This post was edited by SelfTaught on Apr 25 2014 11:29pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 25 2014 11:28pm
Code
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct Record
{
Record(){}
~Record(){}
string word;
int occurence;
};

//Dictionary
struct Dictionary
{
public:
Dictionary(int cap) : capacity(cap){ words = new Record[cap]; size = 0; }
~Dictionary() { delete words; }
void addWord(string);
void addOccurence(string);
void display();

private:
Record* words;
int size;
int capacity;
};

void Dictionary::addWord(string word)
{
this->words[this->size].word = word;
this->words[this->size].occurence = 0;
this->size++;
}

void Dictionary::addOccurence(string word)
{
for(int i = 0; i < this->size; i++)
if(this->words[i].word == word)
this->words[i].occurence++;
}

void Dictionary::display()
{
for(int i = 0; i < this->size; i++)
cout << this->words[i].word << " " << this->words[i].occurence << endl;
}
//End Dictionary


string readInputFileName();
void processFile(string);
Dictionary* readDictionaryFromFile(ifstream&);
void processDataFromFile(ifstream&, Dictionary*);
bool readUserChoice();


string readInputFileName()
{
string inputFileName;

cout << "Enter the input file name:" << endl;
cin >> inputFileName;

return inputFileName;
}

Dictionary* readDictionaryFromFile(ifstream& inputFile)
{
int numberOfWords;
Dictionary* dict;
string currentWord;

inputFile >> numberOfWords;

dict = new Dictionary(numberOfWords);

while(numberOfWords-- > 0)
{
inputFile >> currentWord;
dict->addWord(currentWord);
}

return dict;
}

void processDataFromFile(ifstream& inputFile, Dictionary* dict)
{
string line, word;

while(!inputFile.eof())
{
getline(inputFile,line);
istringstream iss(line);

while(iss >> word)
dict->addOccurence(word);

}
}

void processFile(string inputFileName)
{
Dictionary* dict;
ifstream inputFile;

//open file
inputFile.open(inputFileName.c_str(),ios::binary);

//create dictionary from file
dict = readDictionaryFromFile(inputFile);

//display dictionary with initial occurences
dict->display();

//read data from file and fill dictionary
processDataFromFile(inputFile,dict);

//close file
inputFile.close();

//display dictioanry with total occurences
dict->display();

//delete dictionary
delete dict;

}

bool readUserChoice()
{
char c;

cout << "Do you want to continue? (Y/y to continue. Any other key to quit)" << endl;
cin >> c;

return c == 'Y' || c == 'y';

}


int main()
{
bool choice;
string inputFileName;

do
{
inputFileName = readInputFileName();
processFile(inputFileName);
choice = readUserChoice();

}while(choice);

return 0;
}

Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 25 2014 11:31pm
Quote (Minkomonster @ Apr 25 2014 09:28pm)
Code
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct Record
{
    Record(){}
    ~Record(){}
    string word;
    int occurence;
};

//Dictionary
struct Dictionary
{
    public:
        Dictionary(int cap) : capacity(cap){ words = new Record[cap]; size = 0; }
        ~Dictionary() { delete words; }
        void addWord(string);
        void addOccurence(string);
        void display();

    private:
        Record* words;
        int size;
        int capacity;
};

void Dictionary::addWord(string word)
{
    this->words[this->size].word = word;
    this->words[this->size].occurence = 0;
    this->size++;
}

void Dictionary::addOccurence(string word)
{
    for(int i = 0; i < this->size; i++)
        if(this->words[i].word == word)
            this->words[i].occurence++;
}

void Dictionary::display()
{
    for(int i = 0; i < this->size; i++)
        cout << this->words[i].word << " " << this->words[i].occurence << endl;
}
//End Dictionary


string readInputFileName();
void processFile(string);
Dictionary* readDictionaryFromFile(ifstream&);
void processDataFromFile(ifstream&, Dictionary*);
bool readUserChoice();


string readInputFileName()
{
    string inputFileName;

    cout << "Enter the input file name:" << endl;
    cin >> inputFileName;

    return inputFileName;
}

Dictionary* readDictionaryFromFile(ifstream& inputFile)
{
    int numberOfWords;
    Dictionary* dict;
    string currentWord;

    inputFile >> numberOfWords;

    dict = new Dictionary(numberOfWords);

    while(numberOfWords-- > 0)
    {
        inputFile >> currentWord;
        dict->addWord(currentWord);
    }

    return dict;
}

void processDataFromFile(ifstream& inputFile, Dictionary* dict)
{
    string line, word;

    while(!inputFile.eof())
    {
        getline(inputFile,line);
        istringstream iss(line);

        while(iss >> word)
            dict->addOccurence(word);

    }
}

void processFile(string inputFileName)
{
    Dictionary* dict;
    ifstream inputFile;

    //open file
    inputFile.open(inputFileName.c_str(),ios::binary);

    //create dictionary from file
    dict = readDictionaryFromFile(inputFile);

    //display dictionary with initial occurences
    dict->display();

    //read data from file and fill dictionary
    processDataFromFile(inputFile,dict);

    //close file
    inputFile.close();

    //display dictioanry with total occurences
    dict->display();

    //delete dictionary
    delete dict;

}

bool readUserChoice()
{
    char c;

    cout << "Do you want to continue? (Y/y to continue. Any other key to quit)" << endl;
    cin >> c;

    return c == 'Y' || c == 'y';

}


int main()
{
    bool choice;
    string inputFileName;

    do
    {
        inputFileName = readInputFileName();
        processFile(inputFileName);
        choice = readUserChoice();

    }while(choice);

    return 0;
}


You're code is much cleaner than mine :P
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 25 2014 11:33pm
Quote (SelfTaught @ Apr 26 2014 12:31am)
You're code is much cleaner than mine :P


I had to stop myself. I almost split the Dictionary and Record into their own .h/.cpp respectively and made them classes. But the assignment specifically called for struct, so I left them be.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 25 2014 11:43pm
Quote (Minkomonster @ Apr 25 2014 09:33pm)
I had to stop myself. I almost split the Dictionary and Record into their own .h/.cpp respectively and made them classes. But the assignment specifically called for struct, so I left them be.


Lol. It would be cleaner that way but yeah it sounds like the assignment calls for C code. Not sure why that would be in a C++ class o_0
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 25 2014 11:46pm
Eh, correct me if I am wrong but the only difference between struct and class is all struct members are implicitly public, while class are private. And the assignment specifically asks the student to delete the dynamic array. Destructors are C++ only, right?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 26 2014 07:58am
Quote (Minkomonster @ Apr 25 2014 09:46pm)
Eh, correct me if I am wrong but the only difference between struct and class is all struct members are implicitly public, while class are private. And the assignment specifically asks the student to delete the dynamic array. Destructors are C++ only, right?


RIght
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll