Write your question here.
How can I compare these two strings and count when the strings are the same? It prints 10 all the time
[]
Put the code you need help with here.
[
#include <iostream>
#include <string>
#include <cmath>
//#include <cstdlib>
using namespace std;
void readtosentinel (string a[],int capacity,
int &size, string sentinel);
int occur (string myArray[], int mass, string array[], int size );
/* Reads doubles into the array a. Ends if the sentinel
value is read, or if the capacity of the array is reached.
Size is set to the number of elements read.
*/
int main () {
int MAX_SIZE=1000;
string text[MAX_SIZE];
int count = 0;
int NUMTERMS = 10;
string sentinel = "XXX";
string terms[NUMTERMS] = {"array", "compute", "declare", "define", "for", "function", "if", "loop", "variable", "while" };
cout << "Please enter up to " << MAX_SIZE;
cout<< " words, terminated with " << sentinel << ":";
readtosentinel (text, MAX_SIZE, count, sentinel);
int counter[NUMTERMS];
for (int i = 0; i < NUMTERMS; i++){
cout << terms[i];
counter[i] = occur(text, count, terms, NUMTERMS);
// counter[i] = value;
cout<<counter[i]<< endl;
}
return 0;
}
void readtosentinel (string a[],int capacity, int &size, string sentinel){
1E3 is concerned with loop array function variable and so on
the first exam question will test loop
the next two will test function the last one array
array is a set of variables of same type
use for loop to process array
array function takes in array and size
function can return a value or function can print or update array element
variable stores value of size of array
loop allows you to do same thing many times
while loop for going until you notice you reach the end
for loop for doing something a predictable number of times
loop control variable often i is used to control the for loop
if statement allows branching one for true and one for false
boolean conditions are used in if statement while loop
bool variable often called a flag
up till now we have used array to deal with problems of known size
Often we want to use array but we dont know in advance how many students employees words there will be
XXX
Thanks for the reply,
the occur() function counts how many times the hard-coded words from terms array appears in text array and returns it. I understand the simplification code, however the entire codes from main down does not count the actual number of the individual strings.
this is the terminal view :
array10
compute10
declare10
define10
for10
function10
if10
loop10
variable10
while10
but in reality, each of those words do not appear 10 times in the text. Only "array" appears 10 times
times the hard-coded words from terms array appears in text and returns it
it. One number. Sum of all individual occurrences.
Hardcoding is restrictive.
Consider a function that counts how many times one word occurs in array text. Just one word.
(The C++ Standard Library already has such function, but it is better for learning if you can come up with your version.)
What does such function need as arguments?
* The array
* Size of array
* The word
Now that we look at the function we see the return statement on line 11. That renders some lines inaccessible.
Here is a bit shorter version that does exactly the same as yours:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int occur( string myArray[], int mass, string array[], int size )
{
int count = 0;
if ( 0 < size )
{
for (int i = 0; i<mass; i++)
{
if (array[0] == myArray[i])
count++;
}
return count;
}
// what do we return here? Compiler should warn about this.
}
That should make it clear, why you do get count for "array".
Thank you for your replies. I have figured out the source code, apparently c++ cannot compare two different arrays simultaneously. One must check the values individually.
void readtosentinel (string a[],int capacity,
int &size, string sentinel);
int occur (string word, string array[], int mass );
/* Reads doubles into the array a. Ends if the sentinel
value is read, or if the capacity of the array is reached.
Size is set to the number of elements read.
*/
const int MAX_SIZE = 1000;
const int NUMTERMS = 10;
int main () {
string text[MAX_SIZE];
int count = 0;
string sentinel = "XXX";
string terms[NUMTERMS] = {"array", "compute", "declare", "define", "for", "function", "if", "loop", "variable", "while" };
cout << "Please enter up to " << MAX_SIZE;
cout<< " words, terminated with " << sentinel << ":";
readtosentinel (text, MAX_SIZE, count, sentinel);
int counter[NUMTERMS];
int value;
for (int i = 0; i < NUMTERMS; i++)
{
value = occur(terms[i], text, MAX_SIZE);
counter[i] = value;