i have just started learning C++,this is my first programming language.
but i was solving some question in array,but this one was a problem to me.
here it goes.
create array,in that array find sub arrays(subset),
in which there is a maximun number in increasing order.
for example array below
10 1 2 3 0 5 2 8 6 1 9 11 12 13.
the subsets in increasing order will be
1 2 3
0 5
2 8
1 9 11 12 13
so from above example,the longest is
1 9 11 12 13
how can i do this ???
please i will appreciate for help))

4
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by mrcniceguy

Here's a hint.

``````if(Orig[i] > Sub[j-1])
Then insert Orig[i] into Sub[j]``````

hey,i appreciate for ur response,but i would be better if u write the whole code,coz i`m totally new to this as i explained above))

@mrcniceguy : You are pretty old to this community 148 posts still asking for codes ??? SHOCKING !!!

those posts u saw,it was the time i were studying PHP,of which the DAniweb community helped alot.
Now i`ve started c++,Which seems to be harder than i aspected))

mrniceguy - you're going to have to put in some effort before anyone here will help you. Give it a try and then we can take a look and see where you've gone wrong.

below is what i tried to do,but the code is giving me strange results)))

``````#include <iostream>
#include<ctime>
using namespace std;
int main()
{
int n,i,i1,j,max,m,sum,k,u;
//array

cout<< " Enter the size of array : ";
cin>>n;
int a[n];

srand(time(0));
for(i=0;i<n;i++)
{
a[i]=rand()%20;
cout<<a[i]<<" ";

}
cout<<endl;
cout<<endl;
cout<<endl;
//-------------------------------------------------
m=0;//for counting how many sub arrays
max=0;//max number of sub arrays
int h=0;//for breaking the line when condition does does not satisfy
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((a[i]<=a[j]))
{
m=m+1;
cout<<a[i]<<" ";
}
else
{
if(m>max)
{

max=m;
m=0;
h=h+1;
if(h>0)
{
cout<<endl;
}

}else{
m=0;
if(h>0)
{
cout<<endl;
}

}

}

}
}
cout<<endl;
cout<<"the long sub array has : ";
cout<<max;
cout<<endl;

system("pause");
return 0;
}``````

Mistakes :

1> srand function is defined in cstdlib header and you haven't used it.
2> Your for looping is hopelessly gibberish (not making fun of your coding skills just an honest opinion about the loop structure ;))

Here is a better structured for loop :

``````// Initialize the necessary variables first.
for(i=0;i<n-1;) // i should run only till last but 1 element and you don't know how much to increment i
{
x=i;y=i+1;m=1; // Temporary variables x and y to hold values m set to 1
while(a[x]<=a[y] && y<n) // This loop finds out the length of the next sub array
{
m=m+1;
x=x+1;
y=y+1;
}
if(m>max) max=m;//If the length found > max then this value is maximum
if(m>1) // Print sub array only if length > 1
{
// Printing the sub array where starting index is i and ending index is y
for(j=i;j<=x;j++) cout<<a[j]<<" ";
cout<<endl<<endl;
}
i=i+m; // Increment i with the sub array length
// Above step reduces the looping and increases efficiency and also it prevents
// the printing and finding of sub array of sub arrays
// That is in a sequence 1 2 3 4 5 it prints 1 2 3 4 5 and quits and not
// 1
// 1 2
// 1 2 3 and all upto 1 2 3 4 5
// Because even though they satisfy the condition they are not really required.
}``````

Now I suppose you have come to know what we expect from you,Its just your effort and then you'll find loads of help . :)

Thankx so much,everything is working as i wanted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.