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<=c[i+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
else continue;
}

-----

am i correct? plz guide me...

Recommended Answers

All 5 Replies

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.

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? :?:

Yes the code you posted is incorrect. Follow my post to get to a solution.

Modifying the code you posted:

for(j = 0; j < n - 1; j++)
{
  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++.

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;
}

the function could look sort of like this:

typeOfReturnValue nameOfFunction(parameterType parameterName, secondParameterType secondParameterName)
//start function
   declare int called j
   declare variableToReturn

   //use a single loop starting here
   loop through array using index called j which ranges from 0 to array size minus 2
   each time through the loop increment j by 1
      //compare value of element at index j + 1 with value of element at  index j
      if element at j + 1 is less than element at j
          break out of loop since element at j + 1 is out of place
   //loop ends here

   //now that loop is done determine value of j
   if j is index of last element in array 
      then array is in order so assign appropriate value to variableToReturn
   else  //the array is not in order
      so assign the first index of the array which is not in order to variableToReturn
  
   return variableToReturn
//end function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.