Question

hello, I'm having trouble understanding the problems below for my Computer science class, please include a...

hello, I'm having trouble understanding the problems below for my Computer science class, please include a work-through and some explanations on how to do it so that I can understand remember how do problems like this on my own.

Thank you very much

WE ARE USING C++

The following project includes the frequency_start_letter function and a few other functions that work with a string array.

Reminder: you could click "Open in Repl.it" on the top right corner to see the project in full screen.

#include <iostream>

#include "library.h"

using namespace std;

int main() {

  string words[100];  //to store up to 100 words

  int num_words = 0;  //to track the number of words in the words array

  //collect and store words into the words arrray.

  //num_words is updated to reflect the words stored

  collect_data(words, num_words, "words.txt");

  cout << "After collection:" << endl;

  print(words, num_words, 5);   //5 indicates the number of value per row

  cout << "Number of words starting with a:\t";

  cout << frequency_start_letter(words, num_words, 'a') << endl;

  string str;

  bool found = false;

  cout << "What word would you like to remove? ";

  cin >> str;

checkpoint

The above Repl project contains the following prototype in the "library.h" file

int count(const string arr[], int size, int len);

The implementation for the above function in the "library.cpp" file simply returns 0. Improve the function implementation to count the number of elements in the array whose length is the same as specified by the len parameter.

Paste your tested function implementation for the count function into the following box.

The above Repl project contains the following prototype in the "library.h" file

bool add(string arr[], int& size, string element);

The implementation for the above function in the "library.cpp" file simply returns false. Improve the function implementation to add the element into the array while keeping it sorted.

Paste your tested function implementation for the add function into the following box.

Paste the function implementation into the following box.

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

#include <string>

#include <iostream>

using namespace std;

#define ARRAY_SIZE 100

// To count number of words of array having length len and current size = size

// We are putting const before arr[] to make sure that, this function is not modifying the arrray

// Other variables like size and len are called by value, so these changes won't be reflected in main()

int count(const string arr[], int size, int len) {

int count = 0; // Initialize count

for(int i=0; i<size; i++) { // Iterate through array

if(arr[i].size() == len) // To get length of string, use built in function size() of class string. If lengths are matching

count += 1; // increment count

}

return count;

}

// Add element to sorted array arr[] with current size

bool add(string arr[], int& size, string element) {

if((size + 1) >= ARRAY_SIZE) { // Check if we can add to array. i.e. after adding element, new size should be less than total array size

return false; // If new size > total size, then return false, since we can't add to array

}

int left=0 ,right=size-1, pos; // We are doing binary search to find the position to insert the new element. Initialize left=0(first element) and right = size-1(last element)

while (left <= right) { // Loop until left <= right i.e. there is atleast one element in search array

int mid = left + (right - left) / 2; // compute mid. Mid will be leftmost element + current search array size/2. Here current search array

// will be having right - left elements.

if (arr[mid] == element) // Compare element against mid element of array

pos = mid; // If true, update position to insert the new element as mid

if (arr[mid] < element) { // If mid element of array is less than element means we have to do the search in right hand side

left = mid + 1; // we make left = mid + 1 (first element of search array becomes mid+1)

pos = left; // Update pos

}

else {

right = mid - 1; // If mid element was greater, then we have make the search array to the right of mid

pos = right; // Update pos

}

}

pos = pos > 0 ? pos : 0; // If pos is negative, make position to 0. We have to insert into position pos

cout <<"Position to insert is: " << pos << endl;

for(int i = size; i > pos; i--) { // Move all elements after position pos to next position so that we can insert the element

arr[i] = arr[i-1]; // From the last element of array we will shift the element until the position reaches pos

}

arr[pos] = element; // Add new element to position pos in array

size += 1; // Update size. Since call by reference, this value will updated in main() also

return true; // Return true, as we have updated the array.

}

void print_array(const string arr[], int size) { // Display array with size.

for (int i=0; i<size; i++) {

cout << arr[i] << " ";

}

cout << endl;

}

int main() {

int len = 2, c, size=0; // Declare variables

string words [ARRAY_SIZE]; // Declare array with size equals ARRAY_SIZE

// Initialize the array with test data here. Data should be sorted. We are not reading data as mentioned in the question. But this suffices to show the correctness

// of required functions

words[size++] = "abcd"; words[size++] = "befg"; words[size++] = "cest"; words[size++] = "dbc"; words[size++] = "def"; words[size++] = "run"; words[size++] = "to";

c = count(words, size, len); // Find numbers of words in array with length "len" having size no. of strings in it.

cout << "Number of strings with length '" << len << "' : " << c << endl; // Display it

add(words, size, "zen"); // Add test words to array words.

print_array(words, size); // print array after insertion.

add(words, size, "ab");

print_array(words, size);

add(words, size, "dbc");

print_array(words, size);

}

Provided explanation in comments in code.

KINDLY REVIEW

Add a comment
Know the answer?
Add Answer to:
hello, I'm having trouble understanding the problems below for my Computer science class, please include a...
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
  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • 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...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

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
ADVERTISEMENT