Thread: translate

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    translate

    I dont get this question can anyone make it much more easier to understand cause i dont understand the question you don't need to answer the question just explain what am i going to do tnx^_^..

    Write a program that computes the following sum:
    sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + .... + 1.0/N
    N will be an integer limit that the user enters.
    Enter N
    4

    Sum is: 2.08333333333


    I can't understand the question.......???help needed

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + .... + 1.0/N
    Use a for loop on the red bits.
    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
    May 2006
    Location
    in your head
    Posts
    49
    like this??
    Code:
    for(int a=1; a<=N; a++){
    formula..........
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by limitmaster
    like this??
    Code:
    for(int a=1; a<=N; a++){
    formula..........
    }
    Perhaps. Real code gets real answers. (And you'd probably have already answered that question if you had tried real code.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    okey tnx...^_^
    Code:
    #include <iostream>
    
    main(void)
    {
    
    int n, sum =1;
    std::cout << "input number\n";
    std::cin>> n;
    std::cin.ignore();
    for(int a=1; a<=n; a++){
    sum += 1.0/a;
    }
    std::cout << sum;
    std::cin.get();
    }
    Last edited by limitmaster; 08-12-2006 at 10:14 PM.

  6. #6
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    I got another problem and again can someone make it clearer to me as much as possible tnx..
    here's the question;

    Write a program that computes the standard deviation of a set of floating point numbers that the user enters. First the user will say how many numbers N are to follow. Then the program will ask for and read in each floating point number. Finally it will write out the standard deviation. The standard deviation of a set of numbers Xi is:
    SD = Math.sqrt( avgSquare - avg2 )
    Here, avg is the average of the N numbers, and avg2 is its square.
    avgSquare is the average of Xi * Xi. In other words, this is the average of the squared value of each floating point number.
    For example, if N = 4, say the numbers were:
    Xi Xi * Xi
    2.0 4.0
    3.0 9.0
    1.0 1.0
    2.0 4.0
    ----- ------
    sum 8.0 18.0
    then
    avg = 8.0/4 = 2.0
    avg2 = 4.0

    avgSquare = 18.0/4 = 4.5

    SD = Math.sqrt( 4.5 - 4.0 ) = Math.sqrt( .5 ) = 0.7071067812
    To do this you will need to do several things inside the loop body for each floating point value as it comes in: add it to a sum, square it and add it to a sum of squares. Then after the loop is finished apply the formula.

    Im poor in analysis and that is a very big problem in me

    __________________________________________________ ______________________________________

    nvm the question above^_^. i think i got it......but i have a question..........
    the user must determine how many numbers he/she gonna input for ex. the user want's to input 5 or 3 or 6 numbers... i can't use if else statement i can't say
    Code:
    if(num==5){ for(int a=1; a<=5; a++) cout << "input number"; }or if(num==3)etc..
    .......i don't know what to use....anyone there that can help me out tnx...
    Last edited by limitmaster; 08-13-2006 at 12:19 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > First the user will say how many numbers N are to follow.
    So start with that then.

    You've described in a very detailed manner what you want to do, so now just go and do it.

    Print out your description on paper and read it one sentence or paragraph at a time.
    Copy that statement as a comment in the code, then write and test the code (should be just a few lines in most cases).
    When it's done, cross that description out on your paper and move onto the next one.

    Eg.
    The words say
    "First the user will say how many numbers N are to follow."

    The code looks like
    Code:
    // First the user will say how many numbers N are to follow.
    int N;
    cout << "How many?";
    cin >> N;
    See how you get on with that and post where you get to.
    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.

  8. #8
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Yes thank you^_^ i got it...
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    float num;
    int avg = 0, avg2, n;
    cout << "How many numbers would u like to input^_^\n";
    cin >> n;
    for(int a =1; a<=n; a++){
            cout << "input number\n";
            cin >> num;
            cin.ignore();
    avg += num;
    }.............
    i'll just finish the code^_^tnx....

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    tnx for the help i completed my exercise^_^. I'll start my next one later....tnx again.
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(void)
    {
    float num, avg = 0, avgsqr = 0, tl1, tl2, SD;
    int n;
    cout << "How many numbers would u like to input^_^\n";
    cin >> n;
    for(int a =1; a<=n; a++){
            cout << "input number\n";
            cin >> num;
            cin.ignore();
    avg += num;
    num *= num;
    avgsqr += num;
    tl1 = avg/n;
    tl2 = avgsqr/n;
    SD = tl2 - tl1;
    }
    cout <<"avg = " << avg << "= " << tl1 << "\n";
    cout << "avg2 = " << sqrt(tl1) << "\n";
    cout << "avgSquare = " << avgsqr << "\n";
    cout << "avgSquare/" << n << " = " << tl2 << "\n";
    cout << "SD = " << sqrt(SD) << "\n";
    cin.get();
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looking pretty good.

    Just a couple of comments.
    > #include <math.h>
    The C++ namespace aware version of C header files drop the .h and add a 'c' to the front of the name, so this would be
    #include <cmath>

    The other point would be to tighten up the indentation of your code to be consistent.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please translate
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-31-2008, 06:37 AM
  2. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  3. Please help me... Translate from c++ to visual basic
    By westcard in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2004, 02:29 PM
  4. can anyone translate this all to aschii characters?
    By tetra in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 09:18 PM
  5. just translate !
    By black in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-03-2002, 02:14 AM