Thread: simple program help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    simple program help

    SUP!, well i need help, im using visual C++, and i need to write a simple program that reads 100 numbers from 1 to 100 on a txt file, calculates their average, and the standard deviation and then inputs the standard deviation and average into the original file at the bottom:? .....k i know you start off with the basic library's you'll be using and the basic setup and such,, but from there on i am completely lost8O ,........be great if someone could help me writing this out so i can understand what i should be doing..shouldn't take long since i was peeping the forum and people seem C++ savy here...

    btw the file im using is called data.txt...and is in the "Temp" folder of my c drive...THIS is attempt so far ....keep in mind im mad beginner ...in essence i need help in the whole std deviation part.

    THANKS!

    btw this is my code so far, be great if someone could tell me if im ok so far, aslo how i could do the standard deviation, i cant find a way to do it..
    Code:
    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main() 
    {
        float sum = 0, StdDev = 0;
        int x = 0;
        ifstream inFile;
        inFile.open("data.txt");
    
        while ( inFile >> x )
    	{
    		sum += x;
    	}	
        inFile.close();
    
    	ofstream outFile( "data.txt", ios::app );
    	outFile.seekp( ios::end );
    	outFile << sum << endl;
    	outFile.close();	
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Rule 5
    2. Do you know what "standard deviation" means in mathematical terms?

    I would leave off the "append result to file" part until everything works, otherwise you will end up processing whatever junk you wrote to the file the next time you run the code.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Hi, standard deviation just means, how you would normal do something, like 4/2, 4+5, 5*3 so on. I am working out a little example but lol I kinda forgot how to read numbers from a text file XD (guess cus its like 2:38 AM here) Anyways, if you do know how to code it I would just

    1. Add all the numbers up, at the same time, getting how many lines it takes to do this. Like...

    (This shows how many lines the're, not how to add the numbers in the text file.)
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() 
    {
    string line;
    int x;
    x=0;
    
      ifstream myfile ("tst.txt");
      if (myfile.is_open())
      {
         while (! myfile.eof() )
    	  {
          getline (myfile,line);
          x=x+1;
          cout << line << endl; 
    	  }
        myfile.close();
      }
        else cout << "Unable to open file";
        cout<<"Sum is "<<x<<endl; 
        getchar();
    }
    After you get all the lines you could just divide the numbers (numbers in the texts / by the number of lines the're) to get the average. Like lets say the number in the text was a sum of 10 and there was 5 lines, so it be 10/5 or 1/2.
    Last edited by adr; 10-08-2007 at 12:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM