Hi there,
I was trying to code a simple program that bubble sorts an array into a list of numbers arranged in a descending order:
Here is the code I used:
#define size1 5
#define size2 5
#include <stdio.h>
int main()
{
int a[size1]={8,4,5,2,3};//array 1
int i=0;
int n=0;
int temp=0;
//sort array A into a descending sequence
for(i=0;i<size1;i++)
{
for(n=i+1;n<size1;n++)
{
if (a[i]<a[n])
{temp = a[i];a[i]=a[n];a[n]=temp;}
}
}
for (i=0;i<size1;i++)
{
printf("%d\n",a[i]);
}
return(0);
}
Now the block of code written above works just fine, however if I replace
for(n=i+1;n<size1;n++)
with
for(n=1;n<size1;n++)
..the program randomly generates a sequence of numbers along with a garbage value. What's going on here? As far as I can see both codes are equivalent.
Any input will be appreciated.