Thread: pseudocode

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    pseudocode

    i have to write a program after the pseudocode that i already have :
    Code:
    READ X
    READ N
    Y <-- 1
    Counter <-- N
    WHILE Counter > 0
        Y <-- Y*X
        Counter <-- Counter-1
    EndWHILE
    WRITE Y
    That is the algorithm of the program. You must enter a number X that is a float and a N that is int .

    my code looks like this but it's not correct when i entered the values it doesn't calculate it right.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        int Y,N,Counter;
        float X;
        printf("Enter the real number:\n");
        scanf("&#37;f",&X);
        printf("Enter natural number:\n");
        scanf("%d",&N);
        Y = 1;
        Counter = N;
        while(Counter > 0)
    	{
    	    Y = Y * X;
    	    Counter = Counter-1;
    	    }
        printf("The final result is:\n%d",Y);
        getch();
    
        }
    Do i have to convert Y to float also to work?
    when i compile it and test it gives some numbers but not the ones it supose to .
    i also must specify this is not portable code its Borland C++ 3.1 DOS version.
    Please help me out or tell me where to study more.
    Thank You
    Last edited by samsung; 10-28-2007 at 12:01 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    my code looks like this but it's not correct when i entered the values it doesn't calculate it right.
    What values did you enter? Perhaps it is your expectation of the result that is incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    Let's say you enter tge real number X=3 and the natural nr N=10.
    59049 is the result does it look ok ?
    i don't think so

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's say you enter tge real number X=3 and the natural nr N=10.
    59049 is the result does it look ok ?
    Yes, it looks perfectly fine to me.

    Do you understand what your algorithm does?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    actually don't .
    can you please explain?
    i enter a real number .
    i enter a natural number.
    it does a loop . the N number specify the number of times the loop is run?
    if N <0 it doesn't execute the while sequence .
    What i don't understand is what Counter = Counter - 1 ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here's a hint:
    Try with X=2 and N=1. Then try again with N=2. Then N=3, 4, 5...

    Of course, this would just be "engineer's intuition", since you are figuring out what the algorithm does based on a limited pattern. However, once you have your guess, go through your X=3 and N=10 case again, step by step, looking at how Y and Counter change on each iteration of the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    1 more thing Y is integer and X is float
    Y = Y*X is this allowed?
    i mean does it convert X to integer
    lets say if i enter the X 3,14 ?
    what happends then with it?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Y = Y*X is this allowed?
    Yes, but it means that your floating point values get truncated, so your calculations are more inaccurate,

    i mean does it convert X to integer
    More like the result of Y*X is truncated to an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    thanks for all your help .
    how you think the accurate program should look ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 09:07 AM
  2. URGENT Pseudocode help!!!
    By mcgeady in forum C Programming
    Replies: 10
    Last Post: 11-17-2004, 09:03 PM
  3. Poll: Is pseudocode really needed ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 02-28-2002, 06:33 PM
  4. Replies: 1
    Last Post: 02-24-2002, 06:22 AM
  5. pseudocode, algorithm and flowwchart
    By ucme_me in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 12:11 AM