i have managed to write a program that reads an arbitrary number of integers less than or equal to 20 and finds the sum and average. i am now trying to write a bubble sort to order the integers from least to greatest but i am having problems. this is the code i have so far.

if it works its suppose to do this.
3 2 1 *
Entered integer 0: was 3
Entered integer 1: was 2
Entered integer 2: was 1
The sum was 6
the average is: 2.000000
--------------
The sorted list is 0: 1
The sorted list is 1: 2
The sorted list is 2: 3

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int count = 0;
  int array[SIZE];
  int sum = 0;
  int i = count;

  while (scanf("%d", &array[count]) != 0 ){
    printf("Entered Integer number %d: was %d\n", count, array[count]);
    sum += array[count];
    count++;
  }  

  printf("The sum was %d\n", sum);
 
  float average = (float)sum / count;
  printf("The average is: %f\n", average);

  int this, next, temp;

  for ( this = 0; this < count ; this ++){
    for ( next = this + 1; next < count ; next ++ ){
      if ( array[this] > array[next] ){
	temp = array[this] ;
	array[this] = array[next] ;
	array[next] = temp ;
    
      }
    }
  } 
  printf("------------\n");
  for(count = 0; count < i; count++);{
    printf("%d\n", array[count]);
  }

return 0;
}

it instead does this
3 2 1 *
Entered integer 0: 3
Entered integer 1: 2
Entered integer 2: 1
The sum was 6
The average is: 2.00000
-----------------
1

please help

Recommended Answers

All 3 Replies

In a previous version, i may have equaled the count of the numbers entered, but not now.

Now you want i=0;i<count;i++ and %d, array for the for and print statements, respectively.

Technically, the top for statement should be "this < count-1". Why? Because next is always one element higher than "this". So "this", doesn't need to go to the end of the array. In reality, it just loops one more time, doing nothing, because "next" has already reached the highest element in the array, and won't even start looping outside the array. So it's fine, but know that, if you're asked by some smarty pants teacher. ;)

In a previous version, i may have equaled the count of the numbers entered, but not now.

Now you want i=0;i<count;i++ and %d, array for the for and print statements, respectively.

Technically, the top for statement should be "this < count-1". Why? Because next is always one element higher than "this". So "this", doesn't need to go to the end of the array. In reality, it just loops one more time, doing nothing, because "next" has already reached the highest element in the array, and won't even start looping outside the array. So it's fine, but know that, if you're asked by some smarty pants teacher. ;)

i messed around with it and go this program which works too

#include <stdio.h>

#define SIZE 20

int main(void)
{
  int i, count = 0;
  int array[SIZE];
  int sum = 0;
 
  while (scanf("%d", &array[count]) != 0 ){
    printf("Entered Integer number %d: was %d\n", count, array[count]);
    sum += array[count];
    count++;
  }  

  printf("The sum is: %d\n", sum);
 
  float average = (float)sum / count;
  printf("The average is: %f\n", average);

  while (count < SIZE && scanf("%d", &array[count]) == 1) count++;
  i = count;

  int this, next, temp;

  for ( this = 0; this < count ; this ++){
    for ( next = this + 1; next < count ; next ++ ){
      if ( array[this] > array[next] ){
	temp = array[this] ;
	array[this] = array[next] ;
	array[next] = temp ;
    
      }
    }
  } 
  printf("------------\n");
  for(count = 0; count < i; count++){
    printf("The sorted list %d: is %d\n", count, array[count]);
  }

return 0;
}

i now need to have the program print an error when the number of integers given is more than 20. where would i put this loop in my program? im thinking it would have to go in the first one

Line 11 above, should be removed, and line 22 should be put in it's place. Just be sure to remove the count++ on the end of the line. You want to keep the count++ that you have now, in place.

Then, if you want the user to quit entering data by entering a zero, put this line into line 12:

if(array[count]==0)
  break;

If it's done at line 12, then the zero will not be counted, or put into any part of the average. Otherwise, it will be part of both those variables.

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.