Question

can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

can you please split this program into .h and .cpp file

#include <iostream>
#include<string>
#include<fstream>
#define SIZE 100
using namespace std;

//declare struct
struct word_block {
   std::string word;
   int count;
};
int getIndex(word_block arr[], int n, string s);

int main(int argc, char **argv)
{
   string filename="input.txt";
   //declare array of struct word_block
   word_block arr[SIZE];
   int count = 0;

   if (argc < 2)
   {
       cout << "Usage: " << argv[0] << " inputfilename" << endl;
       return -1;
   }

   //filename = argv[1];
   //cout << filename << endl;
   //open file for reading
   ifstream in;
   in.open(filename);
   //check if file can be opened
   if (!in)
   {
       cout << "Not able to open the input file " << filename << endl;
       return -1;
   }
   //read file till EOF
   string str;
   int i;

   while (!in.eof())
   {
       in >> str;
      
       //check if the word is presernt in the array of struct
      
       if ((i = getIndex(arr, count, str)) >= 0)
       {
           ++arr[i].count;
       }
       else
       {
           arr[count].word = str;
           arr[count].count = 1;
           ++count;
       }
      
   }
  
   word_block tmp;
   //sort the array in descending order of word count using bubble sort
   for (int i = 0; i < count; i++)
   {
       for (int j = 0; j < count - i - 1; j++)
       {
           if (arr[j].count < arr[j + 1].count)
           {
               tmp = arr[j + 1];
               arr[j + 1]= arr[j];
               arr[j] = tmp;

           }
       }
   }
   //print the output
   for (int i = 0; i < count; i++)
   {
       cout << arr[i].word << " appears " << arr[i].count << " times" << endl;
   }
   return 0;
}

int getIndex(word_block arr[], int n, string s)
{
   for (int i = 0; i < n; i++)
   {
       if (arr[i].word == s)
       {
           return i;
       }
   }
   return -1;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//header file

#ifndef WORD_H
#define WORD_H
#define SIZE 100
struct word_block {
std::string word;
int count;
};
int getIndex(word_block arr[], int n, string s);
#endif

//cpp file

#include <iostream>
#include<string>
#include<fstream>
#include "word.h"

using namespace std;

int main(int argc, char **argv)
{
string filename="input.txt";
//declare array of struct word_block
word_block arr[SIZE];
int count = 0;

if (argc < 2)
{
cout << "Usage: " << argv[0] << " inputfilename" << endl;
return -1;
}

//filename = argv[1];
//cout << filename << endl;
//open file for reading
ifstream in;
in.open(filename);
//check if file can be opened
if (!in)
{
cout << "Not able to open the input file " << filename << endl;
return -1;
}
//read file till EOF
string str;
int i;

while (!in.eof())
{
in >> str;
  
//check if the word is presernt in the array of struct
  
if ((i = getIndex(arr, count, str)) >= 0)
{
++arr[i].count;
}
else
{
arr[count].word = str;
arr[count].count = 1;
++count;
}
  
}
  
word_block tmp;
//sort the array in descending order of word count using bubble sort
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (arr[j].count < arr[j + 1].count)
{
tmp = arr[j + 1];
arr[j + 1]= arr[j];
arr[j] = tmp;

}
}
}
//print the output
for (int i = 0; i < count; i++)
{
cout << arr[i].word << " appears " << arr[i].count << " times" << endl;
}
return 0;
}

int getIndex(word_block arr[], int n, string s)
{
for (int i = 0; i < n; i++)
{
if (arr[i].word == s)
{
return i;
}
}
return -1;
}

Add a comment
Know the answer?
Add Answer to:
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT