help please))

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

help please))

 
0
  #1
Jun 11th, 2009
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))
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 79
Reputation: MatEpp is an unknown quantity at this point 
Solved Threads: 12
MatEpp MatEpp is offline Offline
Junior Poster in Training

Re: help please))

 
0
  #2
Jun 11th, 2009
Here's a hint.

  1. if(Orig[i] > Sub[j-1])
  2. Then insert Orig[i] into Sub[j]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Re: help please))

 
0
  #3
Jun 11th, 2009
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))
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: help please))

 
0
  #4
Jun 11th, 2009
@mrcniceguy : You are pretty old to this community 148 posts still asking for codes ??? SHOCKING !!!
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Re: help please))

 
0
  #5
Jun 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: help please))

 
0
  #6
Jun 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Re: help please))

 
0
  #7
Jun 11th, 2009
below is what i tried to do,but the code is giving me strange results)))


  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: help please))

 
0
  #8
Jun 11th, 2009
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 :
  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.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 217
Reputation: mrcniceguy is an unknown quantity at this point 
Solved Threads: 4
mrcniceguy mrcniceguy is offline Offline
Posting Whiz in Training

Re: help please))

 
0
  #9
Jun 11th, 2009
Thankx so much,everything is working as i wanted.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC