# Need Help With C!!!

Discussion in 'OT Technology' started by ILuvMtnDew, Nov 6, 2004.

1. ### ILuvMtnDewGuest

Okay, so here is my assignment for my C programming class: Need to set up a 10 variable, 1 dimensional array. The user needs to be able to input all the numbers that will be put into the array. Then the computer will need to sort the array from greatest to smallest, but it has to stop once everything is in order...which means no bubble sort. So I have the BASIC programming done but it seems to be on an invinite loop. The theory behind my program is this:

I'll set up a loop within a loop, the first loop will keep going until break is equal to 1, at which point it'll print out the array. The second loop will go 10 times, each time it will check and see if Array[x] is greater than Array[x+1]. If it is, break = 0, otherwise break = 1. each time it loops, it'll take x+1. This way, once it goes through and array is = 0 after ALL the loops, it'll quit. Good concept, doesn't work... here is my code, any help is MUCH APPRECIATED!!!

#include <stdio.h>
int main()
{
int a[10];
int count;
int breaka;
int countb;
int hold;
void switchVars(int a[], int SIZE);
void printArray(int a[], int SIZE);
count = 0;
while (count <= 9)
{
printf("Enter a number: ");
scanf("%d",&a[count]);
count = count + 1;
}
count = 1;
countb = 1;
while (breaka != 0)
{
while (countb <= 10)
{
if (a[countb] >= a[countb + 1])
{
breaka = 0;
printArray(a,10);
}
else
{
breaka = 1;
printf("****%d****",countb);
printf("%d <= %d\n",a[countb],a[countb+1]);
}
countb = countb + 1;
}
switchVars(a,10);
countb = 1;
}
return 0;
}

void switchVars(int a[],int SIZE)
{
int pass, j, hold;
for ( j=0; j<=SIZE-2; j++ )
{
if ( a[j] < a[j+1] )
{
hold = a[j];
a[j] = a[j+1];
a[j+1] = hold;
}
}
return;
}

void printArray(int a[],int SIZE)
{
int i;
for ( i = 0; i < 10; i++ )
{
printf("a[%d]=%d ", i, a );
}
printf("\n");
return;
}

2. ### RactoonShibumi in progress...

Joined:
Apr 30, 2001
Messages:
256
0
Hmm, I don't know if I'm just reading that wrong but it looks like you're using bubble sort anyway with an odd implementation. Are you going for like an insertion sort or selection sort instead with early termination?

3. ### ILuvMtnDewGuest

Selection sort w/ early termination

Joined:
Jan 6, 2001
Messages:
2,337