Question

In: Computer Science

I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...

I haves code on bottom.
what do i need to edit?

Create a subdirectory called proj1

For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory.

The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z'). Words are case insensitive ("AA", "Aa", "aA", and "aa" are the same). A number is defined as a sequence of digits ('0'..'9'). Note that both words and numbers can be of length of 1, that is, contain one letter or one digit, respectively. Different sequences represent different numbers. For example, number "001" is different from number "1". Words are separated by numbers or other non-letter and non-digit characters. Numbers are separated by words or other non-letter and non-digit characters. Your program should record the number of times each word, number, and character happens (note that characters are case sensitive). The program should then output the ten most used characters (case sensitive), the ten most used numbers, and the ten most used words (case insensitive) as well as the number of times these characters/numbers/words are used. Since words are case insensitive, the program only outputs lower case words. The characters, numbers and words should be outputted in the descending order based on the number of times they are used. When two characters happen in the same number of times, the character with a smaller ASCII value should be considered as being used more frequently. When two words (numbers) happen in the same number of times, the word (number) that occurs earlier in the input should be considered as being used more frequently. 

An example executable code of the program proj1.x is provided to you. In proj1.x, the output related to the display of characters, words, and numbers is aligned in the following manner: the width of the column of the characters, words, and numbers is the length of the longest words and numbers to be displayed, plus five (5). You should make the outputs of your program the same as those of 'proj1.x'. When printing characters, use '\t' for tab and '\n' for newline. All other characters should be outputted normally.

Write a makefile for your project that compiles an executable called proj1.x

You are encouraged to use any C++ STL containers and algorithms. You should also use C++ string class instead of the built-in string type.

Your program must be able to compile and run on linprog.

I have some code. What do I need to do to satisfy the requirements from my code
include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;

int main() {

string s = "aaa";
bool g;
char str[100];
char str1[100];
char str2 [100];
int charCount = 0;
int c[100];
int num[100];
int numCount = 0;
int p = 0;
for (int i = 0; i < s.length(); i++)
{

if (isalpha(s[i]))
{
    str[i] = s[i];
    for(int j = 0; j < s.length();j++)
    {
   // checks to see how many times the char appears
    if(str[i] == s[j])
    {
        p++;
    }
  
        c[i] = p;
        p = 0;
    }
    cout << "Alpha: " << str[i] << " and shows " << c[i] << " times" << endl;
}

else if (isdigit(s[i]))
{
    str1[i] = s[i];
    cout << "Number: " << str1[i] << endl;
}

else
{
    str2[i] = s[i];
    cout <<"Char: " << str2[i] <<endl;
}
}

    return 0;
}

Solutions

Expert Solution

Answer:-

The below is the required source code for the given problem in C++

Code:-

#include<iostream>
#include<vector>

using namespace std;

class getMost{
   private:
       //charVect holds the count of each character in each index corresponding with the ascii value
       vector<int> charVect;
       //charCount holds the total number of different ascii values read in
       int charCount;

       //wordVect[0][0] will be the total number of different words, wordVect[i][0] will be the number of instances for each individual word
       //wordVect[i][1->n] will hold each ascii value for the each word.
       vector<vector<unsigned int> > wordVect;
       vector<unsigned int> wordCount;
       vector<unsigned int> wordTemp;
       //numVect is made in the same structure of wordVect
       vector<vector<unsigned int> > numVect;
       vector<unsigned int> numCount;
       vector<unsigned int> numTemp;

       //i is the current ascii value read in
       char i;
   public:
       //getMost constructor sets the structure of the vectors as explained above
       getMost(){
           charVect.resize(128);
           charCount = 0;
      
           wordVect.resize(1);
           wordCount.resize(1);
           wordCount[0] = 0;
           wordVect[0] = wordCount;
           wordTemp.resize(1);
           wordTemp[0]=1;

           numVect.resize(1);
           numCount.resize(1);
           numCount[0] = 0;
           numVect[0] = numCount;
           numTemp.resize(1);
           numTemp[0] = 1;
       }

       //reads in each character, calling a different set of functions dependend on the ascii type
       void parseList(){
           while(cin.get(i)){
               if((i >= 48) && (i <= 57)){
                   numList();
                   charList();
               }
               else{
                   charList();
               }
           }
       };

       //fills the charVect with any ascii value and iterates charCount if its the first time seeing i's ascii value
       //calls wordList() if the ascii value is a letter
       void charList(){
           int a = charVect[i];
           charVect[int(i)]++;
           if(a==0){
               charCount++;
           }
           if(((i >= 65) && (i <= 90)) || ((i >= 97) && (i <= 122))){
               wordList();
           }
       };

       //fills the wordVect
       void wordList(){
           if((i >= 65) && (i <= 90)){
                i = i+32;
                wordTemp.push_back (i);
            }
           else{
               wordTemp.push_back (i);
            }
            if((cin.peek() < 65) || ((cin.peek() > 90) && (cin.peek() < 97)) || (cin.peek() > 122)){
                bool match = false;
                unsigned int matchIndex;
                for(unsigned int a = 1; a < wordVect.size(); a++){
                    if(wordVect[a].size()==wordTemp.size()){
                        for(unsigned int b = 1; b < wordTemp.size(); b++){
                            if(wordTemp[b]==wordVect[a][b]){
                                if(b==(wordTemp.size()-1)){
                                    match = true;
                                    matchIndex = a;
                                    a = wordVect.size();
                                }
                            }
                            else if(wordTemp[b]!=wordVect[a][b]){
                                match=false;
                                b = wordTemp.size();
                            }
                        }
                    }
                }
                if(match==false){
                    wordVect[0][0]++;
                    wordVect.push_back (wordTemp);
                    wordTemp.clear();
                    wordTemp.push_back (1);
                }
                else if(match==true){
                    wordVect[matchIndex][0]++;
                   wordTemp.clear();
                   wordTemp.push_back (1);
                }
            }
       };

       void numList(){
           numTemp.push_back (i);
           if((cin.peek() < 48) || (cin.peek() > 57)){
               bool match = false;
               unsigned int matchIndex;
               for(unsigned int a = 1; a < numVect.size(); a++){
                   if(numVect[a].size() == numTemp.size()){
                       for(unsigned int b = 1; b < numTemp.size(); b++){
                           if(numTemp[b]==numVect[a][b]){
                               if(b==(numTemp.size()-1)){
                                   match = true;
                                   matchIndex = a;
                                   a = numVect.size();
                               }
                           }
                           else if(numTemp[b]!=numVect[a][b]){
                               match=false;
                               b = numTemp.size();
                           }
                       }
                   }
               }
               if(match==false){
                   numVect[0][0]++;
                   numVect.push_back (numTemp);
                   numTemp.clear();
                   numTemp.push_back (1);
               }
               else if(match==true){
                   numVect[matchIndex][0]++;
                   numTemp.clear();
                   numTemp.push_back (1);
               }
           }
       };

       void printList(){
           unsigned int largest = 1;
           for(unsigned int i = 1; i < wordVect.size(); i++){
               if(wordVect[i].size() > largest){
                   largest = wordVect[i].size();
               }
           }
           for(unsigned int i = 1; i < numVect.size(); i++){
               if(numVect[i].size() > largest){
                   largest = numVect[i].size();
               }
           }
           largest+=5;
          
           //print characters
           if(charCount>=10){
               cout << "Total " << charCount << " different characters, 10 most used characters: " << endl;
               for(int i = 0; i < 10; i++){
                    int maxValue = i;
                    int maxCount = charVect[i];
                    for(int j = 0; j < 128; j++){
                        if(charVect[j] > maxCount){
                            maxValue = j;
                            maxCount = charVect[j];
                        }
                    }
                   if(maxValue==10){
                       cout << "No. " << i << ": \\n";
                       for(unsigned int i = 0; i < largest-2; i++){
                           cout << " ";
                       }
                       cout << maxCount << endl;
                       charVect[maxValue]=0;  
                   }
                   else if(maxValue!=10){
                       cout << "No. " << i << ": " << char(maxValue);
                       for(unsigned int i = 0; i < largest-1; i++){
                           cout << " ";
                       }
                       cout << maxCount << endl;
                       charVect[maxValue]=0;
                   }
               }
           }
           else if(charCount<10){
                cout << "Total " << charCount << " different characters, " << charCount << " most used characters: " << endl;
                for(int i = 0; i < charCount; i++){
                    int maxValue = i;
                    int maxCount = charVect[i];
                    for(int j = 0; j < 128; j++){
                        if(charVect[j] >= maxCount){
                            maxValue = j;
                            maxCount = charVect[j];
                        }
                    }
                   if(maxValue==10){
                        cout << "No. " << i << ": \\n";
                        for(unsigned int i = 0; i < largest-2; i++){
                            cout << " ";
                        }
                        cout << maxCount << endl;
                        charVect[maxValue]=0;
                    }
                    else if(maxValue!=10){
                        cout << "No. " << i << ": " << char(maxValue);
                        for(unsigned int i = 0; i < largest-1; i++){
                            cout << " ";
                        }
                        cout << maxCount << endl;
                        charVect[maxValue]=0;
                    }
   /*
                    cout << "No. " << i << ": " << char(maxValue);
                   for(unsigned int i = 0; i < largest-1){
                       cout << " ";
                   }
                   cout << maxCount << endl;
                    charVect[maxValue]=0;
                }
*/
            }
           cout << endl;  

           //print words
           if(wordVect[0][0]>=10){
                cout << "Total " << wordVect[0][0] << " different words, 10 most used words: " << endl;
                for(unsigned int i = 0; i < 10; i++){
                    unsigned int maxCount = 0;
                    unsigned int maxIndex = 1;
                    for(unsigned int j = 1; j < wordVect.size(); j++){
                        if(wordVect[j][0] > maxCount){
                            maxIndex = j;
                            maxCount = wordVect[j][0];
                        }
                    }
                   cout << "No. " << i << ": ";
                    for(unsigned int k = 1; k < wordVect[maxIndex].size(); k++){
                        cout << (char)wordVect[maxIndex][k];
                    }
                   for(unsigned int i = 0; i < largest-wordVect[maxIndex].size(); i++){
                       cout << " ";
                   }
                   cout << maxCount << endl;
                    wordVect[maxIndex][0]=0;
                }
            }

           else if(wordVect[0][0]<10){
               cout << "Total " << wordVect[0][0] << " different words, " << wordVect[0][0] << " most used words: " << endl;
               for(unsigned int i = 0; i < wordVect[0][0]; i++){
                   unsigned int maxCount = 0;
                    unsigned int maxIndex = 1;
                    for(unsigned int j = 1; j < wordVect.size(); j++){
                        if(wordVect[j][0] > maxCount){
                            maxIndex = j;
                            maxCount = wordVect[j][0];
                        }
                    }
                    cout << "No. " << i << ": ";
                    for(unsigned int k = 1; k < wordVect[maxIndex].size(); k++){
                        cout << (char)wordVect[maxIndex][k];
                    }
                   for(unsigned int i = 0; i < largest-wordVect[maxIndex].size(); i++){
                       cout << " ";
                   }
                   cout << maxCount << endl;
                   wordVect[maxIndex][0]=0;
               }
           }
           cout<<endl;

           //print numbers
           if(numVect[0][0]>=10){
               cout << "Total " << numVect[0][0] << " different numbers, 10 most used numbers: " << endl;
               for(unsigned int i = 0; i < 10; i++){
                   unsigned int maxCount = 0;
                   unsigned int maxIndex = 1;
                   for(unsigned int j = 1; j < numVect.size(); j++){
                        if(numVect[j][0] > maxCount){
                            maxIndex = j;
                            maxCount = numVect[j][0];
                        }
                   }
                   cout << "No. " << i << ": ";
                   for(unsigned int k = 1; k < numVect[maxIndex].size(); k++){
                       cout << (char)numVect[maxIndex][k];
                   }
                   cout << "\t\t" << maxCount << endl;
                   numVect[maxIndex][0]=0;
               }
           }
           else if(numVect[0][0]<10){
               cout << "Total " << numVect[0][0] << " different numbers, " << numVect[0][0]<< " most used numbers: " << endl;
               for(unsigned int i = 0; i < numVect[0][0]; i++){
                    unsigned int maxCount = 0;
                   unsigned int maxIndex = 1;
                   for(unsigned int j = 1; j < numVect.size(); j++){
                       if(numVect[j][0] > maxCount){
                           maxIndex = j;
                           maxCount = numVect[j][0];
                       }
                   }
                   cout << "No. " << i << ": ";
                   for(unsigned int k = 1; k < numVect[maxIndex].size(); k++){
                       cout << (char)numVect[maxIndex][k];
                   }
                   cout << "\t\t" << maxCount << endl;
                   numVect[maxIndex][0]=0;
               }
           }
  
       };
};

int main(){
       getMost test;
       test.parseList();
       test.printList();

   return 0;
}

If you find any difficulty with the code, please let know know I will try for any modification in the code. Hope this answer will helps you. If you have even any small doubt, please let me know by comments. I am there to help you. Please give Thumbs Up,Thank You!! All the best


Related Solutions

use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
I need to understand how I would edit this code without changing anything, only adding. I...
I need to understand how I would edit this code without changing anything, only adding. I am not looking for the exact answer, just information on how I would use the given code to complete some of the todo statements. Thank you! // include this header file so we can use `printf()` #include <stdio.h> // every C program must implement the `main()` function int main(int argc, char *argv[]) {    //TODO: check for enough arguments       // save the...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
I need to create a code in C++ that first has a menu animation of game...
I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
Your code needs to do the following: Create a function called pigLatin that accepts a string...
Your code needs to do the following: Create a function called pigLatin that accepts a string of English words in the parameter sentence and returns a string of those words translated into Pig Latin. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. For example the sentence “The quick brown fox” becomes “hetay uickqay rownbay oxfay”. You may assume the words in the parameter...
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT