Program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compsci
    New Member
    • Jan 2007
    • 32

    Program help

    I have most of the program I just need help with A-C


    Translate the following algorthm into a main function for a C++ program.

    1. Read an integer number- N
    2. While N is not greater than zero
    2.1 Read another number - N.
    3. Set the product to one.
    4. For each value 1 to N
    4.1 product = product * value
    5. Print the product


    Write functions for the following.
    A. Write steps 1 and 2 above as a function which reads and returns the value of N that is read.






    B. Write steps 3 and 4 as a function that has N as the value parameter and computes the factorial of N.







    C. Rewrite the main function using the functions in A and B.
    #include <iostream>
    using namespace std;

    int funct (int n){
    int p;
    cout << "Enter a number:";
    cin >> n;
    p = n;
    return p;
    }


    int main()
    {
    int n;

    cout << "Enter a number:";
    cin >> n;

    while (n<=0)
    {
    cout << "Please a positive number";
    cin >> n;
    }

    int product = 1;
    for(int x = 1; x<=n; x++)
    {
    product = product*x;
    }
    cout << product << endl;

    cout << funct (n) << endl;



    return 0;
    }
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by compsci
    I have most of the program I just need help with A-C


    Translate the following algorthm into a main function for a C++ program.

    1. Read an integer number- N
    2. While N is not greater than zero
    2.1 Read another number - N.
    3. Set the product to one.
    4. For each value 1 to N
    4.1 product = product * value
    5. Print the product


    Write functions for the following.
    A. Write steps 1 and 2 above as a function which reads and returns the value of N that is read.



    int read();


    B. Write steps 3 and 4 as a function that has N as the value parameter and computes the factorial of N.


    int factorial(int N);




    C. Rewrite the main function using the functions in A and B.
    #include <iostream>
    using namespace std;

    int funct (int n){
    int p;
    cout << "Enter a number:";
    cin >> n;
    p = n;
    return p;
    }


    int main()
    {
    int n;

    cout << "Enter a number:";
    cin >> n;

    while (n<=0)
    {
    cout << "Please a positive number";
    cin >> n;
    }

    int product = 1;
    for(int x = 1; x<=n; x++)
    {
    product = product*x;
    }
    cout << product << endl;

    cout << funct (n) << endl;



    return 0;
    }
    There I did the first two for you, you can do the rest.

    Comment

    Working...