Trying to decide which programming language I want to learn

Click For Summary
Choosing a programming language to learn can be challenging, especially for someone with a background in older languages like assembly, Fortran, and Pascal. C# and C++ are considered for firmware design, while Python is favored for its ease of use and relevance to gaming, particularly with grandchildren. C++ is noted for its speed and compactness, making it suitable for game programming, but it may require more effort to learn compared to C#. Resources like Visual Studio for C# and various online tutorials can help beginners get started, while microcontroller programming can be explored through platforms like Arduino and Raspberry Pi. Ultimately, the choice should align with personal interests in scientific or gaming applications.
  • #91
yungman said:
I copied the file I input according to the example in the book. The question only want to read the values in both arrays. That's NOT the question I want to ask. My question is about constexpr that making the moreNumber(24) , a 24 element array.
No, moreNumbers is an array of 25 ints, and myNumbers is an array of 5 ints. Arrays in C and C++ have indexes ranging from 0 to (number of elements - 1).
yungman said:
The book does not talk anything or the syntax on how to create constexpr on array, I just very interest in learning. I tried google, all the example have too many terms I have not learned, so they don't make sense to me. Because of potential error if I write and read back to say moreNumbers(20). If the array is defined wrong, this would be out of bound, but the compiler would not flag out of bound( according to the book). So even if I read back the correct value, that doesn't verify the moreNumber() is created correctly.
You're not creating constexpr on an array -- you're just calculating a number and using it to declare an array of that size. If myNumbers array is defined to have N elements, the moreNumbers array will have ##N^2## elements, This keyword is a fairly new addition to C++, having been added to the C++ 11 standard, and revised a bit for the C++ 14 standard.

Regarding your program, keep in mind that the myNumbers array has only 5 elements (indexed 0 through 4), so if you enter an index larger than 4, you will be storing a number in the myNumbers array well beyond its last element, potentially overwriting something else.
yungman said:
If you look at line 3 and line 10, Something really looks wrong to me. line 3 defines
line3: constexpr int Square(int number) { return number * number; }
line10: int moreNumbers[Square(ARRAY_LENGTH)];
How is line 3 set up for line 10? Can anyone explain this to me?
ARRAY_LENGTH is a constant -- 5, so line 10 calculates the value Square(ARRAY_LENGTH) as 25 at compile time. So the moreNumbers array is declared as an array of type int with 25 elements.
 
  • Like
Likes yungman, pbuk and sysprog
Technology news on Phys.org
  • #92
You can read and write to moreNumbers[20] quite happily because 20 < 25. But if when you run your program you write to moreNumbers[25] your program will not work properly. The compiler cannot do anything about this because it finished its job when it compiled your source code a few minutes, hours or years ago. Checking that the index used to access an array is valid must be done at run time. Some language runtimes do this for you automatically but this makes the code run more slowly and because C++ is designed for maximum performance it does not do this. That means you need to make sure yourself that you only ever use a valid index.
 
  • Like
Likes sysprog
  • #93
FactChecker said:
My advice is to keep it simple and start with C.
You can start much easier with C, but if you insist on starting with C++ then you should not complain when you run into a lot of unfamiliar terms and concepts. The entire basic C language is concisely defined in a short book by Kernighan and Ritchie. On the other hand, C++ books are many hundreds of pages where the concepts are usually scattered all throughout, and incompletely explained in anyone part.
I think the book is not good, it did say you don't have to have any experience( of cause they say that). All I need is the syntax of constantexpr ARRAY(). My guess is constantexpr ARRAY() { some function}. I just need this.

Maybe I should buy another C++ book.
 
  • #94
To fully understand C++, there are a lot of things to learn that books must address. It is very hard to do. The syntax should be helped by the work environment that you use. Once you start the line of code, it should suggest valid syntax alternatives. Follow that guidance. Your Visual Studio should be doing that. If yours is not, you might want to get someone to set VS up properly for simple programs.

It is also true that Microsoft VS is designed to work for all programmers in the Windows operating system, including professionals, and can be very complicated. You might want to start with a simple standard window or terminal user interface.
 
  • #95
FactChecker said:
It is also true that Microsoft VS is designed to work for all programmers in the Windows operating system, including professionals, and can be very complicated.
But it ensures that you are working in a known environment designed by Microsoft to work within their operating system.

FactChecker said:
You might want to start with a simple standard window or terminal user interface.
And then instead of talking about C++ we would be talking about Windows PATH and other environment variables, the difference between Command Prompt and Powershell, Administrator and normal terminal sessions...
 
  • #96
pbuk said:
But it ensures that you are working in a known environment designed by Microsoft to work within their operating system.
Yes.
And then instead of talking about C++ we would be talking about Windows PATH and other environment variables, the difference between Command Prompt and Powershell, Administrator and normal terminal sessions...
All that can not be avoided no matter what approach is used. It may appear in a different way, but it is all still there.
 
  • #97
Mark44 said:
No, moreNumbers is an array of 25 ints, and myNumbers is an array of 5 ints. Arrays in C and C++ have indexes ranging from 0 to (number of elements - 1).
You're not creating constexpr on an array -- you're just calculating a number and using it to declare an array of that size. If myNumbers array is defined to have N elements, the moreNumbers array will have ##N^2## elements, This keyword is a fairly new addition to C++, having been added to the C++ 11 standard, and revised a bit for the C++ 14 standard.

Regarding your program, keep in mind that the myNumbers array has only 5 elements (indexed 0 through 4), so if you enter an index larger than 4, you will be storing a number in the myNumbers array well beyond its last element, potentially overwriting something else.
ARRAY_LENGTH is a constant -- 5, so line 10 calculates the value Square(ARRAY_LENGTH) as 25 at compile time. So the moreNumbers array is declared as an array of type int with 25 elements.
Thanks for you response

So the syntax: int moreNumber[ Square( ARRAY_LENGTH ) ]; is correct?
This looks like it's a constant array of 25 elements. Which has NOTHING to do with constexpr at all.

So what is line 3 constexpr int Square(int number) {return number*number} for?

that's my whole confusion. doesn't seems like I need line 3. The book say nothing about this. I am starting to think this is not a good book, throwing out stuffs it has not covered.

Maybe I should buy another one as backup. Nothing so far is complicate or complex, I just need a simple syntax instead of guessing like what I am doing. I don't buy that because it's a C++ book that it doesn't cover the basics in C. I just went through 60 pages of the book describing the simplest of the simple syntax, then all of a sudden skip the more complicate one like this. The next example show to look for the size of the array with like moreNumber.size() without even explain about it also.

Thanks for your help.
 
  • #98
FactChecker said:
To fully understand C++, there are a lot of things to learn that books must address. It is very hard to do. The syntax should be helped by the work environment that you use. Once you start the line of code, it should suggest valid syntax alternatives. Follow that guidance. Your Visual Studio should be doing that. If yours is not, you might want to get someone to set VS up properly for simple programs.

It is also true that Microsoft VS is designed to work for all programmers in the Windows operating system, including professionals, and can be very complicated. You might want to start with a simple standard window or terminal user interface.
Yes, I never notice if I huffer around the syntax, VS does suggest something. I am going to follow the links. That's for bringing this up, I did not know that, I thought it was just helping me to spell the syntax.

Call me ignorant, VS is not my problem so far, I can write programs, save, run programs so far, I have completed almost 10 programs already.

My issue is mainly on the Syntax, I don't think needing to learn C before C++ is an issue. I just went through 60 pages of all the most basic definition on syntax of the basic stuffs, it explain to the nth degrees detail. It just all of a sudden throwing out this with no explanation that throw me off. Like in the next example, it uses int ARRAY.size() to look for the size without even talking about this before and after the example. The book just missing it.

Any good book you can suggest?

Thanks
 
  • #99
yungman said:
Thanks for you response

So the syntax: int moreNumber[ Square( ARRAY_LENGTH ) ]; is correct?
This looks like it's a constant array of 25 elements. Which has NOTHING to do with constexpr at all.
It's not a constant array. If it were, you wouldn't be able to change the values of any of its elements. constexpr in line 3 creates a constant that is the square of another constant.
yungman said:
So what is line 3 constexpr int Square(int number) {return number*number} for?
It essentially defines a function that is evaluated at compile-time, rather than at run-time, where functions are typically evaluated.
yungman said:
that's my whole confusion. doesn't seems like I need line 3. The book say nothing about this. I am starting to think this is not a good book, throwing out stuffs it has not covered.
You need line 3 because it defines what happens in line 10. The program wouldn't compile without line 3.
yungman said:
Maybe I should buy another one as backup. Nothing so far is complicate or complex, I just need a simple syntax instead of guessing like what I am doing. I don't buy that because it's a C++ book that it doesn't cover the basics in C. I just went through 60 pages of the book describing the simplest of the simple syntax, then all of a sudden skip the more complicate one like this. The next example show to look for the size of the array with like moreNumber.size() without even explain about it also.
The arrays you have in the program we're discussing here are ordinary C-style arrays. If you have an example that uses moreNumber.size(), they must have declared the array differently, using the Standard Template Library (STL) array template. Here's an example of what I'm talking about.
C:
#include <array>
.
.
.
array<int, 4> ai = { 1, 2, 3 };
int size = ai.size();           // size is set to 3
If they jumped from C-style arrays to STL container classes with little or no explanation, this is probably not the best book to learn from.
Although C++ builds on many C concepts, most C++ books are not going to teach you C before presenting material on C++.
 
  • Like
Likes yungman
  • #100
Mark44 said:
It's not a constant array. If it were, you wouldn't be able to change the values of any of its elements. constexpr in line 3 creates a constant that is the square of another constant.
It essentially defines a function that is evaluated at compile-time, rather than at run-time, where functions are typically evaluated.
You need line 3 because it defines what happens in line 10. The program wouldn't compile without line 3.
The arrays you have in the program we're discussing here are ordinary C-style arrays. If you have an example that uses moreNumber.size(), they must have declared the array differently, using the Standard Template Library (STL) array template. Here's an example of what I'm talking about.
C:
#include <array>
.
.
.
array<int, 4> ai = { 1, 2, 3 };
int size = ai.size();           // size is set to 3
If they jumped from C-style arrays to STL container classes with little or no explanation, this is probably not the best book to learn from.
Although C++ builds on many C concepts, most C++ books are not going to teach you C before presenting material on C++.
You have any suggestion of book? Yes, this is only the first one, the following examples are not any better. It talk about Dynamic Array and just said read the program. It said the syntax has not been explained yet! That's not comforting at all. Then why put out an example? I want to chew on every line to understand it! Now the learning is to a grinding halt! It's not hard by any measure, just a simple syntax and a line of explanation will go a long way.
 
  • #101
This is why I would recommend that you start with C or some other language like Python. They will be a lot more fun and easy to learn. Here is some documentation for constexpr. Even as a professional programmer, I would not like to read it. C++ is full of things like that. The only thing I would miss in C is object-oriented programming. There are a lot of languages that allow OO programming.
 
  • #102
Last edited:
  • #104
FactChecker said:
If all you need is the documentation, then what is wrong with this? constexpr specifier (since C++11)
I hope if I have 4 books, one of them is going to have answers for me. Like you and other said, no one book is going to explain all the syntax, but one of them hopefully will have the answer for me so I don't have to keep asking simple question here.

Thanks
 
Last edited:
  • #105
Sorry guys, I got it. I was confused. I originally thought line 3 is part of the syntax to define the constexpr of the array in line 10. I see line 3 is just defining of the variable Square. Line 10 just use the variable to put it in a CONSTANT array.

Half a day of walking away from the problem gives me a fresh eye to look at it again.
C++:
#include<iostream>
using namespace std;
constexpr int Square(int number) { return number * number; }

int main()
{
    const int ARRAY_LENGTH = 5;

    int myNumbers[ARRAY_LENGTH] = { 5, 10, 0, -101, 20 };
    int moreNumbers[Square(ARRAY_LENGTH)];

    cout << "Enter index of the element to be changed: ";
    int elementIndex = 0;
    cin >> elementIndex;

    cout << "Enter new value: ";
    int newValue = 0;
    cin >> newValue;

    myNumbers[elementIndex] = newValue;
    moreNumbers[elementIndex] = newValue;

    cout << "Element" << elementIndex << "in array mynuNumbers is: " << myNumbers[elementIndex] << endl;

    cout << "Element"<< elementIndex <<" in array moreNumbers is:" << moreNumbers[elementIndex] << endl;

    return 0;
}

Sorry for all the troubles
 
Last edited by a moderator:
  • #106
One thing that you should realize is that the documentation of C++ is so large that on-line and computerized documentation has huge advantages and might be the only practical way to do it. They can include hyperlinks, search tools, and syntax-dependent recommendations that no number of books can match.
 
  • Like
Likes sysprog
  • #107
FactChecker said:
One thing that you should realize is that the documentation of C++ is so large that on-line and computerized documentation has huge advantages and might be the only practical way to do it. They can include hyperlinks, search tools, and syntax-dependent recommendations that no number of books can match.
I use google search on everything, but actually to my big surprise, I find searching on line is not helpful for me in C++. This is because I am barely starting to learn, all the links are from like forums where people are much more advance than me. When I read their programs, there are more terms that I don't know than I know. So the programs don't even make sense to me. I don't see and simple explanation on anything.

On line search is very useful if someone already know the subject and just have a specific question. I am surprised like if I have question on very basic electronics, you actually can find article teaching you what is V=IR. But I yet to see any on C++ that get down to this fundamental stuffs. So it's worth to spend extra money to get a few textbooks.

Actually, it's really not the book this is the problem this time, I must be more tired than I realize. I just got stuck in something really simple. I thought both statement ( line3 and line 10 are one syntax), but actually line 3 is just simple definition of a constant(Square), then the constexpr just use that constant to define the size of the array. It's just that simple and I was to blind to see it. After bickering here and wrote a bad review on the book instead of keep working on the program, this morning, when I look at the codes, it just dawn on me that I was stuck in the wrong direction! Now I still have to go and delete my review on the book!

Now I know better, don't rush, don't just keep at it more than 3 or 4 hours a day. I don't have a deadline. I actually read and understand the rest of the Chapter 4 with strings with no problem. I am not in the 20s like before that I can just keep at it anymore.
 
  • #108
You can start with your own simple programs and limit how much of C++ you want to get into. If you are trying to understand other people's code, then they might take you down every C++ rabbit hole that they put in their code. The example code using constexpr is like that. I gave you a link to the documentation on constexpr. That is the best I can do. If you can understand that documentation, then you are doing good. If you can not, then you may want to change your approach so that you can limit the parts of C++ that you need to learn and use. C++ code can be as simple as C, or it can be extremely complicated. Do not expect the complete documentation of C++ to be understandable without years of study and hard work.
 
  • #109
I have a question in this program. I am playing with adding something in the same program I had issues... I added a line to check the size of the array "moreNumbers[]". That is line 27. I learn it from the character string section to check the length of an array by command sizeof(Array).
C++:
//Assign Values to Elements in an Array
#include<iostream>
using namespace std;
constexpr int Square(int number) { return number * number;}// set constant function Square.

int main()
{
    const int ARRAY_LENGTH = 5;

    int myNumbers[ARRAY_LENGTH] = { 5, 10, 0, -101, 20 };
    int moreNumbers[Square(ARRAY_LENGTH)];// replace the constant number for length with a constant function.
   
    cout << "Enter index of the element to be changed: ";
    int elementIndex = 0;
    cin >> elementIndex;

    cout << "Enter new value: ";
    int newValue = 0;
    cin >> newValue;

    myNumbers[elementIndex] = newValue;
    moreNumbers[elementIndex] = newValue;

    cout << "Element" << elementIndex << "in array mynuNumbers is: " << myNumbers[elementIndex] << endl;

    cout << "Element" << elementIndex << " in array moreNumbers is: " << moreNumbers[elementIndex] << endl;
    cout << "The size of moreNumbers[] is: " << sizeof(moreNumbers) << endl;
    return 0;
}

If you run the program, it will return the size is 100 instead of 25. What did I do wrong?
 
  • #110
In your definition of the square, write

return (number*number)

that might be the issue.
 
  • #111
sizeof returns the size in bytes and there are 4 bytes per int
 
  • Like
Likes FactChecker and yungman
  • #112
Jarvis323 said:
sizeof returns the size in bytes and there are 4 bytes per int
How can I forgot that!

Thanks
 
  • #113
Jarvis323 said:
sizeof returns the size in bytes and there are 4 bytes per int
I even modified the array by declaring the array is characters: char morNumbers[]

The length now say it's 25 as characters are 1byte long only.

Thanks
 
  • #114
Jarvis323 said:
there are 4 bytes per int

8 bytes if you're compiling 64-bit code. (And way back in the Stone Age there was code where an int was only 2 bytes, or even 1 byte.)
 
  • #115
I don't think that one byte is large enough to hold the numbers that you want to put in it. newValue is an integer and needs 4 bytes. You can always leave the array large enough for an integer and print sizeof(Array)/sizeof(int).
 
  • Like
Likes yungman
  • #116
FactChecker said:
I don't think that one byte is large enough to hold the numbers that you want to put in it. newValue is an integer and needs 4 bytes. You can always leave the array large enough for an integer and print sizeof(Array)/sizeof(int).
I am just verify the line sizeof() if I change to character as it's 1 byte. Just playing around. I am playing around with the example given by the book by adding things and try it out. It's been fun.

Thanks for the new syntax for integer ( 4 bytes)

I am not at the point of designing and writing my own program, so I just take the sample program, adding stuffs to try to get familiar with the terms and the syntax. I am finishing up chapter 4 tonight, I'll be working with chapter 5...Operators, expressions and statements tomorrow. That's should be a lot more interesting.
 
  • #117
Jarvis323 said:
there are 4 bytes per int
PeterDonis said:
8 bytes if you're compiling 64-bit code. (And way back in the Stone Age there was code where an int was only 2 bytes, or even 1 byte.)
An int is 4 bytes (currently) whether you're compiling 32-bit or 64-bit code. That is definitely the case with Visual Studio, and is probably the case with other compilers as well. The long long (AKA long long int) type is 8 bytes, as is unsigned long long.

When I was first learning C, in 1985, an int was the same size as a short (2 bytes), but all that changed along about 1995 or so. I'm not aware of any machines and compilers for which an int was only one byte. Not saying it didn't happen, but Kernighan and Ritchie were designing a language to be used under Unix, and surely the machine word size was greater than 8 bits.
 
  • #118
yungman said:
I am just verify the line sizeof() if I change to character as it's 1 byte. Just playing around. I am playing around with the example given by the book by adding things and try it out. It's been fun.
Thanks for the new syntax for integer ( 4 bytes)
That's not really syntax -- it's just the size (in bytes) of that data type. You can use the sizeof operator (it's considered an operator, like +, -, *, /, etc., rather than a function) to determine the size of any type or any variable.

When you do more with arrays (the C-style arrays as opposed to the C++ Standard Template Library array template) that are initialized, you can use sizeof to count how many elements there are in the array.

C:
int List[] = {2, 5, 8, 10, 11, 31, -2, 15, 25};

int size = sizeof(List) / sizeof (int);
When this code is run, size will be set to 9. sizeof(List) == 36, and sizeof(int) == 4, so the quotient gives the number of array elements. Similar code will work whatever the base type of the array is: int, short, float, double, structure type, and so on.
 
  • Like
Likes yungman and FactChecker
  • #119
EDIT: I changed "function" to "operator". Thanks, @Mark44 .
The sizeof() function operator can be very useful sometimes. If you make a structure that is a combination of characters, integers, floats, and smaller structures, the computer will often insert padding space to make things align for efficient use. It is tricky to know how big the final result is. The sizeof() function operator can tell you that.
 
Last edited:
  • Like
Likes yungman
  • #120
Anyone know how much one semester in the college C++ class covers? I am using this book:
https://www.amazon.com/gp/product/0789757745/?tag=pfamazon01-20

Seems like it's a lot to cover the whole book in one semester, I just want to know what chapters I have to cover to equal to one college semester. You can see the description of the chapters if you look inside the book in the Amazon link.

Thanks
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 133 ·
5
Replies
133
Views
10K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
16
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 58 ·
2
Replies
58
Views
4K