need just a little help.

This is a discussion on need just a little help. within the C++ Programming forums, part of the General Programming Boards category; ok i posted in a exiting thread before as my question was relevant to that thread. Now im starting this ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    need just a little help.

    ok i posted in a exiting thread before as my question was relevant to that thread. Now im starting this thread so i wont hog the other person's thread.

    so far with the help of various people like, hk_mp5kpdw andyhunter, and 7stud ive managed to get a little further on my program but im still having some confusing results.

    here is the code
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;
    
    const int FILENAMELEN =20, ARRAYLEN =20;
    
    class array
    {
        public:
                int      readarray();
                array0();
                float      finddev();
                float  findaverage();
                float writeresults();
    
        private:
                int            j;
                int            i;
                int        count;
                int  numbers[20];
                float        sum;
                float    average;
                float        var;
                float        std;
                float  sqrofsums;
                float  sumofsqrs;
                char  values[20];
                char     filename[FILENAMELEN];
                ifstream inflie;
                //int currentChar;
                
        
    };
    array::array0()
    {
        count =0;
    
    }
    
    int array::readarray()
    {
        
        char filename[FILENAMELEN];
        char inchar;
        ifstream infile;
        //currentChar = 0;
        count = 0;
        
    
        cout<<"Enter filename->" <<'\t';
        cin>> filename;
    /*
        for (i=0; i<20; i++)
        {
            numbers[i]=0;
        }*/
    
        //openfile
        infile.open(filename);
        //if you cant open the file, print error message
        if (!infile)
        {
            cerr<< "Cannot open " <<filename << endl;
            exit(1);
        }
    
            //Read the file 
            
            while(!infile.eof() && count < 20)
            {
            
            //something is going on right here? getting mixed output
                
                infile.getline(values,20); 
                cout<<values<<endl;
    
                inchar =infile.get();
                cout <<inchar<<endl;        
                {
                    if( infile >> numbers[count]) ++count;
                    else break;
    
                }
                    
            }    
            
            infile.close();
            return(0);
    }
    
    //find the average
    float array::findaverage()
    {
        
        sum      = 0.0;
        average  = 0.0;
    
        for (j=0;j<count;j++)
        {
            sum+=numbers[j];
            
        }
            average =sum/j;
            return average;
    }
    
    //find the std
    float array::finddev()
    {
        sum       = 0.0;
        var       = 0.0;
        std       = 0.0;
        sqrofsums = 0.0;
        sumofsqrs = 0.0;
        
        //sum of the numbers squared
        for (j=0;j<count; j++)
        {
            sum+=numbers[j];
        }
        sqrofsums =(float)pow(sum,2);
    
        //sum of squared numbers
        for(j=0;j<count; j++)
        {
            sumofsqrs+=numbers[j] *numbers[j];
        }
    
        //variance
            var =(sumofsqrs -(sqrofsums/j));
            var = (var/j-1);
    
            std = (float)sqrt(var);
    
        return std;
    }
    
    //display the results
    float array::writeresults()
    {
            
    
        cout << "The average is:  " << findaverage(); 
        cout << endl;
        cout << "The standard deviation is:   " << finddev(); 
        cout << endl;
        
        return 0;
    }
    
    int main()
    {
        int numbers[20];
        int *pn=numbers;
    
        array myarray;
        myarray.readarray();
        myarray.writeresults();
    
        return 0;
    }
    the text file is as follows
    array1.txt
    Code:
    5
    4
    3
    2
    6
    5
    the result i get is
    Code:
    Enter filename->        array1.txt
    5
    4
    
    2
    
    5
    The average is:  4.5
    The standard deviation is:   1.11803
    Press any key to continue
    When no one helps you out. Call google();

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Alright the only thing you are trying to do is read in the integers stored in the file right? Thus you can greatly reduce the size and complexity of your read loop. Something like this would suffice:
    Code:
    //in class def
    int count;
    int num;
    int numbers[20];
    
    //your read loop
    
    while(!((myFile >> num)==NULL)) { //check for EOF
            numbers[count++] = num; // load contents of file into your int array
    }
    Of course you would also add in a check to ensure you do not go outside of the bounds of the array.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    myFile here would be the infile?
    When no one helps you out. Call google();

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    yes, and don't forget to add a bounds check for your array, aka count cannot be allowed to exceed 20.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    int array::readarray()
    {
    	
        //char inchar;
    	ifstream infile;
    	//currentChar = 0;
    	count = 0;
    	
    
        cout<<"Enter filename->" <<'\t';
        cin>> filename;
    
    /*	for (i=0; i<20; i++)
    	{
    		numbers[i]=0;
    	}*/
    
        //openfile
        infile.open(filename);
        //if you cant open the file, print error message
        if (!infile)
        {
            cerr<< "Cannot open " <<filename << endl;
            exit(1);
        }
    
        	//Read the file 
    		while(!((infile >> num)==NULL))//check for EOF
    		{ 
            numbers[count++] = num; // load contents of file into your int array
    		}	
    		infile.close();
    		return(0);
    }
    When no one helps you out. Call google();

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    And your bounds check would be:
    Code:
     //Read the file 
    while((!((infile >> num)==NULL)) && count < 21)//check for EOF and bounds check for array numbers
    { 
            numbers[count++] = num; // load contents of file into your int array
    }
    This will read your file into the array numbers.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

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