|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
Code:
// Start out by saying that the array is not sorted
// because we have not gone through the array
bool sorted=false;
for (pass = 1; pass <= NUM_QUIZZES - 1&&!sorted; ++pass)
{
// Set sorted to true. now we will go though the list once
// if we find any variable out of place then we set sorted
// to false. If we have made it through without moving
// any variables there is no reason to repeat the outer
// loop because we will not move any variables on
// subsequent checks.
sorted=true;
for (quiz = 0; quiz <= limit; ++quiz)
if (grade[quiz] > grade[quiz + 1])
{
temp = grade[quiz];
grade[quiz] = grade[quiz + 1];
grade[quiz + 1] = temp;
sorted = false;
}
--limit;
}
Last edited by WingedPanther; 09-27-2007 at 10:51 AM. Reason: Add code tags |
|
|||||
|
felixme86, here on CodeCall (and on many other forums) we've a so-called code-tags. It's easier for us to read your code, if you use them. They work in this way:
Code:
[code] This is where you put your code. [/code]
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. - To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. - To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. I'm always up for a chat, so feel free to contact me... |
|
|||||
|
Code:
/* Prob 11-10
* Created by Yuriy Mokriy
*/
#include <stdio.h>
#define NUM_QUIZZES 10 /* Total number of quiz grades in the array */
main()
{
int grade[NUM_QUIZZES], /* The array to store the quiz grades */
temp, /* For swapping array elements */
quiz, /* Subscript for the array grade[] */
pass, /* The number of the pass */
limit, /* Keeps track of how far to go on a pass */
sorted = 1; /* A flag that indicates when the array is sorted */
/* Get the grades */
printf("\nPlease enter %d integer quiz grades. \n\n", NUM_QUIZZES);
for (quiz = 0; quiz < NUM_QUIZZES; quiz++)
{
printf("\nEnter grade for quiz %d: ", quiz + 1);
scanf("%d", &grade[quiz]);
}
/* Check if grade entered is greater or smaller than the one previous */
if (quiz > 0)
{
if (grade[quiz] < grade[quiz - 1])
sorted = 0;
else
sorted = 1;
}
/* Display the quiz grades */
printf("\n\nThe grades you entered are as follows:\n");
for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
printf("%6d", grade[quiz]);
printf("\n");
/* Do the bubble sort if sorted is equal to 0 */
limit = NUM_QUIZZES - 2;
if (sorted != 1)
{
for (pass = 1; pass <= NUM_QUIZZES - 1; ++pass)
{
for (quiz = 0; quiz <= limit; ++quiz)
if (grade[quiz] > grade[quiz + 1])
{
temp = grade[quiz];
grade[quiz] = grade[quiz + 1];
grade[quiz + 1] = temp;
sorted = 1;
}
--limit;
}
}
/* Display the sorted quiz grades */
printf("\n\nThe grades in increasing order are as follows:\n");
for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
printf("%6d", grade[quiz]);
printf("\n");
}
![]()
__________________
For $1000: Something that is a miserable pile of secrets. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python arrays... | Sir_Rimo | Python | 3 | 06-20-2007 08:54 AM |
| Finding non-standard sorting order via graph theory | elle | General Programming | 1 | 05-17-2007 09:36 PM |
| Arrays | clookid | PHP Tutorials | 1 | 01-11-2007 08:30 PM |
| Sorting Arrays | falco85 | PHP Forum | 5 | 08-23-2006 09:31 PM |
| Arrays | Sionofdarkness | C and C++ | 5 | 07-26-2006 05:35 PM |
| Xav | ........ | 1097.16 |
| MeTh0Dz|Reb0rn | ........ | 986.37 |
| morefood2001 | ........ | 850.04 |
| John | ........ | 841.93 |
| WingedPanther | ........ | 684.54 |
| marwex89 | ........ | 638.26 |
| Brandon W | ........ | 493.36 |
| chili5 | ........ | 292.12 |
| Steve.L | ........ | 188.37 |
| orjan | ........ | 187.41 |
Goal: 100,000 Posts
Complete: 79%