Thread: What can I do in c++, that I can't do in c?

  1. #16
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I have also always wanted to develop a video game, but my lack of a background in physics would probably disallow me from ever creating a good game,
    Do it. Start with a simple text game, that's what I did and although there is nothing "WOW" about
    them visually, the mechanics are all there of most modern games. This is made simpler because you
    do not need to use a game engine, it's all ASCII and text files. All you worry about externally is the
    user input. As simple as it gets.
    Double Helix STL

  2. #17
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Could you elaborate on C being "mathematically intuitive"?


    Also - I would say - do not just reread your old books on C or C++.

    K&R2 as good as it was in 1988 - is quite frankly outdated now. Get a book that acknowledges C99 and C11.
    "C Programming A Modern Approach" by K. N. King - the second edition covers C99.
    "C Primer Plus" by Prata in its 6th edition also covers C11.
    Since you're not exactly new to the language - you might just as well grab the ISO C working draft and consult it while reading a book.

    As for C++ - grab "A Tour of C++" by Stroustrup and go from there. Lots of stuff changed - you want an updated resource. Look for fresh books from Meyers, Sutter and other well recognized experts.
    edit: Though, "Accelerated C++" by Koening&Moo was a good book too, in its time. What laserlight wrote isn't wrong per se - you might as well go that route. I'd go with modern first, though.

    It doesn't mean their older books aren't relevant - but I'd start with those that cover the new avenues of the language (and more modern idioms) first.
    Last edited by Xupicor; 10-16-2016 at 04:20 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Terrance
    I will have to say that one benefit of only relearning c and not c++ is that I don't want to reread all of those long c++ books, and focus my relearning process on actually coding and doing exercises. With c, I won't have to read through tons of c books to relearn all of the syntax, which means I can focus more time on doing exercises to relearn c.
    You don't need "all those long c++ books" to learn essential C++ syntax. Yes, there are plenty of esoteric techniques in C++ that in themselves can fill tomes, but for the most part they are unnecessary, i.e., mainly for special interest in building certain types of libraries, or solving problems in special ways. For example, although dated, Accelerated C++ by Koenig and Moo would allow you to cover most of what you need to know to be a competent C++ programmer in 300+ pages, with plenty of exercises for retaining the material. You can then supplement this with some online material for C++11.

    Quote Originally Posted by Terrance
    This is because c is so mathematically intuitive, that I can learn all of c's syntax merely by reading the Kerninghan and Ritchie book, and most of it will be intuitive because of its mathematical nature. But with c++, I'm sure I would have to read a lot of books to relearn object oriented programming.
    Frankly, it sounds like you're just not interested in learning the object oriented programming paradigm: it doesn't take so many books as you imagine to learn it, so that sounds like a lame excuse. Not wanting to learn a programming paradigm is fine, but you shouldn't fool yourself into believing that you're choosing C because "C is so mathematically intuitive" compared to other programming languages: my experience with maths students learning C programming is that it isn't. The syntax may look a bit "mathematical", but it isn't the same, and often students who go into C thinking it is maths get tripped up over simple things like assignment versus equality comparison. There are still things like structs and hence object-based structures to contend with. There's manual memory management, which ironically can be easily abstracted away automatically in C++, but which you have to explicitly deal with in C, unlike mathematics.

    In fact, I have the impression that the mathematically inclined tend to prefer functional programming languages because of the lambda calculus background that that family of languages has. But of course it is yet another programming paradigm: C++ does have some support for it, but C++ is more "multi-paradigm" in typical usage than object oriented or functional. You would likely be better off with a member of the Lisp family, e.g., Scheme, if you would like to focus on functional programming.

    Quote Originally Posted by Terrance
    This is what I'm looking to avoid, programming for the sheer purpose of becoming more marketable to businesses. The problem with focusing on increasing your job market value is that the real world expects you to code in a manner that is most cost-effective for the business. Thus, a business will want you to code in COBOL if that is best for the business, or vb.net if it makes reusing the code in the future easier, or focusing on something that would make you a better team player, etc.
    If you will eventually program for a business with business requirements, then you will have to deal with this sooner or later: no amount of hiding from it will save you. If you only plan to program for fun, for yourself, or for a business where you get to control everything because you're also funding it, etc, then it is not going to be a problem anyway, so you might as well just look into "what type of work you would be interested in and find out what languages are used in that field".
    Last edited by laserlight; 10-16-2016 at 03:56 AM.
    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

  4. #19
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    I've written a few games in the past, a GUI based tic-tac-toe, a GUI hangman, and several console based games, I wrote and designed a console based football game from scratch that was pretty fun, from what I remember. I may have coded a few more games, I don't remember.

    In terms of how many programming books I will read, my ultimate long term goal is to master the book The Art of Computer Programming, and I'm not sure how many years it will take me to master this book. I'm sure it will take me years to get to the point where I can read through it and fully understand the book. So I will have to read easier programming books before I get to that point.

    In terms of coding for the primary purpose if making myself more marketable to businesses, this is what I'd prefer not to do. If I wanted to make myself the most marketable as possible, I would practice strictly Java and vb.net, as these are good languages to have mastered in the real world job market. But I'd rather prefer to learn complex languages that make better use of my mathematical background. Again, I'd prefer to stay away from Matlab or haskell, as my education was more in the realm of economics and mathematics, or game theory, and game theory uses heavy amounts of arithmetic and algebra, but does not cover more complex mathematical analysis, such that my background in mathematics covers much of what is available in c.

    As far as using c, and why I think it is mathematically intuitive, my mathematics background is in economics of mathematics and game theory. This means I've studied a lot of algebraic theory, arithmetic, and mathematics of resource allocation, and all of these are the root concepts of the c language. So my mathematical education is best suited for coding in the c language, and this is why I'd like my short term focus to be on c. But again, ultimately, I'd like to read and master The Art of Computer Programming. Also, in terms of c being different from c++ from a mathematically intuitive perspective, from what I've seen thus far in relearning c++, is that it is not very mathematically intuitive based around my personal educational background in mathematics. While most c code will compile in a c++ compiler, from what I remember (though I haven't done much c++ in close to 13 years), is that actually coding in c is far different from coding in c++. From what I remember, coding in an object oriented language becomes less mathematically intuitive, while coding in a low level- function-based language like c requires much more focus on how to perform specific operations to manipulate the computer's memory resources. But again, I haven't coded in c++ in 13 years, so I may have forgotten what it is really like to code in c++. I like c because (from what I remember) is that much of the coding and algorithmic writing is intuitive to those with a heavy background in algebra and arithmetic. In other words, the c language is designed as an allocation memory language, since it works at such a low level and allows you to manipulate a computer much like assembler language does. For those of you familiar with mathematical economics, this is also what you do in math based economics, with algorithms geared towards resource allocation decisions, with much of the algorithms being based in algebra and arithmetic. So my education in economics and mathematics makes me best suited to write code in a function based- low level programming language. In fact, much of my mathematical education was obtained after learning c/c++, such that I probably geared towards mathematical economic concepts that would help me grasp certain programming theory better. So there is no doubt I'm best suited to code in c, since it is very similar to the math I have been studying over the years. And since I learned most of my math education after learning c/c++, I believe that the math I did learn covers the types of algorithms and theory that most interest me (algebra, arithmetic, resource allocation etc.). And this is most likely the types of mathematical theory that I'm best suited to work with. So I believe I am best suited to code in c.
    Last edited by Terrance; 10-16-2016 at 06:18 AM.

  5. #20
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    If you believe you're best suited to code in C - then do so. I still don't quite see what's so "mathematically intuitive" about C versus functional languages.

  6. #21
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by Xupicor View Post
    If you believe you're best suited to code in C - then do so. I still don't quite see what's so "mathematically intuitive" about C versus functional languages.
    My background is in mathematics and economics, I have studied a lot of game theory, and did a lot of difficult research in the area of mathematical modelling.

    Why c is so mathematically intuitive, is because it works at such a low level, it practically allows you to manipulate a computer at the assembler level.

    Now if you've studied mathematical economics, much of the theory is rooted in matrix algebra, where all algorithms depend on decision making and resource allocation. This is the essence of what you are also trying to do with much of c. So the necessary tools to do such, functions, variables, decision making statements, mirror much of what you learn in mathematical economics. Thus, with my educational background in math and economics, the primary syntax used in c is very intuitive for me, as I have studied and written many algorithms utilizing these types of operations.

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Terrance View Post
    Why c is so mathematically intuitive, is because it works at such a low level, it practically allows you to manipulate a computer at the assembler level.
    Could you provide a short example of a C program that is mathematically intuitive (e.g. do some simple operations on matrices). Then someone here can show you how to make it into more intuitive C++, using higher level abstractions of the same operations. For example, some matrix operations might look like this with an appropriately defined C++ type Mat:

    Code:
    Mat<float> a(5,5); // Let a be an empty 5x5 matrix. Each element can hold a float.
    a(1,1) = 1234.56;  // Set top left element of a.
    a(5,5) = 9999.99;  // Set last element of a.
    a += 10;           // Add 10 to every element of a.
    a.print();         // Print a's contents to stdout.
    That is what you can do with C++ -- abstractions of whatever concept your dealing with (matrices, etc.) can be done with a more convenient notation, and they are usually at no cost (compared to the non-abstracted version). Though there is some more programming work to be done in order to define these to work in a convenient way (as compared with just defining a bunch of procedures to carry out operations like mat_multiply, add_scalar, print_mat, etc.).

    Of course, for the matrix example, people have already written libraries already to do this, which you will probably just re-use if possible.

  8. #23
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by c99tutorial View Post
    Could you provide a short example of a C program that is mathematically intuitive (e.g. do some simple operations on matrices). Then someone here can show you how to make it into more intuitive C++, using higher level abstractions of the same operations.
    I mean, matrix algebra is pretty intuitive. You have an infinite number of possible rows, and an infinite number of possible columns (think like an Excel spreadsheet). And your goal is to manipulate the values of each of the cells.

    I don't have a specific example in mind, and again, I just started writing in c again a few days ago (though I have been writing a lot of SQL code for work).

    Anyways, in terms of purely writing algorithms, coding a small c program is fairly mathematically intuitive:

    Code:
    #include <stdio.h>
    
    main()
    {
        int i;
        int i2;
        int x[100][100];
        int rowsUp;
        int rowsAcross;
    
        printf("How many rows do you want to move up?  (Choose a number between 0 and 100)\n");
        scanf("%i\n", &rowsUp);
        printf("How many rows do you want to move across?  (Choose a number between 0 and 100)\n");
        scanf("%i\n", &rowsAcross);
    
        
        if(rowsUp >= 0 && rowsUp <= 100)
        {
            for(i = 0; i <= rowsUp; i++)
            {
                x[i-1][1] = i;
                printf("%i\n",x[i-1][1]);
            }
            
            printf("You moved %i ", x[i-2][1]);
            printf("rows up.\n");
        }
    
            if(rowsAcross >= 0 && rowsAcross <= 100)
        {
            for(i2 = 0; i2 <= rowsAcross; i2++)
            {
                x[1][i2-1] = i2;
                printf("%i\n",x[1][i2-1]);
            }
            
            printf("You moved %i ", x[1][i2-2]);
            printf("rows across.\n");
        }
    
    
    
    }
    So in other words, all of the syntax in c is fairly intuitive. Relearning functions, arrays, variables, pointers, bits, etc is not that difficult given my mathematical education. Again, I just started to relearn c again a few days ago, but all of the syntax is pretty easy to learn. And understanding the basic concepts of c is mathematically intuitive.
    Last edited by Terrance; 10-16-2016 at 11:58 AM. Reason: code update

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Terrance View Post
    I mean, matrix algebra is pretty intuitive. You have an infinite number of possible rows, and an infinite number of possible columns (think like an Excel spreadsheet). And your goal is to manipulate the values of each of the cells.
    But why would you want to do this instead of something that's much more intuitive such as what c99tutorial showed? That's still mathematically intuitive, except you don't have to do the hard work of doing the actual calculus. Besides, you are doing out-of-bounds accesses in your code, so it's undefined behaviour.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by Elysia View Post
    But why would you want to do this instead of something that's much more intuitive such as what c99tutorial showed? That's still mathematically intuitive, except you don't have to do the hard work of doing the actual calculus. Besides, you are doing out-of-bounds accesses in your code, so it's undefined behaviour.
    I am only relearning c at the moment, so my code is not that strong. Again, I like to rely on my algorithm writing skills to perform different types of operations. My ultimate goal is to read the book The Art of Computer Programming, which is very mathematical. I am doing this from more of a hobbyist perspective at the moment, but I would certainly like to be a programmer again in the future. Since my mathematical background relied heavily on studying a lot of function based algorithms, I think coding in c for the purpose of better learning algorithms would be beneficial to me.

    I had written a lot of c++/c# code in the past, but the use of object oriented programming feels a bit different than writing function based code in c. But I feel I will have to relearn c++/c# again in the future, but I'd prefer to stick with and master c first, then I can consider writing code in c++ afterwards.

  11. #26
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Terrance View Post
    The Art of Computer Programming, which is very mathematical.
    It's a multi-volume set, and only a portion of the set is mathematically oriented. Wiki link:

    The Art of Computer Programming - Wikipedia

  12. #27
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by rcgldr View Post
    It's a multi-volume set, and only a portion of the set is mathematically oriented. Wiki link:

    The Art of Computer Programming - Wikipedia
    I have volumes 2 and 3 next to me, and both are very mathematical, I can barely imagine getting through them at the moment. I believe that it is a 4 volume set, and I would guess that volumes 1 and 4 are just as mathematical as 2 and 3.

  13. #28
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Terrance View Post

    Code:
    #include <stdio.h>
    
    main()
    {
        int i;
        int i2;
        int x[100][100];
        int rowsUp;
        int rowsAcross;
    ...
            printf("You moved %i ", x[1][i2-2]);
            printf("rows across.\n");
        }
    }
    As Elysia pointed out, you have an error in this code related to array indexing. It may be illustrative to see a simple way to rewrite this program (without fixing the error) in C++ without using any other features except std::vector, which performs bounds checking. Notice that you can use the output of running this C++ program to improve (fix) your original C program. I also adjusted the output format to not scroll so much and included a simple automated testing function. This version is still incorrect, but the behaviour should at least be well-defined -- accessing an invalid index with .at(...) should cause the program to terminate.

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    // Simulate user input for testing purposes.
    int user_input(char endchar = '\n');
    
    int main()
    {
        int i;
        int i2;
        // 100-by-100 int's, each initialized to 0.
        vector<vector<int>> x(100, vector<int>(100, 0)); 
        int rowsUp;
        int rowsAcross;
     
        for(;;) {
            printf("Rows move up? (0-100 or -1 to quit): ");
            rowsUp = user_input(); // scanf("%i\n", &rowsUp);
            if (rowsUp == -1) {
                printf("Bye.\n");
                break;
            }
            
            printf("Rows to move across?        (0-100): ");
            rowsAcross = user_input(); // scanf("%i\n", &rowsAcross);
             
            if(rowsUp >= 0 && rowsUp <= 100)
            {
                printf("up { ");
                for(i = 0; i <= rowsUp; i++)
                {
                    x.at(i-1).at(1) = i;
                    printf("%i ",x.at(i-1).at(1));
                }
                printf("}\n");
                printf("You moved %i ", x.at(i-2).at(1));
                printf("rows up.\n");
            }
         
            if(rowsAcross >= 0 && rowsAcross <= 100)
            {
                printf("across { ");
                for(i2 = 0; i2 <= rowsAcross; i2++)
                {
                    x.at(1).at(i2-1) = i2;
                    printf("%i ",x.at(1).at(i2-1));
                }
                printf("}\n");
                printf("You moved %i ", x.at(1).at(i2-2));
                printf("rows across.\n");
            }
        }
        exit(0);
    }
    
    int user_input(char endchar /*= '\n'*/)
    {
        static vector<int> test_input = {
            0, 0, // 0 rows up, 0 rows across.
            1, 1, // 1 row up, 1 row across.
            2, 2,
            19, 45,
            100, 100, // Maximum valid values.
            101, 100, 
            101, 101, 
            -1,     // Quit.
        };
        static size_t next=0;
        
        if (next == test_input.size()) {
            printf("%s: No more test input! Program will abort.\n", "user_input");
            exit(1);
        }
        
        int v=test_input.at(next++);
        printf("%d", v);
        if (endchar != '\0') printf("%c", endchar);
        return v;
    }

  14. #29
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    With proper encapsulation, you can write significantly more intuitive mathematically/scientifically-oriented code.

  15. #30
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Code:
     static vector<int> test_input = {
            0, 0, // 0 rows up, 0 rows across.
            1, 1, // 1 row up, 1 row across.
            2, 2,
            19, 45,
            100, 100, // Maximum valid values.
            101, 100, 
            101, 101, 
            -1,     // Quit.
        };
    I'm receiving a compile error at the end of this piece of code.

Popular pages Recent additions subscribe to a feed

Tags for this Thread