Arrays problem

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

Join Date: Mar 2006
Posts: 12
Reputation: dors_zone is an unknown quantity at this point 
Solved Threads: 0
dors_zone dors_zone is offline Offline
Newbie Poster

Arrays problem

 
0
  #1
Mar 22nd, 2006
here are the question,

a) Write a function named OutOfOrder that takes as input parameters an array of doubles named Arr and an int parameter named size and returns a value of type int. The return value is -1 if the array is in order, meaning that the first element in the array is less than or equal to the second element in the array, the second element in the array is less than or equal to the third element in the array and so on. If the array is not in order, the return value is the index of the first element in the array that is out of order.


int i,j,n;
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if (c[i]<=c[i+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
else continue;
}

-----

am i correct? plz guide me...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays problem

 
0
  #2
Mar 22nd, 2006
What you have posted looks like an ill fated attempt at a bubble sort. It bears some relation to the problem assigned, but not a whole heck of a lot. The relation to your assignment and the code you posted is that the body of the function you are supposed to write can be done with a single for loop and containing a comparison, an example of which can be found in the code you posted. With that and a way to detect where, if at all, the values are out of place (a break statement in the body of the conditional, and a second comparison outside the loop should do that), you should be set. Don't try to write code until you can write out the solution to the problem step by step in your native language. Then try to convert that into code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 12
Reputation: dors_zone is an unknown quantity at this point 
Solved Threads: 0
dors_zone dors_zone is offline Offline
Newbie Poster

Re: Arrays problem

 
0
  #3
Mar 22nd, 2006
Originally Posted by Lerner
What you have posted looks like an ill fated attempt at a bubble sort. It bears some relation to the problem assigned, but not a whole heck of a lot. The relation to your assignment and the code you posted is that the body of the function you are supposed to write can be done with a single for loop and containing a comparison, an example of which can be found in the code you posted. With that and a way to detect where, if at all, the values are out of place (a break statement in the body of the conditional, and a second comparison outside the loop should do that), you should be set. Don't try to write code until you can write out the solution to the problem step by step in your native language. Then try to convert that into code.
i refer from the books but i still don't understand...could u plz tell me more...did the code is incorrect?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays problem

 
0
  #4
Mar 22nd, 2006
Yes the code you posted is incorrect. Follow my post to get to a solution.

Modifying the code you posted:
  1. for(j = 0; j < n - 1; j++)
  2. {
  3. if(c[j + 1] < c[j])
this is about all I would use from what you have posted. Do as I suggested and write it out in your native language before you try to write it using C/C++.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 12
Reputation: dors_zone is an unknown quantity at this point 
Solved Threads: 0
dors_zone dors_zone is offline Offline
Newbie Poster

Re: Arrays problem

 
0
  #5
Mar 22nd, 2006
it is be like this ? hmmm more difficult


int i,j,n;
for(i=1;i<=n-1;i++)
{

for(j = 0; j < n - 1; j++)
{
if(c[j + 1] < c[j])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
else continue;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays problem

 
0
  #6
Mar 22nd, 2006
the function could look sort of like this:
  1. typeOfReturnValue nameOfFunction(parameterType parameterName, secondParameterType secondParameterName)
  2. //start function
  3. declare int called j
  4. declare variableToReturn
  5.  
  6. //use a single loop starting here
  7. loop through array using index called j which ranges from 0 to array size minus 2
  8. each time through the loop increment j by 1
  9. //compare value of element at index j + 1 with value of element at index j
  10. if element at j + 1 is less than element at j
  11. break out of loop since element at j + 1 is out of place
  12. //loop ends here
  13.  
  14. //now that loop is done determine value of j
  15. if j is index of last element in array
  16. then array is in order so assign appropriate value to variableToReturn
  17. else //the array is not in order
  18. so assign the first index of the array which is not in order to variableToReturn
  19.  
  20. return variableToReturn
  21. //end function
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC