I got an assignment to be done..

Given a sequence of positive integers. You need to find the number of triples in that sequence. For this problem, (x, y, z) constructs a triple if and only if x + y = z. So, (1, 2, 3) is a triple, where (3, 4, 5) is not.

Input
Each input set starts with a positive integer N. Next few lines contain N positive integers. Input is terminated by EOF.

Output
For each case, print the number of triples in a line.

Constraint
3 <= N <= 5000

Sample Input........................Sample Output
1 2 3 4 5 6......................TRIPLE is equal to 3.

1 2 4 8 16 32....................TRIPLE is equal to 0.

100000000 200000000 100000000....TRIPLE is equal to

1 1 1 2 2........................TRIPLE is equal to 6.


---------------------------------------------------------------------------------------
And i've tried to do this by my own,but until now,this is the farthest that i could figure out :

#include <iostream>
#include <string>

using namespace std;

int main() {

int integers;
int integer;
int x,y,z;
int triple_first=0;
int triple=0;

cout<<"How many integers? (3 to 5000) : ";
cin>>integers;

while(integers<3 || integers>5000){
    cout<<"How many integers? (3 to 5000) : ";
    cin>>integers;
    }

cout<<"Enter the integers: ";

for(int i=1; i<=integers; i++){

    cin>>x;
    if((..codes...))
    {
    
        ..........codes......

              }
    }

cout<<"Triple is equal to "<<triple;

}

I don't know weather it is right or not,but i think maybe it is more likely to be that way..

Please help..TQ :)

Recommended Answers

All 11 Replies

Ok i didn't really understood what is triple. Can you give me some output in my sequences? So i will understand what is that :)
1 2 3 6 9
1 2 3 5 8 9
9 10 19 29

write the pseudocode to accept three numbers and display the largest number.

i want this question ans plz give me the answer

Ok2...the three numbers is considered as triple when you add any of those two numbers and the answer is equal to the other one...for example :

the user entered this three numbers = 1 1 2

so ((the first number)+(second number)=(third number))
1+1=2,thus those three numbers is a triple..

say f we got the other sequence,let say = 1 1 2 3

thus, (1+1=2)= first triple
and (1+2=3)= second triple

thus,this sequence got 2 triple in total..

do you understand it now?

hope so..and please help :)

Ok i didn't really understood what is triple. Can you give me some output in my sequences? So i will understand what is that :)
1 2 3 6 9
1 2 3 5 8 9
9 10 19 29

this the output for your sequences :

1 2 3 6 9 .......triple is equal to 4
1 2 3 5 8 9......triple is equal to 6
9 10 19 29.......triple is equal to 2

this is so far that i could get now..
but it is still not complete..

Please help me to complete this..TQ :)

#include <iostream>
#include <string>

using namespace std;

int main() {

int integers;
int integer;
int x,y,z;
int triple_first=0;
int triple=0;

cout<<"How many integers? (3 to 5000) : ";
cin>>integers;

while(integers<3 || integers>5000){
    cout<<"How many integers? (3 to 5000) : ";
    cin>>integers;
    }

integers=integers/3;

cout<<"Enter the integers: ";

for(int i=1; i<=integers; i++){

    cin>>x>>y>>z;
    if((x+y==z|| x+z==y || y+z==x))
    {
        triple=triple+1;
              }
    }

cout<<"Triple is equal to "<<triple;

}

could someone help me out???
the deadline of this assignment is today...

please..

Hi,
Your description of the problem is still ambiguous to me.
Some of the answers you posted seem inconsistent.

Give a more detailed explanation as to what constitutes a triple. In some of your answers it seems spaces between X and Y are allowed, and in others not. Do X and Y necessarily have to be right next to each other? I assume Z does not have to be directly after Y judging by the majority of your answers. In some of your answers it seems X and Y can be in any order (i.e. 1 2 3 = two triples, namely, 123, and 213), but in some of your other answers that is not allowed.

Hi,
Your description of the problem is still ambiguous to me.
Some of the answers you posted seem inconsistent.

Give a more detailed explanation as to what constitutes a triple. In some of your answers it seems spaces between X and Y are allowed, and in others not. Do X and Y necessarily have to be right next to each other? I assume Z does not have to be directly after Y judging by the majority of your answers. In some of your answers it seems X and Y can be in any order (i.e. 1 2 3 = two triples, namely, 123, and 213), but in some of your other answers that is not allowed.

i really dont know the other way of explaining this..

i'll try to explain this to you one more time as best as i can..

This program will first ask the user ,how many integers he/she want to enter?(the lowest is three and the most is five thousand)
so for example the user want to enter 3 integers..

Thus,this program will allow him/her to enter the number of integers that he/she wanted according to what he/she told this program just now(which is 3)

Then,for example the user entered the number of 1,2 and 3..

thus the program will identify one of each number and will try to add them up,whether or not the
(first number+second number=third number) or
(third number+second number=first number) or
(third number+first number=second number) or
any other possibility of addition between those numbers that the user has entered..

The program will determine whether or not each addition/condition is true,and each time the addition condition is correct,count triple as one.If the other condition is correct,count triple as two,and so on until there is no more addition/condition available according to the integers/numbers that the user entered.
Thus,for the input of 1,2 and 3 :

(first number+second number=third number)=TRUE......now triple is equal to 1
(third number+second number=first number)=FALSE.....now triple is also equal to 1
(third number+first number=second number)FALSE......now triple is also equal to 1

Because only one available condition that is appear to be true,the TOTAL NUMBER OF TRIPLE is equal to 1.


To make this clear, i will provide you with the other example.
Let say right now,the user want to enter 4 integers/numbers.And that user entered the number of 1,2,3 and 4.

So first this program will find all possibilities of additions/conditions available from the numbers that the user has entered and determine weather or not each condition is true :

(first number+second number=third number)=TRUE.................now triple is 1
(first number+second number=fourth number)=FALSE...............now triple is 1
(first number+third number=fourth number)=TRUE.................now triple is 2
(second number+third number=first number)=FALSE................now triple is 2
(second number+third number=fourth number)=FALSE...............now triple is 2
(second number+fourth number=first number)=FALSE...............now triple is 2
(first number+second number+third number=fourth number)=FALSE..now triple is 2
(first number+second number+fourth number=third number)=FALSE..now triple is 2
(fourth number+third number+second number=first number)=FALSE..now triple is 2
(fourth number+third number+first number=second number)=FALSE..now triple is 2

..i dont know if there is any other condition,but this is all that i could figure out..
So let say above is all the addition or condition that are available for the input of
(1,2,3 and 4).Since there are only two conditions that are TRUE,thus the TOTAL NUMBER OF TRIPLE IS EQUAL TO 2.


I think this is the best explanation that i could give you...but if it is still not clear for you,be free to ask me again..

I really hope someone could help me out..Thank you..

I got an assignment to be done..

Given a sequence of positive integers. You need to find the number of triples in that sequence. For this problem, (x, y, z) constructs a triple if and only if x + y = z. So, (1, 2, 3) is a triple, where (3, 4, 5) is not.

Input
Each input set starts with a positive integer N. Next few lines contain N positive integers. Input is terminated by EOF.

Output
For each case, print the number of triples in a line.

Constraint
3 <= N <= 5000

Sample Input........................Sample Output
1 2 3 4 5 6......................TRIPLE is equal to 3.

1 2 4 8 16 32....................TRIPLE is equal to 0.

100000000 200000000 100000000....TRIPLE is equal to

1 1 1 2 2........................TRIPLE is equal to 6.


---------------------------------------------------------------------------------------
And i've tried to do this by my own,but until now,this is the farthest that i could figure out :

#include <iostream>
#include <string>

using namespace std;

int main() {

int integers;
int integer;
int x,y,z;
int triple_first=0;
int triple=0;

cout<<"How many integers? (3 to 5000) : ";
cin>>integers;

while(integers<3 || integers>5000){
    cout<<"How many integers? (3 to 5000) : ";
    cin>>integers;
    }

cout<<"Enter the integers: ";

for(int i=1; i<=integers; i++){

    cin>>x;
    if((..codes...))
    {
    
        ..........codes......

              }
    }

cout<<"Triple is equal to "<<triple;

}

I don't know weather it is right or not,but i think maybe it is more likely to be that way..

Please help..TQ :)

Here's a working skeleton version. May want to test it though.

#include <iostream>
using namespace std;


int main(){

cout << "How many integers will you be entering? [3-5000]" << endl;

int num_ints;

cin >> num_ints;

int* int_array;
int_array = new int[num_ints];

cout << "Enter the numbers now." << endl;


for (int i = 0; i < num_ints; ++i){
        cin >> int_array[i];
        }

int num_triples = 0;

int x_position = 0;

while (x_position < num_ints - 2) {

        for (int y_position = x_position + 1; y_position < num_ints - 1; ++y_position){
                for (int z_position = y_position + 1; z_position < num_ints; ++z_position){
                        if (int_array[x_position] + int_array[y_position] == int_array[z_position]){
                                num_triples++;
                        }
                }
        }
        x_position++;

}

cout << "Number of triples is: " << num_triples << endl;

return 0;
}

If you're confused by the nested for loops, here's how it works:
X position starts at the beginning.
Y position starts always 1 place past where X currently is.
Z position starts always 1 place past where Y currently is.

Then, so in the inner most loop, where Z_position++, we are checking if the same X and Y is equal to ANY Z in the list.
so if you have 1 2 3 4 5 6 7
We first check 1+2=3? 1+2=4? 1+2=5? 1+2=6? 1+2=7?
After we get to the end of the list for the possible Z's, we increase Y by 1 and do the same thing.
So we check 1+3=4? 1+3=5? 1+3=6? 1+3=7?
And thus we do that until Y gets to 6. (of course you dont want it to get to 7, because then nothing is after that and Z will go past the end of the array).
After we get through all the Y's, we finally increase X, and repeat the whole process from the beginning, except that now that we start with X = 2.

Actually I don't know why I started with a while loop and then switched over to for loops... Oh wait I know, it's 5 am.

You can get rid of that while loop by just doing this instead:

for (int x_position....
for (int y_position...
for (int z_position...

-Greywolf

Here's a working skeleton version. May want to test it though.

#include <iostream>
using namespace std;


int main(){

cout << "How many integers will you be entering? [3-5000]" << endl;

int num_ints;

cin >> num_ints;

int* int_array;
int_array = new int[num_ints];

cout << "Enter the numbers now." << endl;


for (int i = 0; i < num_ints; ++i){
        cin >> int_array[i];
        }

int num_triples = 0;

int x_position = 0;

while (x_position < num_ints - 2) {

        for (int y_position = x_position + 1; y_position < num_ints - 1; ++y_position){
                for (int z_position = y_position + 1; z_position < num_ints; ++z_position){
                        if (int_array[x_position] + int_array[y_position] == int_array[z_position]){
                                num_triples++;
                        }
                }
        }
        x_position++;

}

cout << "Number of triples is: " << num_triples << endl;

return 0;
}

If you're confused by the nested for loops, here's how it works:
X position starts at the beginning.
Y position starts always 1 place past where X currently is.
Z position starts always 1 place past where Y currently is.

Then, so in the inner most loop, where Z_position++, we are checking if the same X and Y is equal to ANY Z in the list.
so if you have 1 2 3 4 5 6 7
We first check 1+2=3? 1+2=4? 1+2=5? 1+2=6? 1+2=7?
After we get to the end of the list for the possible Z's, we increase Y by 1 and do the same thing.
So we check 1+3=4? 1+3=5? 1+3=6? 1+3=7?
And thus we do that until Y gets to 6. (of course you dont want it to get to 7, because then nothing is after that and Z will go past the end of the array).
After we get through all the Y's, we finally increase X, and repeat the whole process from the beginning, except that now that we start with X = 2.

Actually I don't know why I started with a while loop and then switched over to for loops... Oh wait I know, it's 5 am.

You can get rid of that while loop by just doing this instead:

for (int x_position....
for (int y_position...
for (int z_position...

-Greywolf

huh..thats amazing...but actually i cannot use the array in this assignment,but at least now i got the answer..thats a relief..
Em, Thank You for explain it so well too :)

Thank You...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.