| | |
Arrays problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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.
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
Yes the code you posted is incorrect. Follow my post to get to a solution.
Modifying the code you posted:
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++.
Modifying the code you posted:
C++ Syntax (Toggle Plain Text)
for(j = 0; j < n - 1; j++) { if(c[j + 1] < c[j])
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
the function could look sort of like this:
C++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Searching within arrays problem (PHP)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Using two vectors? (C++)
- C String Manipulation (C)
- Javascript array from sql query (JSP)
- Not Displaying all my entries!! (Java)
- Can't figure out this seg fault (C++)
- Lottery Program (Java)
Other Threads in the C++ Forum
- Previous Thread: linkedlist
- Next Thread: What the program does?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






