I am trying to write a program that will pring a list of random numbers from 1-100 and store them in an array. and then bubblesort them into ascending order. This is what i have so far but it is giving me a whole different set of ascending numbers after the sort. please help! this is what i got so far.

#include<stdio.h>

int main(){

int s,temp,i,j,a[20],x;

srand(time(NULL));
while (s<10)
   {
    s++;
    printf("%d\n",rand()%100+1);

   }

  for(i=0;i<s;i++){
   a[i]=rand()%100+1;}




  //Bubble sorting algorithm

  for(i=s-2;i>=0;i--){

      for(j=0;j<=i;j++){

           if(a[j]>a[j+1]){

               temp=a[j];

              a[j]=a[j+1];

              a[j+1]=temp;

           }

      }

  }




  printf("After sorting: \n");

  for(i=0;i<s;i++)

      printf(" %d\n",a[i]);




  return 0;

}

a whole different set of ascending numbers after the sort

for(i=0;i<s;i++){
a[i]=rand()%100+1;}

the values you assign to the array are different than the values you displayed on the previous while loop
print the values of the array in this loop to see

commented: right +2
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.