home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++: Recognising characters and numbers for the std deviation

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1171
Discussions: 200,929, Posts: 2,379,148, Members: 246,296
Old February 14th, 2008, 11:40 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Feb 2008
Posts: 2
Post
C++: Recognising characters and numbers for the std deviation

Hi,

I've almost completed my first c++ programming assignment that calculates the mean, standard deviation of the distribution and standard deviation of the mean.

Everything runs smoothly when a file of numbers is opened with my program, all the values for the mean...etc...are correct.

However i'm having major problems when there is an inclusion of a character in a file that I open. For example the following sequence
2 4 5 7 4 g 6 h j 7.
My program reads the 2,4,5,7,4 but then ignores everything after the first letter that is part of the sequence. I just want to create an error message for when there are characters on the input file.

any help is appreciated, thank you

P.S how does the programme look for an absolute beginner ?

Code:
include <cmath> // for math calculations #include <iostream> // for general input/output #include <fstream> // for file input/output #include <cstdlib> // for memory management #include <string> // string used to replace char  usingnamespace std; double mean(double * Array, double N); double std_dev(double * Array, double N); // declaring 2 prototype functions int main(){  double array[10000]; // declaring an array holding 10000 integers constint Y = 5000; // number of integers allowed in file string infilename; cout << "Please enter the input filename. "<< endl; cin >> infilename; // inputting and reading input filename ifstream in(infilename.c_str()); if(!in){ cerr << "Failed to open input file " << infilename << endl; exit(1); } // error in reading the input file int i = 0; while(in){  if(i >= Y) break; // close file if integer limit exceeded if(in >> array[i]) i++; // close at end of file  }  in.clear();  in.close(); // closing input file double M = mean ( array, i); double S = std_dev ( array, i); double SM = S/i; // declaring Mean, Standard deviation and Standard deviation of mean  cout << " There are "<< i << " numbers" << endl; cout << " The mean is " << M << endl; cout << " The standard deviation of the distribution is " << S << endl; cout << " The standard deviation of the mean is " << SM << endl; } // writing output closing it  double mean (double * array, double N) { double sum = 0 ; for (int i = 0; i < N; i++) sum = sum + array [i]; return sum/N; } // function calculating mean double std_dev (double * array, double N) { double sum = 0; double STD_DEV = 0; // returning zero's for (int i = 0; i < N; i++) { sum = sum + array [i]; STD_DEV = STD_DEV + pow(array [i], 2); } return sqrt ((STD_DEV/N) - (pow(sum/N,2))); } // function calculating standard deviation
sagz is offline   Reply With Quote
Old February 19th, 2008, 02:53 PM     #2 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
Probably better off reading it in as character data; of course this assumes data will not contain decimal points.

Your array is declared double, but the comment says integer!

If the limit (Y) is 5,000, then why are you declaring an array twice that big???

I don't like your file read either...I would use "while !(In.eof())" for the loop.

Are you getting the right answer for these or just checking your data is read in correctly? Because I don't think your logic will result in the correct answers...
Rootstonian is offline   Reply With Quote
Old February 22nd, 2008, 01:30 PM     #3 (permalink)
Junior Member
 
Join Date: Feb 2008
Posts: 2
Thank you Rootstonian.

The comment was a typo on the array and the limit was pretty stupid of me !!!

I have replaced the while! with "while !(In.eof())" and inserted an error for the inclusion of characters. That eof statement was the key to answering my problem, fantastic.

Anyone who wants to use this program....the program produces the correct numbers, spots an invalid filename and also checks if there is any characters in the file.
sagz is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Firewire not recognising a Sony camera BluesMan1 Technical Support 5 June 8th, 2005 09:24 AM
Sig- only 150 characters? jmoore2001 IMO Community 15 November 8th, 2004 11:13 PM
C++ calculating standard deviation Elburn Webmastering and Programming 30 March 18th, 2004 07:16 PM
DVDRW not recognising media Dreemur Technical Support 1 April 11th, 2003 04:33 PM
More characters! Theophylact Suggestion Box 17 July 31st, 2002 01:15 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2868)
Obama the Muslim (14)
California Passes Anti-Flat-HDTV Le.. (39)
Is the PSU I received dead? (10)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Install XP pro and a Vista laptop ?.. (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Ideal cheap graph card for PC-Gamin.. (16)
New Computer wont recognize XP disc (7)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
Regular Build (3)
solutions for virtical white lines on.. (0)
Ideal cheap graph card for PC-Gaming? (16)
Graphics Card Upgrade Question (0)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Install XP pro and a Vista laptop ?? (10)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)


All times are GMT -4. The time now is 04:52 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28