| | |
help please))
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 217
Reputation:
Solved Threads: 4
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))
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))
•
•
Join Date: Jan 2009
Posts: 79
Reputation:
Solved Threads: 12
Here's a hint.
C++ Syntax (Toggle Plain Text)
if(Orig[i] > Sub[j-1]) Then insert Orig[i] into Sub[j]
•
•
Join Date: Mar 2008
Posts: 217
Reputation:
Solved Threads: 4
below is what i tried to do,but the code is giving me strange results)))
C++ Syntax (Toggle Plain Text)
#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 :
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 .
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 :
c++ Syntax (Toggle Plain Text)
// 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 .
Last edited by csurfer; Jun 11th, 2009 at 11:45 am.
I Surf in "C"....
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help needed in appending the file from beginning
- Next Thread: Return pointer
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





