Question

In: Computer Science

Done in C++ please. And use #include iostream, string, and fstream. do not use #include algorithm....

Done in C++ please. And use #include iostream, string, and fstream. do not use #include algorithm.

Question 1 In this question, you will read words from a file and place them into an array of type string.

1- Make a data text file “words.txt” – that contains one word on each line. Use at least 20 words.

2- Now write a program that reads the words in the file into an array of strings (a repeated word should not be inserted into the array – your program should not allow that and you should make sure your data file has duplicate words to test this functionality). Make your array size enough to hold 1000 words. To read the words into the array, you should make a function that takes a string array, a data size integer (dsize) by reference and an ifstream – please look at the example we did in class.

3- Declare your array and file streams in main and call the function that reads the data into the array.

4- Write a printArray function that takes a string array, the dsize and an ostream object so that you could print to the console or to an output file.

5- Print your array from main by calling the printArray function – once to the console and once to a file “wordsoutput.txt”.

6- Use the selectionSort Algorithm that we covered in class to sort the array.

7- Repeat #5 and make sure the array is sorted.

8- Find the maximum string and the minimum string in the array (remember arrays are compared based on the ASCII value).

9- Write a function that takes a string and converts every character of it to uppercase. Now call that function from a function that you pass the array and dsize to, to uppercase all words in the array (convert all words in the array to uppercase letter) and call that from main passing your array to that function. Print the array.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// words.txt

beautiful
zone
lemon
rabbit
console
ambrane
quit
beautiful
zone
rabbit
console
brain
quality
price
increment
quit
beautiful
zone
gemini
algorithm

______________________

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;

//Function declarations
int readData(ifstream &dataIn,string array[]);
bool isDuplicate(string word,string array[],int cnt);
void printArray(string array[],int cnt);
void selectionSort(string names[],int count);
   string maxString(string names[],int count);
   string minString(string names[],int count);
   void converToUpperCase(string names[],int count);
int main() {
   ifstream dataIn;
   //Declaring variables
   const int size=1000;
string array[size]={""};
   int cnt=0;
  
   dataIn.open("words.txt");
   //checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
   cnt=readData(dataIn,array);
   cout<<"Displaying the array :"<<endl;
   printArray(array,cnt);
  
   selectionSort(array,cnt);
   cout<<"Displaying the array after sorting :"<<endl;
   printArray(array,cnt);  
   string max=maxString(array,cnt);
   cout<<"Maximum String :"<<max<<endl;
   string min=minString(array,cnt);
   cout<<"Minimum String :"<<min<<endl;
   converToUpperCase(array,cnt);
   cout<<"Displaying the array after convertingh to upper-case :"<<endl;
   printArray(array,cnt);  
  
}
  
  
   return 0;
}
int readData(ifstream &dataIn,string array[])
{
   int cnt=0;
   string word;
   while(dataIn>>word)
   {
       if(!isDuplicate(word,array,cnt))
       {
           array[cnt]=word;
           cnt++;
       }
   }
   return cnt;
}
bool isDuplicate(string word,string array[],int cnt)
{
   for(int i=0;i<cnt;i++)
   {
       if(array[i].compare(word)==0)
       return true;
   }
   return false;
}
void printArray(string array[],int cnt)
{
   for(int i=0;i<cnt;i++)
   {
       cout<<array[i]<<endl;
   }
}


void selectionSort(string names[],int count)
{
int start;
string temp;
for (int i = count - 1; i > 0; --i)
{
start = 0;
for (int j = 1; j <= i; ++j)
{
if (names[j].compare(names[start]) > 0)
{
start = j;
}
temp = names[start];
names[start] = names[i];
names[i] = temp;
}
}    
   }
       string maxString(string names[],int count)
       {
  
               int maxAscii=0,val;
               int indx=0;
               for(int i=0;i<names[0].length();i++)
               {
                   maxAscii+=(int)names[0].at(i);
               }
              
               for(int i=0;i<count;i++)
               {
                   val=0;
                   for(int j=0;j<names[i].length();j++)
               {
                   val+=(int)names[i].at(j);
               }
               if(maxAscii<val)
               {
                   maxAscii=val;
                   indx=i;
                   }
               }
              
               return names[indx];      
           }
   string minString(string names[],int count)
   {
   int minAscii=0,val;
               int indx=0;
               for(int i=0;i<names[0].length();i++)
               {
                   minAscii+=(int)names[0].at(i);
               }
              
               for(int i=0;i<count;i++)
               {
                   val=0;
                   for(int j=0;j<names[i].length();j++)
               {
                   val+=(int)names[i].at(j);
               }
               if(minAscii>val)
               {
                   minAscii=val;
                   indx=i;
                   }
               }
              
               return names[indx];      
   }
   void converToUpperCase(string names[],int count)
   {
       string upper="";
       for(int i=0;i<count;i++)
       {
           upper="";
           for(int j=0;j<names[i].length();j++)
           {
               if(islower(names[i].at(j)))
               {
                   upper+=(names[i].at(j))-32;
               }
               else
               {
                   upper+=(names[i].at(j));
               }
           }
           names[i]=upper;
       }
      
   }

__________________________

Output:

_______________Could you plz rate me well.Thank You


Related Solutions

#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include <string> #include "Classroom.h" using namespace std; Classroom myLib; void loadDataset(string fileName) { ifstream myFile; myFile.open(fileName.c_str()); } void continueMessage(string message) { cout << message << endl; cout << "Press Enter to continue.." << endl; cin.get(); } int main() { string fileName = "dataset.txt"; loadDataset(fileName); myLib.print(); continueMessage; string ID = "Janet Newman"; myLib.removeStudent (ID); continueMessage("Janet Newman has been removed."); //--------------------------------------------------------------------------- myLib.print(); continueMessage("All students are listed!"); return...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
#include<iostream> #include<string> #include<fstream> #include<vector> /*HW: Create two more functions, Modify the posted grade example that uses...
#include<iostream> #include<string> #include<fstream> #include<vector> /*HW: Create two more functions, Modify the posted grade example that uses dynamic arrays and file I/O to include the following: -Use vectors -read from values from the file and store them in the vector. Modify the function getGrades Extend the program to also include the following features: -A menu option that allows the user to add more students and there grades. Assume the number of quizzes stay the same based on the value read from...
*****MUST ONLY USE****** #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> Description The word bank system...
*****MUST ONLY USE****** #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT