Question

In: Computer Science

C++ Programming The following program reads the sentences in "fdata.txt" and stores each word in a...

C++ Programming
The following program reads the sentences in "fdata.txt" and stores each word in a word vector starting with the first letter.
Fill in the blanks.

#include "pch.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>


using namespace std;

class SVector
{
public:
int size;
int idx;
char x;
string *sptr;
// (a)
void insert(string s);
void print();
};

SVector::SVector(char c, int size)
{
x = c;
idx = 0;
this->size = size;
//(b)
}
void SVector::insert(string s)
{
//(c)
}
void SVector::print()
{
if(idx != 0){
cout << "starting with " << x << endl;
for(int i = 0; i < idx; i++){
cout << sptr[i] << endl;
}
}
}

int main()
{
SVector *sv[26];
string word;
ifstream fin;
fin.open(".\\data\\fdata.txt");
//cout << fin << endl;
if(!fin){
cout << "file not opened" << endl;
return 0;
}

for(int i = 0; i < 26; i++){
char c = 'a' + i;
sv[i] = //(d);
}

while(true){
fin >> word;
if(fin.eof()) break;
int fc = //(e);
sv[fc]->insert(word);
}

for(int i = 0; i < 26; i++){
sv[i]->print();
}
  
return 0;
}

Solutions

Expert Solution


#include "pch.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>


using namespace std;

class SVector
{
    public:
    int size;
    int idx;
    char x;
    string *sptr;
    SVector(char c, int size);// (a)
    void insert(string s);
    void print();
};

SVector::SVector(char c, int size)
{
    x = c;
    idx = 0;
    this->size = size;
    sptr=new string[size];
}
void SVector::insert(string s)
{
    //(c)
    idx+=1;
    sptr[idx]=s;
}
void SVector::print()
{
    if(idx != 0)
    {
        cout << "starting with " << x << endl;
        for(int i = 0; i < idx; i++)
        {
            cout << sptr[i] << endl;
        }
    }
}

int main()
{
    SVector *sv[26];
    string word;
    ifstream fin;
    fin.open(".\\data\\fdata.txt");
    //cout << fin << endl;
    if(!fin)
    {
        cout << "file not opened" << endl;
        return 0;
    }
  
    for(int i = 0; i < 26; i++)
    {
        char c = 'a' + i;
        sv[i] = new SVector(c,100);//(d);
    }
  
    while(true)
    {
        fin >> word;
        if(fin.eof()) break;
        int fc =int(word[0]-'a') //(e);
        sv[fc]->insert(word);
    }
  
    for(int i = 0; i < 26; i++)
    {
        sv[i]->print();
    }
    
    return 0;
}


Related Solutions

C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Q#2 Write a C++ program that reads 10 integer values and stores them in an array....
Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average.
Q#1 Write a C++ program that reads n integer values and stores them in an array...
Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements. Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those integers into an array and sorts the integers in the array. The program should then prompt the user for an index, and then display the integer at that index. For example, if the array contains ten integers and the contents are 79 4 42 51 12 22 33 17 91 10 after sorting, the integer at index 6 is 42 You are encouraged to...
2. For each of the following sentences, fill in the blanks with the best word or...
2. For each of the following sentences, fill in the blanks with the best word or phrase in the list below. Not all words or phrases will be used; use each word or phrase only once During transcription in __________________ cells, transcriptional regulators that bind to DNA thousands of nucleotides away from a gene’s promoter can affect a gene’s transcription. The __________________ is a complex of proteins that links distantly bound transcription regulators with the proteins bound closer to the...
4. For each of the following sentences, fill in the blanks with the best word or...
4. For each of the following sentences, fill in the blanks with the best word or phrase selected from the list below. Not all words or phrases will be used; use each word or phrase only once. Microtubules are formed from the tubulin heterodimer, which is composed of the nucleotide-binding __________________ protein and the __________________ protein. Tubulin dimers are stacked together into protofilaments; __________________ parallel protofilaments form the tubelike structure of a microtubule. __________________ rings are important for microtubule nucleation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT