0

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;

}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by zeroliken
1

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

Votes + Comments
right
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.