FYI: If you get a compile error, it helps to post the error message you got.
Tim S.
FYI: If you get a compile error, it helps to post the error message you got.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
I have to say, C being mathematical is debateable. People with maths degrees writing programs tend to write in python, matlab, things like this... don't let your own biases completely close off what you learn. Much of the point in the thread so far has been you would learn and do much more picking another language and forgetting how "bare metal" C is.
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 intialized 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): "); for( i = 0; i <= rowsUp; i++) 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(1).at(i2 - 2)); printf("rows across.\n"); } } exit(0); } int user_input(char endchar /*= '\n'*/) { static vector<int> test_input = { 0,0, //rowsup, 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; } Output: In function 'int user_input(char)': Line 56: error: scalar object 'test_input' requires one element in initializer compilation terminated due to -Wfatal-errors.
It's not that I've never learned languages other than c. I have learned c++, c#, Visual Basic and vb.net, javascript, Java, and SQL. But I believe my educational background is best suited to allow me write in c at the moment. And I don't have a math degree, I actually have 2 business degrees. But I have studied tremendous amounts of mathematics, particularly game theory, in which I put in close to 2000 hours of research. I also studied and coded in c/c++/c#, putting in over 1000 hours before going to college.
Now that I'd like to be a professional programmer again, I would like to start off by mastering c again. While I realize c is bare metal, in terms of how you have to use it, I would still like to master c before moving to another language. Further, if I wanted to study other languages, such as python, I would probably be best off taking some kind of coding bootcamp that specializes in teaching the currently hot languages in today's job market. This is because there will always be boot camps that will teach students languages that are currently popular in the job market.
I got something completely different when I compiled your code?
Try looking at the line above this one.Code:prog.cpp: In function 'int user_input(char)': prog.cpp:61:9: error: expected ';' before 'exit' exit(1); ^
Did you compile with the option to use C++14? It is not used by default in many compilers or online compilers either.
Last edited by whiteflags; 10-16-2016 at 04:39 PM.
I compiled the code with an online compiler.
Did you compile with the option to use C++14? It is not used by default in many compilers or online compilers either.
Aside from the missing semicolon:C++11 mode is enough to compile that.Code:$ g++ --std=c++11 --pedantic -Wall -Wextra -Wfatal-errors xx.cpp xx.cpp: In function 'int main()': xx.cpp:16:9: warning: unused variable 'rowsAcross' [-Wunused-variable] int rowsAcross; ^ xx.cpp:38:51: warning: 'i2' may be used uninitialized in this function [-Wmaybe-uninitialized] printf("You moved %i ", x.at(1).at(i2 - 2)); ^
This error is probably caused by compiling in C++98 mode. For the user_input function to work in C++98 mode, an initializer list cannot be used. Instead you could initialize the test_input vector in the following C++98 way:
NELEMS is a convenient macro for such situations and is defined as followsCode:static const int arr[] = { 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. }; const vector<int> test_input(arr,arr+NELEMS(arr));
Code:#define NELEMS(arr) (sizeof(arr)/sizeof(*arr))This error is caused by a missing semicolon on the previous line (looks to be a typo).prog.cpp: In function 'int user_input(char)':
prog.cpp:61:9: error: expected ';' before 'exit'
exit(1);
^
You should be aware that due to optimisations applied by modern compilers even at the lowest optimisation levels, this is somewhat of an illusion unless you actually do dip into assembly.Originally Posted by Terrance
It seems to me that you made up your mind before starting this topic, and no amount of reasoning is going to persuade you otherwise because you are utterly convinced even before any argument has been made that C is most suited to your particular mathematical background. Hence, stop wasting your time trying to justify yourself on the C++ programming forum as to why you would choose to (re)learn C; focus on learning/mastering C instead. After that, if you would like to (re)learn other programming languages, perhaps even C++, go ahead. In the end, once you're back in the game with C, you'll have re-established the programming background to more easily learn other programming languages.Originally Posted by Terrance
Last edited by laserlight; 10-17-2016 at 12:17 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why don't you consider Matlab as a complex language that makes better use of mathematical background? Another complex mathematical language called APL (A Programming Language) dates back to the 1960's. Here's an example of Conway's somewhat complex game of life implemented with a small amount of code in APL:
Conway's Game Of Life in APL - YouTube
Not quite, there aren't many languages that include support for some of the SIMD / SSD instructions. Here are a pair of links, the first describes a fast CRC algorithm, the second is assembly code of an enhanced version from Intel that "folds" 128 bytes at a time. Note - the comment in the assembly code for rk3 and rk4 are wrong, the comments should be rk3 == (2 ^(32*31)) mod q, and rk4 == (2 ^(32*33)) mod q. I converted the assembly to Microsoft Masm / ML syntax to work with Visual Studio, to confirm that the code works.
http://www.intel.com/content/dam/www...lqdq-paper.pdf
isa-l/crc16_t10dif_01.asm at master * 01org/isa-l * GitHub
Last edited by rcgldr; 10-17-2016 at 01:25 AM.
I don't have a mathematics degree, so my background in mathematics is specialized around game theory, which goes deeply into matrix algebra, and Matlab does not necessarily suit me well in terms of my mathematical background. Also, I have never learned Matlab in the past, so it would take me considerably longer to learn Matlab over c. Further, I have already learned c in the past and it suits my mathematical training well.
Last edited by Terrance; 10-17-2016 at 01:55 AM.
If I showed you some linear algebra that I wrote in C++, would it help change your mind? I've been thinking of ways to improve the interface of the classes and functions as well.
Also, someone's had to have mentioned it by now but there _is_ a language literally based off of category theory.
I'd probably be the least useful person to help with improving your code, but I'd be interested to see it, MutantJohn.
(I've mentioned Haskell, which you may have in mind... OP isn't interested. Somehow.)
Last edited by Xupicor; 10-17-2016 at 10:46 AM.
Oh awesome. I'll post some code later tonight when I have time to explain my problem domain and my methodology.
And yes, I was referring to Haskell. I'm glad you mentioned it then! I kind of skimmed the thread lol but I got the gist. The thing is, C and C++ map naturally to scientific math, imo. To me, pure math is more what Haskell attempts to do with its roots in category theory.