Modifying a program for adjacent triplets in C

I have this program that calculates adjacent pairs. My question is how can I modify it to calculate adjacent triplets?

//Include statements.

#include <cstdlib>
#include <iostream>
#include <math.h>

//Standard namespace.
using namespace std;

void input (int array[20]); //Used when user inputs the numbers.
void calculate(int array[20], int *pairs); //Used to calculate the matches.
void output(int *pairs); //Used to output the number of pairs.

int main(void)
{
int array[20];
char quit;

start:
int pairs = 0;
input(array);
calculate(array,&pairs);
output(&pairs);

//Ask the user if they want to exit

printf("\nWould you like to continue testing my project, or exit?");
printf("\nTo exit input: 'N' or 'n'. To continue testing input anything else.");

//store their input in variable: exit

scanf("%s",&quit);

//If they want to exit...

if (quit == 'N' || quit == 'n')
{
//exit,
exit;
}
//otherwise,
else
{
//clear the screen
system("cls");
//and go back to the start.
goto start;
}
}

void input(int array[20])
{
int count = 0;
for (count;count<20;count++)
{
printf("Enter values . . . \n");
scanf("%i", &array[count]);
}
}

void calculate(int array[20], int *pairs)
{
int counter = 0;
for (counter;counter<19;counter++)
{
if (array[counter] == array[counter+1])
*pairs+=1;
}
}

void output(int *pairs)
{
printf("Number of pairs: [%i]\n", *pairs);
}
-Look at item 1 in the array
-Compare to item 2 in the array, if they are the same
-Compare to item 3 in the array, if they are the same, that's a triplet.

-Look at item 2 in the array,
-Compare to item 3 in the array, if they are the same
-Compare to item 4 in the array, if they are the same, that's a triplet

etc...

Also, you should not for any reason be using goto in this program. I strongly suggest removing the goto call. You should be using control loops (do...while, while, for etc...) and functions to control program flow.
Can I compare all three items in a single if statement?
Yes, using the logical and operator which is &&.

1
2
3
4
//Example usage of logical and ( && )
if(array[0] == 1 && array[1] == 1 && array[2] == 1)
{
}


1
2
3
4
5
int cmp_3_elements(int *iptr)
{
  if ((iptr[0] == iptr[1]) && (iptr[1] == iptr[2])) return 1;
  return 0;
}
Okay I did it like this and it works
1
2
 if (array[counter] == array[counter+1]  && array[counter] == array[counter+2])
*trip+=1;


Thanks for the help
Now I removed the Goto call in the program and I'm having problems going from one function to the next

I got it to go from main to the input function but when I try to go from input to calculate I get an error

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdlib>
#include <iostream>

using namespace std;

void input (int array[31]);
void calculate (int array[31], int *trip);
void output (int *trip);

int main(void)
    {
   
    int array[31];
    input(array);
      
}

void input(int array[31])
{
    printf("Please enter 30 numbers \n");
    
               int count = 0;
               for (count;count<30;count++)
                   {
                        scanf("%i", &array[count]);
            
                   }  
                   calculate(array, &trip);      
}

void calculate(int array[31], int *trip)
{
     
         int counter = 0;
         for (counter;counter<30;counter++)
             {
                 if (array[counter] == array[counter+1]  && array[counter] == array[counter+2])
                  *trip+=1;
             }
             output(&trip);
}
                 
void output(int *trip)
{
         
     printf("Number or triplets: [%i]\n", *trip);
}


It worked on line 14 but not line 28 and 40
when I try to go from input to calculate I get an error

Thankyou for not telling us what the error is. This would be so much less fun if we didn't have the extra thrill of playing guessing games.

In line 28, where is trip defined? I don't see any definition for a variable of that name within the scope of the input() function.
Firstly, when you describe your problems, can you give as much detail as possible? Saying "it doesn't work" isn't very helpful. Saying "it doesn't work and x happens instead" or "it doesn't work and my compiler gives whatever error" is very much more helpful. ;) So why exactly is it not working or what is or is not happening?

I'm also confused as to why you are using <iostream> but not using std::cout and std::cin ? Either do C++ OR C, don't mix them.

Sorry about that the error I'm getting is

1
2
3
4
5
 In function `void input(int*)': 
line 28  `trip' undeclared (first use this function) 
  (Each undeclared identifier is reported only once for each function it appears in.) 
 In function `void calculate(int*, int*)': 
line 40  cannot convert `int**' to `int*' for argument `1' to `void output(int*)' 
Well, the first of those errors is self-explanatory. As I've already pointed out, trip is undefined in your input function. Did you mean to pass it in as an argument?

At line 40, you're attempting to pass the address of trip into output. But you've already defined trip as being a pointer to an int, i.e. an int* (see line 31), so the address of it is a pointer to a pointer to an int, i.e. an int**.

There's no need to pass the address of trip here - just pass trip itself, because it's already a pointer.
Okay thanks for the help I figured it out.
Topic archived. No new replies allowed.