help with small program. array issue

This is a discussion on help with small program. array issue within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream.h> #include <stdlib.h> // Math library #include <math.h> const int FileNameLen = 30, ArrayLen = 6; // The ...

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

    help with small program. array issue

    Code:
    #include <fstream.h>
    #include <stdlib.h>
    // Math library
    #include <math.h>
    
    const int FileNameLen = 30, ArrayLen = 6;
    
    // The class array
    class array 
    {
    public: 
    array(void);
    //Inline functions 
    inline int getsize() {return(size);}
    void read(void);
    void write(void);
    void     draw(void);
    private:
    
    //Private - available only to member functions
    int x[ArrayLen];
    int size;
    };
    
    
    array::array(void)
    {
    size = 0;
    }
    
    //  Read the array
    
    void array::read(void)
    {
    size = 0;
    // Read each value 
    do {
    cout << "Enter a value\t?";
    cin >> x[size++]; // Read an integer 
    } while (x[size-1] != 0 && size < ArrayLen);
    
    // Array indices run from 0 to n-1 
            size = (size == ArrayLen)? size - 2: size - 1;
    }
    
    
    void array::write(void)
    {
    //CREATE A FILE
    ofstream outfile; // file object
    char filename[FileNameLen]; // Store the file name
    // as a character string
    cout << "Enter file name ->\t";
    cin >> filename;
    
    // Open the file
    outfile.open(filename);
    // If you can't open the file, print an error message
    // and quit
    if (!outfile) {
    cerr << "Cannot open " << filename << endl;
    exit(1);
    }
    
    
    for (int i = 0;  i < size;  i++) 
    outfile << "x[" << i << "] = " << x[i] << '\n';
    
    
    }
    
    void array::draw(void)
    {
    ifstream infile; // file object
    char filename[FileNameLen]; // Store the file name
    char inchar; // as a character string
    
    cout << "Enter file name ->\t";
    cin >> filename;
    
    infile.open(filename);
    
    if (!infile) {
    cerr << "Cannot open " << filename << endl;
    exit(1);
    }
    
    //Read the file  a charachter
    while (!infile.eof())  
    {
    inchar = infile.get(); //This reads in 
    //even whitespace charachters
    cout << inchar; //Display it on screen
    
    } 
    infile.close();  //close the file
    
    
    }
    
    
    
    int main(void)
    { 
    
    double sum = 0.0;
    double sumOfSquares = 0.0;
    double average = 0.0;
    double standardDeviation = 0.0;
    
    //display file of screen
    const int FileNameLen = 30; // constant integer
    ifstream infile;   //file object
    
    average = sum / ArrayLen;
    standardDeviation = sqrt(sumOfSquares / ArrayLen - average * average);
    
    
    array myarray;
    
    
    myarray.draw();
    
    cout << "\nThe average is: " << average << endl;
    cout << "The standard deviation is: " << standardDeviation <<endl;
    return(0);
    }
    also consider that the program reads the following txt file that has these values
    x[0] = 5
    x[1] = 6
    x[2] = 7
    x[3] = 45

    so the program has to find the average and standard deviation. can someone help me out and show me how is that done? i have the formulas and all but i dont know how to compute all that from the array.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Is your code as badly formatted in your source code editor as it appears here?
    Tidying up the presentation so that indentation follows the structure of the program would be a start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You must create a loop to go through each element of the array. You can either do this inside a sum or average function that you add to the Array class, or you can add some sort of get method to let you get all the values in the array and make the sum function global.


    Quote Originally Posted by Salem
    Is your code as badly formatted in your source code editor as it appears here?
    Tidying up the presentation so that indentation follows the structure of the program would be a start.
    I have actually noticed the loss of indentation when pasting code in the new version of the forum software. Of course, I noticed this when I hit Preview Post, so I fixed it. I think it may have something to do with using the Enhanced interface.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Can I issue a command to DOS from within a program?
    By Sukarn in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2006, 06:44 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 06:33 AM
  5. help with array & function program
    By Sway in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2002, 07:30 PM

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