I need help on two c++ homework problems that are due my midnight tonight. My fi
ID: 3533232 • Letter: I
Question
I need help on two c++ homework problems that are due my midnight tonight.
My first problem is the write a function that returns the number of words in a string. For example, the string "Mary had a little lamb" would return 5. I have a start but I have no idea how to check each position of the string and be able to distuingish whether or not a new word has started in the string. As you can see, I have a while loop and an if statement that are blank because I don't know how to check each position in the string(assuming you do that with the while loop) and then to check when the next word has begun(with the if statement) and then increment the word count. Any help would be greatly appreciated.
// P5.9
#include <iostream>
#include <string>
using namespace std;
int wordcount(string str);
int main()
{
string str;
cout<<"Insert a string: "<<endl;
cin>>str;
cout<<"There are "<<wordcount(str)<<" words in your string"<<endl;
}
int wordcount(string str)
{
int word_count=0;
int i=0;
while()
{
if()
{
word_count++;
}
i++;
}
return word_count;
}
The second problem is to generated a sequence of 20 random numbers between 0 and 99, print those number, then sort the numbers and finally print the sorted sequence. I must use the sort function from the standard c++ library. As you can see below, I have generated my random numbers but I have not done anything to sort them. I am confused as to how the sort function works. My only experience with programming is MATLAB which will sort something just by typing in 'sort(array)'. Obviously c++ does not work that way and I am really confused as to where to begin. Any help would be greatly appreciated.
// P6.6
#include <iostream>
using namespace std;
int main()
{
int numbers[20];
int sort_numbers[20];
for(int i=0; i<20; i++)
{
numbers[i] = rand() % 100;
cout<<numbers[i]<<endl;
}
system("pause");
}
Explanation / Answer
please rate - thanks cin only reads until a space must input using getline() // P5.9 #include #include using namespace std; int wordcount(string str); int main() { string str; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.