Notices
Results 1 to 6 of 6

Thread: I've A Question

  1. #1 I've A Question 
    Forum Freshman
    Join Date
    Oct 2012
    Posts
    18
    Hey Guys,

    I have a question here:

    Write a program that reads a set of floating point values from user input and computes the count of values, the average and the standard deviation.

    You can compute the quantity by keeping track of count, the sum and the sum of squares as you process the input values..

    p/s:thanks in advanced


    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Professor pyoko's Avatar
    Join Date
    Jun 2011
    Location
    Australia
    Posts
    1,094
    That's asking to do your homework for you :P And you haven't stated what language you are using.

    Use an array.


    It is by will alone I set my mind in motion.
    Reply With Quote  
     

  4. #3  
    Forum Freshman
    Join Date
    Oct 2012
    Posts
    18
    Quote Originally Posted by pyoko View Post
    That's asking to do your homework for you :P And you haven't stated what language you are using.

    Use an array.

    yeah...=) i have try the question for hours.This is c++ language
    Reply With Quote  
     

  5. #4  
    Moderator Moderator Markus Hanke's Avatar
    Join Date
    Nov 2011
    Location
    Ireland
    Posts
    7,302
    Like pyoko said, use an array of floating point numbers as your main data structure. Then set up a loop, and read user input one by one into your array ( it is good practice to include some form of validation routine, to make sure you get only valid floats ), bearing in mind that you need some way to exit the loop, even if the user only enters nonsense. Once the array is populated you can then calculate the count, average and standard deviation; there are library routines for that I believe, or you can do that manually. The trick with C++ is to keep everything as simple and straightforward as possible - the simpler your routine, the less potential for errors you will have. And always assume that the user is a plonk, so your routine must be able to handle nonsense input.
    Reply With Quote  
     

  6. #5  
    Forum Freshman
    Join Date
    Oct 2012
    Posts
    18
    thanks for your feedback.Can u take a look on my wrong answer

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main ()
    {
    int sum=0;
    int numbers;
    int sumproduct=0;
    int std;
    int sumsqro;
    int i=1;
    while (i>=0)
    {
    cout<<"Enter A Number"<<endl;
    cout<<"Press-1 to exit"<<endl;
    cin>>numbers;


    while (numbers!=1)
    {


    sum+=numbers;
    sumproduct+=pow(numbers,2);
    sumsqro=pow(sum,2);
    }
    i++;
    }


    std=sqrt((sumproduct-(sumsqro/i)) /i-1);
    cout<<"This Is Your Answer"<<std<<endl;


    return 0;
    }
    Reply With Quote  
     

  7. #6  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,679
    You have not defined numbers as an array so it will only store one value. This could work if you read the values in within the loop. As it is you only read one value in before the loop starts. It might be better to use a do ... while loop.

    You also have an error that means your loop will not terminate as you expect. I'll leave you to spot that...
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

Similar Threads

  1. RELATED QUESTION TO MY LIGHT QUESTION BELOW
    By BARCUD in forum Physics
    Replies: 9
    Last Post: January 30th, 2009, 06:58 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •