I'm trying to write a program to read lists of number from a txt file then bubble sort it in increace order and find the average of every single list.
That's what i got so far

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;
void bubbleSort(int *array,int length)
  int i,j,temp;

  for (i=(length-1);i>= 0;i--)
    for (j=1;j<=i;j++)
      if (array[j-1]>array[j])

int main()
    int sum=0,temp=0, many=0;
    double average=0.0;
    ifstream myfile("data.txt");
        for(int i=0;i<many;i++)
        average=((double) sum)/many;
        cout<<"There were "<<many<<" numbers.\n";
        cout<<"Their average was "<<average<<endl;

I don't know if i'm doin' it right!

The limit for the 'i' loop in bubbleSort() is interesting, but it appears that it would work.

You will probably need to allocate the array and fill it with the data you read from the file. Then you will need to call bubbleSort() and pass it the array and the count.

Are you supposed to output the sorted array?

You should be doing your own testing on your application. You should create some data files with known data in them. You should manually calculate the count and average. Then when you run your application with your file as the input, you know what results the program should produce.

The goal of your testing is to find the problems yourself before someone else can complain. If you find your program is not producing the output you want, we can usually help.

Post the code, the data file and the output. Include the problem definition statement, something along the lines of: "When I run this program I expected to see ____ but I'm seeing ____ instead." or something similar.

This article has been dead for over six months. Start a new discussion instead.