Need some answers to these super short problems

This is a discussion on Need some answers to these super short problems within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Sebastiani Just FYI, gratuitous space != obfuscation. Yeah I just wanted to quickly make something confusing looking ...

  1. #16
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Quote Originally Posted by Sebastiani View Post
    Just FYI, gratuitous space != obfuscation.
    Yeah I just wanted to quickly make something confusing looking so that's what I got

  2. #17
    Registered User
    Join Date
    Jan 2010
    Posts
    8
    Quote Originally Posted by Sebastiani View Post
    Alternately, you can use Gauss's equation: ( N * ( N + 1 ) ) / 2.
    Heres what I did:
    Code:
    #include <iostream>
    //Write a program that calculates the sum N numbers.
    using namespace std;
    int main()
    {
    int N = 0;
    int sum = 0;
    
    cout << "Enter N...>"<< endl;
    cin >> N;
    for( int i = 1; i <= N; i = i + 1)
    {
    sum = sum + i;
    }
    cout << "The total sum of N numbers: " << sum << endl;
    system ("pause");
    return 0;
    }

  3. #18
    Registered User
    Join Date
    Jan 2010
    Posts
    8
    OK take a look at this:
    Write a program that calculates the sum of squares of numbers from 1 to N.

    Heres what Ive done but the result (when i run) its not correct.
    Code:
    #include <iostream>
    //Write a program that calculates the sum of squares of numbers from 1 to N.
    using namespace std;
    int main()
    {
    int N = 0;
    int sum = 0;
    
    cout << "Enter a number...>" << endl;
    cin >> N;
    
    for( int i = 1; i <= N; i = i + 1){
    sum = sum*sum + i;
    }
    cout << "The total sum of of squares of numbers from 1 to N is: " << sum << endl;
    system ("pause");
    return 0;
    }

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by dnilra View Post
    OK take a look at this:
    Write a program that calculates the sum of squares of numbers from 1 to N.

    Heres what Ive done but the result (when i run) its not correct.
    Code:
    #include <iostream>
    //Write a program that calculates the sum of squares of numbers from 1 to N.
    using namespace std;
    int main()
    {
    int N = 0;
    int sum = 0;
    
    cout << "Enter a number...>" << endl;
    cin >> N;
    
    for( int i = 1; i <= N; i = i + 1){
    sum = sum*sum + i;
    }
    cout << "The total sum of of squares of numbers from 1 to N is: " << sum << endl;
    system ("pause");
    return 0;
    }
    Instead of multiplying the 'sum' with itself and then adding 'i', you should be adding the product of 'i' with itself to 'sum'.

    Also, for future reference, you can simply use Aryabhata's formula: ( ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ).

  5. #20
    Registered User
    Join Date
    Jan 2010
    Posts
    8
    Quote Originally Posted by Sebastiani View Post
    Instead of multiplying the 'sum' with itself and then adding 'i', you should be adding the product of 'i' with itself to 'sum'.

    Also, for future reference, you can simply use Aryabhata's formula: ( ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ).
    Ok thanks I fixed that and its working . THanks a lot.

    3 to go now:
    Write a program to show all the numbers that are divisible by X from 1 to n (dont have a clue)
    Write a program that calculates an average of n scores entered by user (dont understand it)
    Write a program to create a countdown from N to 1 and display a text as a fire (need some serious help on this)

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by dnilra View Post
    Ok thanks I fixed that and its working . THanks a lot.

    3 to go now:
    Write a program to show all the numbers that are divisible by X from 1 to n (dont have a clue)
    Write a program that calculates an average of n scores entered by user (dont understand it)
    Write a program to create a countdown from N to 1 and display a text as a fire (need some serious help on this)
    1) Set up a loop as you did in your previous problems. Apply the modulo operator to the increment variable. If it's zero, then X divides it evenly, so print it.
    2) Read input until the user is done. Each iteration, add it to the running total, and increment a counter that keeps track of the number of entries thus far (alternately, you can prompt the user for the number of scores to be entered, beforehand, but this is a bit less flexible). After the data entry phase is complete, divide the running total by the number of entries to obtain the average.
    3) Not clear on the meaning of that one.

  7. #22
    Registered User
    Join Date
    Jan 2010
    Posts
    8
    Thanks all of you.

    Finished.

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by dnilra View Post
    Im offering a domain in exchange for this...
    Right, so you were publicly offering a bribe for others to help you break the rules?

    Think again!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a 2-D tile system...
    By Raigne in forum C++ Programming
    Replies: 14
    Last Post: 05-24-2007, 05:51 PM
  2. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 12:19 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2006, 12:54 AM
  4. Rectangle List Craziness
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 05-01-2006, 07:24 PM
  5. Practice C Problems, with answers
    By john123 in forum C Programming
    Replies: 3
    Last Post: 12-01-2002, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21