Page 4 of 4 FirstFirst ... 234
Results 31 to 40 of 40

Thread: Square root algorithm

  1. #31
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    "A stream is an abstraction that represents a device on which input and ouput operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length."

    int main (int argc, char** argv)
    In this part integrer means that the user performs input in fortm of integer( n!) and main initiates the declaration of a function.
    Char is a parameter, that determines the type of elements that are going to be manipulated, in this case argc which is integer.

    As you pointed out what if we do not want to prompt a user for information but initialize the istringstream with the data string (eg. user enters a random number and program calculates it`s factorial). In our case you want to tell the code to perform <iostream>
    objects. Narrow-oriented objects include cin, cout, cerr and clog. What do they do? (cout calculates).

    In summary <iostream> declares the objects used to communicate through the standard input and output (including cin and cout) and whose properties I don`t exactly understand.
    Last edited by spacerocket; May 8th, 2014 at 03:21 PM.

  2. #32
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Square root algorithm

    Quote Originally Posted by spacerocket View Post
    In this part integrer means that the user performs input in fortm of integer( n!) and main initiates the declaration of a function.
    Char is a parameter, that determines the type of elements that are going to be manipulated, in this case argc which is integer.
    NO. The main() is declared that way because I expected to pass arguments to the application. argc is an integer that denotes the number of arguments that are being passed to the program, and argv is the array of pointers to char (i.e. strings).

    A main() function that declares the variables argc and argv (which by the way, are arbitrarily chosen names) will always have at least 1 argument... that is, the program name.

    As you pointed out what if we do not want to prompt a user for information but initialize the istringstream with the data string (eg. user enters a random number and program calculates it`s factorial). In our case you want to tell the code to perform <iostream>
    objects. Narrow-oriented objects include cin, cout, cerr and clog. What do they do? (cout calculates).
    You're spending way too much effort analyzing the program. Input has to arrive in a certain manner into the main() function, and it just so happens it is always as one or more null-delimited strings; in other words, all input arguments are treated as strings. For your particular application, a number is required, and thus it is necessary to convert a string into a number.

    I could have foolishly used atoi() to convert the string to a number, however this system function is replete with problems/limitations. Thus I chose to use istringstream, which is a useful way of converting a string to a number.

    In summary <iostream> declares the objects used to communicate through the standard input and output (including cin and cout) and whose properties I don`t exactly understand.
    At your stage of learning C++, rather than worry about how these constructs are implemented, why not just focus on merely using these tools to accomplish a task.

    I cannot, nor will most people, spend day-in/day-out explaining all of the details about C++ to you. I can say that it is not a trivial language, but on the other hand not super-complex, but I strongly believe that you would probably get better help by reading a good book on the C++ language. Or just experiment with small little programs so that you understand the basics.


    P.S. Why don't you run this program, and supply command-line arguments to see the results:
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        for (int i = 0; i < argc; ++i)
        {
            std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
        }
    }

  3. #33
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    I have named the program as "experiment" and now

    Code:
    ./experiment
    argv[0] = ./experiment
    What did make the factorial program unique compared to the original program that calculates factrorial for 9? In other words what did make the program to calculate factorials for n ?

    If we have a program that calculates factorials for n number (integer), we can combine this property of the program to other program I guess.

    Let`s imagine that a program prompts a user to enter two numbers, n and k. Then the program would use this formula




    to calculate the combination of these numbers. The program would first calculate the factorial of n (first number) like our program did. Then it would divide the result with k!(n-k)!. First it would calculate the factorial of k (second number) and then multiply the result with (n-k)!. We could mark the

    substraction n-k with a letter r, so r=n-k. Then we would only have k! multiplied with r!.
    Last edited by spacerocket; May 9th, 2014 at 05:16 PM.

  4. #34
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Square root algorithm

    Yep, something like:
    Code:
    unsigned long factorial(unsigned long n)
    {
        ...
    }
    
    int main(int argc, char* argv[])
    {
        // Get values for n and k...
    
        unsigned long result = factorial(n) / (factorial(k) * factorial(n - k));
    
        ...
    }
    P.S. The program should attempt to ensure that n > k, and that both are unsigned numbers (thus negating the possibility of dealing with negative numbers).
    Last edited by dwhitney67; May 9th, 2014 at 06:03 PM.

  5. #35
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    Code:
    #include <iostream>
    #include <sstream>
    
    unsigned long factorial(unsigned long n)
    
    {
        
    
       if (n>k) 
           return factorial (n) / factorial(k) * factorial(n-k));
           else
           return 0;
    
    }
    
    
    int main () argc, char* argv[]  // The declaration of the function, argv is the array of pointers to character. The initializer can even have no values, just the braces: argv[]
    
    {
      long number = negative;   // Takes into account the possibility number=negative
      bool have_num =false;    // This is the boolean function, possible values are true/false


    This is part of the program and it`s incomplete. Before I go any further though, I don`t understand the statement " argv is the array of pointers to a character". Is it somehow related to

    "it may be useful for a program to be able to obtain the address of a
    variable during runtime in order to access data cells that are at a
    certain position relative to it."
    Last edited by spacerocket; May 10th, 2014 at 11:06 AM.

  6. #36
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Square root algorithm

    A couple of problems:

    1. You are implementing the equation (n! / (k! * (n-k)!) in the wrong place.
    2. You are checking that n is greater than k (n > k) in the wrong place.

    You want something similar to this:
    Code:
    #include <iostream>
    #include <sstream>
    
    unsigned long factorial(unsigned long a)
    {
        if (a > 1)
            return (a * factorial(a-1));
    
        return 1;
    }
    
    int main(int argc, char** argv)
    {
        unsigned long n = 0;
        unsigned long k = 0;
        bool input_valid = false;
    
        if (argc == 3)
        {
            // Attempt to get the value n
            //
            std::istringstream iss(argv[1]);
            iss >> n;
            input_valid = !iss.fail(); // check whether iss was able to extract an unsigned long
    
            iss.clear();  // clear the status
    
            // Attempt to get the value k
            //
            iss.str(argv[2]);
            iss >> k;
            input_valid = input_valid && !iss.fail(); // combine status with previous status
        }
    
        while (n <= k || !input_valid)
        {
            std::cout << "Invalid or missing input values for n and k...\n" << std::endl;
    
            // Note:  There are insufficient checks below to ensure that the input given is valid.
            //        For example, if the user enters 'abc' instead of a number.
            //
            std::cout << "Enter n: ";
            std::cin  >> n;
    
            std::cout << "Enter k: ";
            std::cin  >> k;
    
            input_valid = n > k;
        }
    
        unsigned long result = factorial(n) / (factorial(k) * factorial(n - k));
    
        std::cout << n << "! / (" << k << "! * (" << n << " - " << k << ")!) = " << result << std::endl;
    }

    P.S. Earlier I had posted code that employed the use of good() when checking the status of the istringstream object. That was an error; I should have been checking the status with fail().
    Last edited by dwhitney67; May 10th, 2014 at 01:28 PM. Reason: Fixed the formula.

  7. #37
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    Code:
    g++ Documents/combination.cpp -o combination
    Documents/combination.cpp: In function ‘int main(int, char**)’:
    Documents/combination.cpp:53:100: error: expected ‘}’ at end of input
         std::cout << n << "! / (" << k << "! * (" << n << " - " << k << ")!) = " << result << std::endl;

    Sorry, I understand maybe 20% of the code, I have looked at that C++ tutorial, but can`t understand. How about that array question I posted earlier.

  8. #38
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Square root algorithm

    [QUOTE=spacerocket;13019679]
    Code:
    g++ Documents/combination.cpp -o combination
    Documents/combination.cpp: In function ‘int main(int, char**)’:
    Documents/combination.cpp:53:100: error: expected ‘}’ at end of input
         std::cout << n << "! / (" << k << "! * (" << n << " - " << k << ")!) = " << result << std::endl;
    You did not copy/paste the code in its entirety. You are probably missing the ending curly bracket.

    Sorry, I understand maybe 20% of the code, I have looked at that C++ tutorial, but can`t understand. How about that array question I posted earlier.
    Your questions are not crystal clear. If you had a question about an array, I did not see it. Also, just so we are clear, a statement containing a question should end with a question mark (e.g. What does a question mark look like?).

  9. #39
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    Well, is it possible that you explain what each of the functions in the code do and why they are implemented like they are.

    To the array issue, why can´t I write a 8x8 board for a chess engine? That was my original project. I think combinations are important in evaluation and search. http://chessprogramming.wikispaces.com/8x8+Board.

    I can use arrays I think. Arrays are found here: http://www.cplusplus.com/doc/tutorial/arrays/

    I would write

    Code:
    void init_board()
    {
    for (int r = 0; r < 8; ++i)
    {
    for (int c = 0; c < 8; ++i)
    {
    board[64] ={0,  8, 16, 24, 32, 40, 48, 56,
                  1,  9, 17, 25, 33, 41, 49, 57,
                  2, 10, 18, 26, 34, 42, 50, 58,
                  3, 11, 19, 27, 35, 43, 51, 59,
                  4, 12, 20, 28, 36, 44, 52, 60,
                  5, 13, 21, 29, 37, 45, 53, 61,
                  6, 14, 22, 30, 38, 46, 54, 62,
                  7, 15, 23, 31, 39, 47, 55, 63};
     ;

    Now you migh start wondering why the numbers are in this order; well I know. If you look at the chessprogramming link I posted above you can notice a similarity between the 8x8 board picture and the order of my numbers: First go up the first rank, then return to the second file and go up that and so on. It was also like this in parrots source code (not this exact code, I constructed it myself).

    That is just an example how to use arrays I guess.
    Last edited by spacerocket; May 10th, 2014 at 04:57 PM.

  10. #40
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Square root algorithm

    Quote Originally Posted by spacerocket View Post
    Well, is it possible that you explain what each of the functions in the code do and why they are implemented like they are.
    First of all, this is not a teaching forum. It is expected that you would take the time to learn the basic constructs of a programming language before delving into Tic-Tac-Toe games or other complex problems.

    I assume you know what a function is, what an array is, and how they are used. I also assume you know how to collect user input. If not, then I suggest that you find a good C++ book and start reading it at Chapter 1. During your studies don't skip chapters of the book because your interests only lie in designing software for a space rocket. Learn the basics first, then come back with intelligent questions.

    So, before you switch topics back to your other project (chess engine? or TTT?), if you have any questions regarding the "Square Root Algorithm" which some how lead to computing factorials, I would suggest that you carefully examine what your goals are.

    PS:

    Quote Originally Posted by spacerocket View Post
    I would write

    Code:
    void init_board()
    {
    for (int r = 0; r < 8; ++i)
    {
    for (int c = 0; c < 8; ++i)
    {
    board[64] ={0,  8, 16, 24, 32, 40, 48, 56,
                  1,  9, 17, 25, 33, 41, 49, 57,
                  2, 10, 18, 26, 34, 42, 50, 58,
                  3, 11, 19, 27, 35, 43, 51, 59,
                  4, 12, 20, 28, 36, 44, 52, 60,
                  5, 13, 21, 29, 37, 45, 53, 61,
                  6, 14, 22, 30, 38, 46, 54, 62,
                  7, 15, 23, 31, 39, 47, 55, 63};
     ;
    Yep, you could write that code, but good luck in getting a compiler to deal with it.

Page 4 of 4 FirstFirst ... 234

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
  •