943,726 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 375
  • C++ RSS
Jun 11th, 2009
0

help please))

Expand Post »
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))
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Jun 11th, 2009
0

Re: help please))

Here's a hint.

C++ Syntax (Toggle Plain Text)
  1. if(Orig[i] > Sub[j-1])
  2. Then insert Orig[i] into Sub[j]
Reputation Points: 21
Solved Threads: 12
Junior Poster in Training
MatEpp is offline Offline
79 posts
since Jan 2009
Jun 11th, 2009
0

Re: help please))

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))
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Jun 11th, 2009
0

Re: help please))

@mrcniceguy : You are pretty old to this community 148 posts still asking for codes ??? SHOCKING !!!
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jun 11th, 2009
0

Re: help please))

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))
Last edited by mrcniceguy; Jun 11th, 2009 at 8:05 am.
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Jun 11th, 2009
0

Re: help please))

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.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jun 11th, 2009
0

Re: help please))

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


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. int n,i,i1,j,max,m,sum,k,u;
  7. //array
  8.  
  9. cout<< " Enter the size of array : ";
  10. cin>>n;
  11. int a[n];
  12.  
  13. srand(time(0));
  14. for(i=0;i<n;i++)
  15. {
  16. a[i]=rand()%20;
  17. cout<<a[i]<<" ";
  18.  
  19. }
  20. cout<<endl;
  21. cout<<endl;
  22. cout<<endl;
  23. //-------------------------------------------------
  24. m=0;//for counting how many sub arrays
  25. max=0;//max number of sub arrays
  26. int h=0;//for breaking the line when condition does does not satisfy
  27. for(i=0;i<n-1;i++)
  28. {
  29. for(j=i+1;j<n;j++)
  30. {
  31. if((a[i]<=a[j]))
  32. {
  33. m=m+1;
  34. cout<<a[i]<<" ";
  35. }
  36. else
  37. {
  38. if(m>max)
  39. {
  40.  
  41. max=m;
  42. m=0;
  43. h=h+1;
  44. if(h>0)
  45. {
  46. cout<<endl;
  47. }
  48.  
  49. }else{
  50. m=0;
  51. if(h>0)
  52. {
  53. cout<<endl;
  54. }
  55.  
  56. }
  57.  
  58. }
  59.  
  60. }
  61. }
  62. cout<<endl;
  63. cout<<"the long sub array has : ";
  64. cout<<max;
  65. cout<<endl;
  66.  
  67.  
  68. system("pause");
  69. return 0;
  70. }
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Jun 11th, 2009
0

Re: help please))

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 :
c++ Syntax (Toggle Plain Text)
  1. // Initialize the necessary variables first.
  2. 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
  3. {
  4. x=i;y=i+1;m=1; // Temporary variables x and y to hold values m set to 1
  5. while(a[x]<=a[y] && y<n) // This loop finds out the length of the next sub array
  6. {
  7. m=m+1;
  8. x=x+1;
  9. y=y+1;
  10. }
  11. if(m>max) max=m;//If the length found > max then this value is maximum
  12. if(m>1) // Print sub array only if length > 1
  13. {
  14. // Printing the sub array where starting index is i and ending index is y
  15. for(j=i;j<=x;j++) cout<<a[j]<<" ";
  16. cout<<endl<<endl;
  17. }
  18. i=i+m; // Increment i with the sub array length
  19. // Above step reduces the looping and increases efficiency and also it prevents
  20. // the printing and finding of sub array of sub arrays
  21. // That is in a sequence 1 2 3 4 5 it prints 1 2 3 4 5 and quits and not
  22. // 1
  23. // 1 2
  24. // 1 2 3 and all upto 1 2 3 4 5
  25. // Because even though they satisfy the condition they are not really required.
  26. }

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jun 11th, 2009
0

Re: help please))

Thankx so much,everything is working as i wanted.
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help needed in appending the file from beginning
Next Thread in C++ Forum Timeline: Return pointer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC