sort data

This is a discussion on sort data within the C++ Programming forums, part of the General Programming Boards category; Hi. I have written a program to import a data file and print out the data set. The program should ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    22

    Question sort data

    Hi. I have written a program to import a data file and print out the data set. The program should then sort the data by value, reprint it and find mean, stdev and create a histogram. The problem is that when it goes to sort the data and reprint it, it is repeats certain values instead of printing each once. It's getting mixed up. Thanx for any advice...
    Code:
    #include <iostream.h>
    #include <math.h>
    #include <iomanip.h>
    #include <fstream.h>
    
    void print_out(double a[100],int N);            //prototype
    void sort(double a[100], int N);
    
    int main()
    {
    double a[100];int i,N,j,his[50]={0};
    cout<<"Type in the number of data points"<<endl;
    cin>>N;
    
    
    if(N<=64){                                     //Read in data from file4.dat 
    ifstream source;
    source.open("file4.dat");
    if(!source)
    {cerr<<"Can't open data file."<<endl;
    return(1);}
                                                 
    for(i=0;i<N;i++)
    {source>>a[i];}
    source.close();}
    
    else{cerr<<"Too many data points."<<endl;
    return (1);}
    
    print_out(a,N);
    //line30
    sort(a,N);
    
    double sum1,average;
    sum1=0.0;                                      //Output the average value of the data set
    for(i=0;i<N;i++)
    {sum1=sum1+a[i];}
    average=sum1/N;
    cout<<"average value = "<<average<<endl;
                                     
                                                 
    double delta,sqrdelta,sum2,stdev;
    sum2=0.0;                                     //Output the standard deviation of the data set
    for(i=0;i<N;i++)
    {delta=a[i]-average;
    sqrdelta=delta*delta;
    sum2=sum2+sqrdelta;}
    stdev=sqrt(sum2/(N-1));
    cout<<"standard deviation of the set is "<<stdev<<endl;
                                                  
                                                  
    double xstart,delx;int icol=0,icolmax;        //Histogram
    cout<<""<<endl;
    cout<<"Type in the low boundary of the histogram."<<endl;
    cin>>xstart;
    cout<<"Type in the x-axis increment for the histogram (delta x value)."<<endl;
    cin>>delx;
    for(i=0;i<=N;i++)
    {icol=(a[i]-xstart)/delx;
    his[icol]=his[icol]+1;
    icolmax=(a[N-1]-xstart)/delx;
    }
                                                  
    //Print out histogram
    //x1 is the low end for each histogram column, x2 is the high end
    double x1=xstart,x2=xstart+delx;
    for(i=0;i<=icolmax;i++)
    {cout<<"From "<<x1<<" to "<<x2<<" there are "<<his[i]<<" measurements"<<endl;
    x1=x1+delx;
    x2=x2+delx;}
    
    return 0;}
                                                 
    
    //function print_out
    void print_out(double a[100],int N)
    {int k,j=0,i=0,colnum=8;
    cout<<"Unsorted data."<<endl;
    for(j=0;j<N;j=j+colnum)
    {k=colnum;
    if((j+colnum)>N)
    {k=N-j;}
    for(i=0;i<k;i++)
    {cout<<setw(8)<<a[i+j];}
    cout<<""<<endl;}}
    
    void sort(double a[100],int N)
    {double temp;int i,j,k,colnum=8;
    cout<<"Sorted data."<<endl;
    for(i=0;i<N-1;i++)
    {for(j=i+1;j<N;j++)
    {if(a[j]<a[i])
    {temp=a[j];a[j]=a[i];a[i]=temp;}}}
    for(j=0;j<N;j=j+colnum)
    {k=colnum;
    if((j+colnum)>N)
    {k=N-j;}
    for(i=0;i<k;i++)
    {cout<<setw(8)<<a[i+j];}
    cout<<endl;}}

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    test file?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    file4.dat is a text file that has numbers like
    2002 2006 2001 2005 2009 2010 2015 2103...
    if that helps.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    ok....what exactly does the program do? I have no clue on what it's doing, so I'm not sure what exactly is wrong with it....:-\

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    It reads in these data and prints them out. It then should sort the data by value and then reprint it out sorted. Thanks for sticking to helping me...

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Umm please, indent and format your code. What you have there is barely readable, use whitespace! Then I might be able to help you.

    Next, use the newer headers:
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    Like I said, the program is able to open up a text file that contains numerical data and print it as it is in the text file. But the program should then sort the data by value and reprint it. It worked when I put the code into the main function, but does not sort properly when I tried to use the sort function.
    Code:
    #include <iostream> 
    #include <cmath> 
    #include <iomanip> 
    #include <fstream> 
    using namespace std; 
    
    void print_out(double a[100],int N);                //prototype
    void sort(double a[100], int N);
    
    int main()
    {
    double a[100];int i,N,j,his[50]={0};
    cout<<"Type in the number of data points"<<endl;
    cin>>N;
    
    if(N<=64)
    {                                     //Read in data from file4.dat 
    ifstream source;
    source.open("file4.dat");
    if(!source)
    {cerr<<"Can't open data file."<<endl;return(1);}
    
    for(i=0;i<N;i++)
    {source>>a[i];}
    source.close();
    }
    
    else
    {cerr<<"Too many data points."<<endl;return (1);}
    
    print_out(a,N);
    
    sort(a,N);
    
    double sum1,average;
    
    sum1=0.0;                                      //Output the average value of the data set
    for(i=0;i<N;i++)
    {sum1=sum1+a[i];}
    
    average=sum1/N;
    cout<<"average value = "<<average<<endl;
    double delta,sqrdelta,sum2,stdev;
    
    sum2=0.0;                                     //Output the standard deviation of the data set
    for(i=0;i<N;i++)
    {
    delta=a[i]-average;
    sqrdelta=delta*delta;
    sum2=sum2+sqrdelta;
    }
    
    stdev=sqrt(sum2/(N-1));
    cout<<"standard deviation of the set is "<<stdev<<endl;
    
    double xstart,delx;int icol=0,icolmax;        //Histogram
    
    cout<<"Type in the low boundary of the histogram."<<endl;
    cin>>xstart;
    cout<<"Type in the x-axis increment for the histogram (delta x value)."<<endl;
    cin>>delx;
    
    for(i=0;i<=N;i++)
    {
    icol=(a[i]-xstart)/delx;
    his[icol]=his[icol]+1;
    icolmax=(a[N-1]-xstart)/delx;
    }
    
    //Print out histogram
    //x1 is the low end for each histogram column, x2 is the high end
    double x1=xstart,x2=xstart+delx;
    for(i=0;i<=icolmax;i++)
    {cout<<"From "<<x1<<" to "<<x2<<" there are "<<his[i]<<" measurements"<<endl;
    x1=x1+delx;
    x2=x2+delx;}
    
    return 0;}
    
    void print_out(double a[100],int N)
    {
    int k,j=0,i=0,colnum=8;
    cout<<"Unsorted data."<<endl;
    for(j=0;j<N;j=j+colnum)
    {
    k=colnum;
    if((j+colnum)>N)
    {k=N-j;}
    for(i=0;i<k;i++)
    {
    cout<<setw(8)<<a[i+j];
    }
    cout<<""<<endl;
    }
    }
    
    void sort(double a[100],int N)
    {
    double temp;int i,j,k,colnum=8;
    cout<<"Sorted data."<<endl;
    for(i=0;i<N-1;i++)
    {
    for(j=i+1;j<N;j++)
    {
    if(a[j]<a[i])
    {temp=a[j];a[j]=a[i];a[i]=temp;}
    }
    }
    for(j=0;j<N;j=j+colnum)
    {
    k=colnum;
    if((j+colnum)>N)
    {k=N-j;}
    for(i=0;i<k;i++)
    {
    cout<<setw(8)<<a[i+j];
    }
    }
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    22

    Wink

    I was able to get the sorting to work after rewriting the program from scratch. I think it helped to organize it on paper first. Thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 02:22 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21