Counting 1 Word in Array C++

Hello,
This is my first post. I'm learning C++ right now, and I have an assignment with two programs. I'm lost like you would not believe.

First Program: Calculate values of a function z = sin(x) + cos(y), x and y change from 0, pi/6, pi/3 to pi and display in table format.

I understand how to put it together, but I cant get it to work... :(
This is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include "math.h"

using namespace std;

int main(int argc, char *argv[])
{

    int array[6][6];
    double x, y;
    double a, b;
    double pi = 3.14;
    
     for(int i = 0; i < 7; i++)
     {
      cout << i << "\t"; 
     }
     
     cout << "\n\n";

     for(int i = 0*pi/6; i < 7*pi/6; i++)
     {
      for(int j = 0*pi/6; j < 7*pi/6; j++)
      {
       array[i][j] = (sin(i/6)) + (cos(j/6));
       cout << array[i][j] << "\t";
      }
 
      cout << "\n";
     }



I am wayyyyy off, and I am confused how to get it in a table:

0 pi/6 2pi/6 3pi/6 4pi/6 5pi/6 pi
0
pi/6
2pi/6
3pi/6
4pi/6
5pi/6
pi


Next Problem:
Write a program to find how many times the word "the" occurs in an article. Here's what I have....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <iostream>
#include <string>
#include "ctype.h"

using namespace std;


int main(int argc, char *argv[])
{
    
     char article[1000] = "The proposal was the product of months of negotiations between the Gang of Six committee members: three Democrats and three Republicans. The other Republicans involved in the negotiations, Sens. Chuck Grassley of Iowa and Mike Enzi of Wyoming, voted no on the compromise measure.";
     cout << article << "\n\n";
     
  
    system("PAUSE");
    return EXIT_SUCCESS;
}


Im using Dev C++ as a compiler.

Its due tomorrow at 12:30pm, and I've spent the last week going through my c++ reference guides and online sites searching for help. I don't want a solution, I want an explanation of how it should be done that way I can learn. Thank You.
1.) Your for loop on line 22/24 won't work the way you want it to. You are trying to start i/j at 0 and increase them by pi/6 per loop. What you are doing is setting it to 0 and incrementing it by 1 until you get to a number > 7pi/6.

Also, I don't know why you are using an array at all, you don't need one.

This one is the one that needs the most work...try looking up for loops and outputting data again.

2.) I would put it into an std::string and use .find() to find the text you want.
I got the first problem solved. It wasn't the prettiest looking table, but the outcome worked perfect. I am still stuck on the second one. I did the .find() and string but it still did not work. I'll post what I did later today. If anyone has any idea where I can start off after what I did in my first post, thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>

int main()
{
    std:: string word = "the"
    std::string article= "The proposal was the product of months of negotiations between the Gang of Six committee members: three Democrats and three Republicans. The other Republicans involved in the negotiations, Sens. Chuck Grassley of Iowa and Mike Enzi of Wyoming, voted no on the compromise measure.";
    size_t found;
    int counter = 0;
    bool loop = true; // imaginative i know

    for(int i=0; i< strlen(article); i++) // "The" is not the same as "the"
         if(isupper(article[i]))
             tolower(article[i]);
    
    while(loop) {
       if(!counter)
           article.find(word)
       else
           article.find(word, found+3)
       if(found != string::npos) {
           counter++;
           loop = false;
       }
    }

// output whatever

//if this doesn't compile sorry! not at a copilable machine but hopefully you get the idea of what i'm doing! 

Last edited on
Topic archived. No new replies allowed.