+ Reply to Thread
Results 1 to 4 of 4

Thread: C++ code

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    2

    C++ code

     
    Hey, i'm trying to write a C++ code to calculate the standard deviation from an imput file that containts 3 columns of numbers, the x values, y values, and sigma (errors on each value), so far i have: /n/n

    Code:
    // include all headers needed here
    #include <iostream>
    #include <cmath>
    #include <cctype>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    
    //declare sum here and link to calculation after main (prototype fucntion declaration)
    
    int main (){
    
        //declare doubles here (what doubles do i need?)
        double x, y, sigma;
    
        // read the input and output filenames
        
    
        double a[1000][3];//x, y and sigma input values
        double b[1000][3];//x, y and sigma output values
    
        cout << "Please enter the input filename: " << endl;
        cin >> a;
        cout << "Please enter the output filename: " << endl;
        cin >> b;
    
        // declare array of structs, open input file and read sets of 3 numbers
    
        // (x, y sigma) into array
    
        ifstream in(a);
        // read the input and output filenames
        if(!in){
            cerr << "Failed to open input file " << a << endl;
            exit(1);
        }
            int j = sizeof(a)/sizeof(double)//no. of items in array
    /n/nhow do i edit the data after i have stored it in the arrays?/b/bi am intending on putting an if function around the cin>>a to make sure that only numbers can be entered however i havnt got as far as that yet
    Last edited by nathan.humphreys; February 8th, 2009 at 11:55 AM.

  2. #2
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Is this a class assignment? If so, what specs did the teach give you? What's the name of the class?

    Or, are you writing this on your own?

    To me, it looks like a class assignment where the teach put in comments for you to "follow". Your code needs help

    And if I remember correctly, file streams in C/C++ are all CHARACTER data! Reading 54.23, 76, 92.99, .02 and converting to doubles would be fun! It's been a while since I've had to code stuff like this (like that damn add/subtract polynomials of any x^n value; that was a BEATCH program!!!)
    Last edited by Rootstonian; February 9th, 2009 at 11:18 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    2
    i put those comments in so i know where i need to go, this is a class assignment but im pretty sure once i know how to edit the data stored in the arrays then i will be able to figure the rest out

  4. #4
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Ok, do me a favor; I call it "going back to basics".

    Forget the files and the associated I/O.

    Write the program to work with functions and hard-coded values:

    double x1 = 2.2, x2 = 5.9, s1 = .01;
    double y1 = 4.2, y2 = 15.99, s2 = 3.01;
    double z1 = 22, z3 = 7.9, s3 = 9.01;

    cout << "Standard Devivation = " compute_sd(x1, x2, s1) << endl;
    cout << "Standard Devivation = " compute_sd(y1, y2, s2) << endl;
    cout << "Standard Devivation = " compute_sd(z1, z2, s3) << endl;

    There! 1/2 your program is done.
    Now, right a SKELETON procedure that just opens a file and checks for success.
    Then, put 3 intetgers into that that file; I forget syntax (that's why I have books), but the data is usually on one line space delimited (e.g. 3 15 6).
    Code your file read loop (again, read your book...while !InFile.eof() { cin >> number[i]}:
    Then replace code above (that worked with)
    cout << "Standard Devivation = "compute_sd(number[i]) << endl;

    I'll let you find out about C++ file I/O and character data vs. int vs. float data I've got my degrees LOL
    Last edited by Rootstonian; February 14th, 2009 at 09:31 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. BB. code ?
    By Chapper in forum Suggestion Box
    Replies: 6
    Last Post: September 29th, 2007, 01:00 AM
  2. Is this code OK?
    By atomicstomp in forum Webmastering and Programming
    Replies: 2
    Last Post: October 30th, 2003, 07:33 PM
  3. Help with a code
    By UpYourz in forum Webmastering and Programming
    Replies: 0
    Last Post: September 18th, 2003, 09:51 AM
  4. help with code
    By kydo76 in forum Webmastering and Programming
    Replies: 5
    Last Post: July 3rd, 2003, 12:40 AM
  5. Could someone help me with this code?
    By Ritalin Kid in forum Webmastering and Programming
    Replies: 7
    Last Post: February 20th, 2002, 08:13 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews