Trying to decide which programming language I want to learn

In summary: C++ is considered a lower-level language, meaning that it is closer to the hardware and gives you more control over how your program runs. However, C# is a high-level language, which means it is easier to learn and use and has a lot of built-in libraries that make coding faster and more efficient. Both languages have their advantages and disadvantages, and it ultimately depends on what type of project you are working on. For gaming, C++ is often preferred because of its speed and control, but C# has become increasingly popular in game development as well. It's worth trying both and seeing which one you prefer working with.
  • #71
yungman said:
I speak too soon, I do have a question. I am going through all the examples in the book, the book tends to build on the last example for the next. Meaning the next example is using like 90% of the code of the last example. It would be very convenient to save the program of the last example and rename as new program so I don't have to retype the whole thing.

I look at "file", there is no "Save as" option that I can save the program in another name. Is there any way to do this?

Thanks
It's called "Save File As" in MS Visual Studio.

If you use Notepad++, you can choose "Save As" or "Save a Copy As" ##-## the latter let's you make multiple versions while keeping the file open with the prior name.
 
Technology news on Phys.org
  • #72
sysprog said:
It's called "Save File As" in MS Visual Studio.

If you use Notepad++, you can choose "Save As" or "Save a Copy As" ##-## the latter let's you make multiple versions while keeping the file open with the prior name.
Actually I referred to saving the whole project. Yes, there is a save .cpp, but that only save one file, I have to go in and create a new folder and put it in before working with the .cpp. Do they have a way to say the all the source files like when they create the new project?

Thanks
 
  • #73
yungman said:
Do they have a way to say the all the source files like when they create the new project?
File -> New -> Project from existing code

But I wouldn't bother for this purpose, do you really need to save your "Hello World" code for posterity? Just work through the early exercises modifying as you go.
 
  • #74
pbuk said:
File -> New -> Project from existing code

But I wouldn't bother for this purpose, do you really need to save your "Hello World" code for posterity? Just work through the early exercises modifying as you go.
No no, I way passed that 2 days ago, this is writing a local function and how to declare global variables. It's like the program showed in post #67. I'll try the new project from existing code.

thanks
 
  • #75
This is just a comment but I feel very strongly about this. In my days, we do everything in HEX number, it is so so much more intuitive. Studying about ASCII and reading the book remind me that now they use decimal numbers, not HEX or binary anymore. In my days, we are trained to work in HEX or binary because that's how the computer thinks, you actually can see the bits how the signal is. For hardward, this is so important to think in HEX. Decimals don't mean a thing. I don't know why it's so hard for people that they have to change to decimal particular doing firmware ( which is a big big field). What is 65535? If it is 0FFFFH, you know right away it's all 16 1s! I just hate it when I design the controller hardware and the programmer talk to me in decimals. Tell me the HEX, I immediately know what signal is what.

I used to program in machine language for Z80 and just type away in HEX to test hardware. People really need to tuff it up and stick the HEX, can't think and do HEX, there might be other career that's better.
 
  • #76
yungman said:
Studying about ASCII and reading the book remind me that now they use decimal numbers

Most programming languages have a syntax for expressing numbers in hex instead of decimal. For example, in Python you can say 0xFFFF instead of 65535.
 
  • #77
PeterDonis said:
Most programming languages have a syntax for expressing numbers in hex instead of decimal. For example, in Python you can say 0xFFFF instead of 65535.
I know the programmers I worked with all these years use C ( don't know C++ or whatever), they talk decimals. I sure hope people use languages for firmware do HEX.

I am reading the book on short, long, long long and gives all the decimal numbers, I have to search and finally found it's just 2bytes, 4bytes and 8bytes resp! Just say it! It's not that hard to learn, suck it up!
 
  • #79
jtbell said:
Same in C++. There are also similar notations for octal and binary integer literals.

https://en.cppreference.com/w/cpp/language/integer_literal
Problem is you give people the option, they take the easy way and use decimal.

It's like for an 8bit controller and write to an 8 bit port to control 8 different event. If I write 05AH, I know immediately it's 01011010, I know right away what condition I am driving. If you tell me 90 in decimal, what is that? Hell, I had to use google to translate 05AH to get 90!
 
  • #80
As has been mentioned, most programming languages support writing and printing integers in different bases (decimal, hex, and octal are the ones usually supported). You're meant to just use whichever one is most natural for what you're doing. So typically you'd use hexadecimal for low-level computer related things (e.g. memory addresses) where powers of 2 tend to have special significance or you see integers as blocks of bits, and decimal for more everyday things (e.g., number of employees on your payroll).

C++ can print in different bases, although the way iostream handles this is really ugly:
C++:
#include <iostream>

using namespace std; // protect code from std prolifiration.

int main()
{
    int x = 0x2A;

    // Save state of cout stream.
    ios_base::fmtflags flags(cout.flags());

    cout << hex << uppercase << x << " in hex is "
         << dec << x << " in decimal." << endl;

    // Restore cout to its initial state.
    cout.flags(flags);
    
    return 0;
}
 
  • #81
What you should use is mostly dependent on what you want to do with it.

One caveat about C languages is that the syntax can get really cryptic and it's so flexible there's a dozen ways to do every task with some far more appropriate than others, so I'd recommend liberally commenting your code to keep track of what you wrote. Also for anything performance-related it pays to know something about the compiler because the size of the code and speed it executes is highly dependent on how the source code is written. I'm sure the compilers are much better with the error-checking now but it probably still pays to keep the code simple and concise to avoid loose pointers and non-terminating loops unless you are focusing on speed of execution and then you'll be writing a lot more inline code to give the compiler more explicit direction or reverting to assembler for hardware-level interface and mathematically intense computation where error checking in the compiler slows down the object code. I'd recommend avoiding the temptation to get clever with tightly condensed C code that is impossible to read and compiles poorly just to save ascii characters in the source and prove your intelligence.

If you are writing first-person-shooter video games with fast action for reflex response time you probably won't be writing such games as a hobby because they are difficult to optimize and all those worries about garbage collection slowing things down don't apply otherwise.

If you are writing role-play games and kid's games or web-based games you will probably end up using Python, Java, or some other modular stickum for speed of development and ease of debug because there's no point in re-inventing the wheel.

If you are doing embedded systems on microcontrollers, you might toss that whole idea of a Windows IDE in favor of a dedicated solution like pi. Honestly since I never did that personally I don't know. I'm just trying to point out that writing games for your grand kids is not the same thing as writing microcode for an embedded controller. You will probably end up with two different development environments and working in entirely different programming languages like the 'real' programmers do.

For GPIB just about anything works because it's so slow anyway and it makes sense to use somebody else's interface library if you can rather than build it yourself.

I'll second the shout-out to Linux. For decades Microsoft programmers used Linux to develop Windows. Linux is industry standard for embedded development AFAIK plus the OS and development tools are available in free and open source versions (FOSS) so you won't have to invest a fortune just to get up and running or spend your time in a Microsoft sandbox environment (I mean the consumer type for kiddies, not the development type for security). Engineers I worked with used Java for web compatible user documentation and the embedded developed in C++/assembler. We used python for quick development of test fixtures with simple to code syntax that technicians can readily understand, modify, and operate interactively via interpreted mode execution on the manufacturing assembly line.

If you just want to build some simple games for your grandkids, I'd stick with a pre-made Windows solution and nix all the artsy. It won't be fast or elegant but it will be simple to learn and you won't need to create your own integrated development environment from scratch on the command line or worry too much about the math or the hardware. The existing libraries will handle all of that for you and you can find workarounds for speed of execution online if you need them.

If I'm off base people feel free to jump in. I didn't actually do much of this stuff personally and I've got strictly an overview perspective from the outside. I did most of my programming in Unix shell scripting for massaging the output files of one design tool as input to another tool and most of that was simple syntax transformation or report generation. I did a little C and assembler also but it was minimal. I also did some HP Basic for HPIB/GPIB interface and that's an entirely different ballgame dedicated to an instrumentation environment for automated testing and control. The most sophisticated C program I wrote drew a Mandelbrot and it was incredibly slow compared to the versions that were out there doing all those fractal graphics in the 1990s so the concern about speed is real when it comes to mathematically intense graphics and not something to be dismissed lightly. The programming language may be less important but the way you write your code has a tremendous impact on how fast it executes and I didn't know anything about it so I wrote turtle code.
 
  • #82
I studied 60 pages, 3 chapters. This is the notes I have in attached file. 60 pages of these! The next chapter is Managing Arrays and Strings! I am still waiting to get to logic function, condition statements to really write some interesting stuffs. This is so boring.
 

Attachments

  • C++ notes.docx
    15 KB · Views: 113
  • #83
Notes aren't bad, just a couple of things
yungman said:
* C++ is Object Oriented Language.
* Compiler converts .cpp into machine language called Object Files.
The word "Object" refers to two completely different concepts in those two statements.

yungman said:
*using namespace std to avoid repeating std:: on every line.
*using std::cout; using std::cin at the beginning so don’t have to repeat for the rest of the program.
That's an either/or, you don't do both. I prefer the latter.

yungman said:
-#define: #define pi 3.1416
A define is different from a constant: the former basically saves you from typing 3.1416 every time you want to enter this literal value in your source code so the compiler can compile it whereas the latter saves the value 3.1416 in some location in memory to be used at run time.
 
  • #84
pbuk said:
Notes aren't bad, just a couple of things

The word "Object" refers to two completely different concepts in those two statements.That's an either/or, you don't do both. I prefer the latter.A define is different from a constant: the former basically saves you from typing 3.1416 every time you want to enter this literal value in your source code so the compiler can compile it whereas the latter saves the value 3.1416 in some location in memory to be used at run time.
Each of the statement after the "*" is a completely independent statement, they have no relation to each other. I use * to say it's a separate bullet point.

I feel so far, it's like looking at all the cars, programs and everything computer related products, they all try so hard to make it user friendly, as much freedom of style to express the same thing, making it easy for people that doesn't have the neck of computer to use computer. That it have so many different ways of doing the same thing.

The most ridiculous one is the typeof! If you don't like the standard definition "long long" for 8byte data length, you can use:

typeof long long int bossLecturing;// so I can use bossLecturing to define long long int.

It is very common 3 or 4 programmers working on the same project, each write a portion of the program. If anything goes wrong after integration and need to debut the whole thing. Someone need to read all the sub programs of different programmer.

Think of all three programmer defined the same variable by each chose to use typeof, one use bossLecturing, one use wifeNagging, one use motherinlawTalking. All to define long long. Can you imagine how confusing this can get?

What's wrong with using straight dedicated words, no option. Then everyone understand exactly what it means instead of having to go read the declaration in the program? What's wrong that the codes look like a computer code instead of looking like English?

Maybe I am old, this whole 60 pages can be summed up in 5 pages if all these nonsense get eliminated. It all can be learn in one afternoon instead of reading 60 pages carefully to make sure it's all nonsense! I still think people either have it or don't. If anyone need the codes to be like English, this might not be for them...or put it more bluntly, they don't have what it takes. These complications cause bugs. I still can't get over my 2018 car spent 5 weeks in the first 7 or 8 months in the shop, all computer problems. Still not getting fix, just learn to live with it.
 
  • #85
yungman said:
* #include <iostream> to perform function of cout to output streams to display using std::cout << follow by
the “asdfongfasd” for words etc.
I get what you're saying, but technically, cout is not a function -- it's a stream object. The actual "function" is the << operator, which is also used to shift bits left. Another standard stream object is cin, and its related operation is >>, which is also used to shift bits right.
yungman said:
*using namespace std to avoid repeating std:: on every line.
*using std::cout; using std::cin at the beginning so don’t have to repeat for the rest of the program.
I agree with what @pbuk said, especially about preferring the latter. A using namespace std statement brings in the entire namespace, which contains a very large number of classes and everything else that is defined in this large namespace. A better idea is to have a separate using statement for each thing you want to use.
yungman said:
*unsign short int: 2 short int: 2
unsign long int: 4 long: 4
int: 4 unsign long long: 8
long long: 8 unsign int: 4
There is no keyword "unsign" -- the correct one is unsigned.
Note that there is some overlap with these types.
unsigned short int is the same as unsigned short
unsigned int is the same as unsigned
unsigned long int is the same as unsigned long
signed char is the same as char
signed short is the same as signed short int and short
signed int is the same as int
signed long int is the same as signed long and long
And so on.
yungman said:
*Declare constant: const double pi = 22/7 // declares “double pi” is constant = 22/7.
Your constant definition intializes pi to 3.0.
Because 22 and 7 are both int constants, 22/7 is calculated using integer division. Any of 22.0/7 or 22/7.0 or 22.0/7.0 would result in floating point division, which is what you certainly intended.
 
  • #86
yungman said:
Each of the statement after the "*" is a completely independent statement, they have no relation to each other. I use * to say it's a separate bullet point.

I feel so far, it's like looking at all the cars, programs and everything computer related products, they all try so hard to make it user friendly, as much freedom of style to express the same thing, making it easy for people that doesn't have the neck of computer to use computer. That it have so many different ways of doing the same thing.

The most ridiculous one is the typeof! If you don't like the standard definition "long long" for 8byte data length, you can use:

typeof long long int bossLecturing;// so I can use bossLecturing to define long long int.

It is very common 3 or 4 programmers working on the same project, each write a portion of the program. If anything goes wrong after integration and need to debut the whole thing. Someone need to read all the sub programs of different programmer.

Think of all three programmer defined the same variable by each chose to use typeof, one use bossLecturing, one use wifeNagging, one use motherinlawTalking. All to define long long. Can you imagine how confusing this can get?

What's wrong with using straight dedicated words, no option. Then everyone understand exactly what it means instead of having to go read the declaration in the program? What's wrong that the codes look like a computer code instead of looking like English?

Maybe I am old, this whole 60 pages can be summed up in 5 pages if all these nonsense get eliminated. It all can be learn in one afternoon instead of reading 60 pages carefully to make sure it's all nonsense! I still think people either have it or don't. If anyone need the codes to be like English, this might not be for them...or put it more bluntly, they don't have what it takes. These complications cause bugs. I still can't get over my 2018 car spent 5 weeks in the first 7 or 8 months in the shop, all computer problems. Still not getting fix, just learn to live with it.
You meant to say typedef right? The point of it is that in C++, types, in their namespaces, and with their templates, and everything, can get extremely long and complicated. typedef is a convenience to save you time and mental energy.

As an example, if you use CGAL (a computational geometry library), you have to do something like this:

C:
typedef CGAL::Exact_predicates_inexact_constructions_kernel         K;
typedef CGAL::Triangulation_vertex_base_with_info_3<unsigned, K>    Vb;
typedef CGAL::Delaunay_triangulation_cell_base_3<K>                 Cb;
typedef CGAL::Triangulation_data_structure_3<Vb, Cb>                Tds;
typedef CGAL::Delaunay_triangulation_3<K, Tds>                      Delaunay;
typedef Delaunay::Point                                             Point;int main()
{
    Point p1( 0, 0, 0 );
    Point p2( 1, 1, 1 );
    Point p3( 2, 2, 2 );
}

because without typedef, you'de need to do this:

C:
int main()
{
    // p1
    CGAL::Delaunay_triangulation_3<CGAL::Exact_predicates_inexact_constructions_kernel,
    CGAL::Triangulation_data_structure_3<CGAL::Triangulation_vertex_base_with_info_3<unsigned,
    CGAL::Exact_predicates_inexact_constructions_kernel>,
    CGAL::Delaunay_triangulation_cell_base_3<CGAL::Exact_predicates_inexact_constructions_kernel>>>::Point p1( 0, 0, 0 );

    // p2
    CGAL::Delaunay_triangulation_3<CGAL::Exact_predicates_inexact_constructions_kernel,
    CGAL::Triangulation_data_structure_3<CGAL::Triangulation_vertex_base_with_info_3<unsigned,
    CGAL::Exact_predicates_inexact_constructions_kernel>,
    CGAL::Delaunay_triangulation_cell_base_3<CGAL::Exact_predicates_inexact_constructions_kernel>>>::Point p2( 1, 1, 1 );

    // p3
    CGAL::Delaunay_triangulation_3<CGAL::Exact_predicates_inexact_constructions_kernel,
    CGAL::Triangulation_data_structure_3<CGAL::Triangulation_vertex_base_with_info_3<unsigned,
    CGAL::Exact_predicates_inexact_constructions_kernel>,
    CGAL::Delaunay_triangulation_cell_base_3<CGAL::Exact_predicates_inexact_constructions_kernel>>>::Point p3( 2, 2, 2 );
}

It's not very fun writing out a type that is 4 lines of code long every place you declare a variable.

Or maybe you want computer language looking names? The CGAL developers could have done this:

C:
int main()
{
    // p1
    F10010::C1001<F1010::R1001, F10010::N0000<F10010::T1111<unsigned, F10010::R1001>, F10010::C2002<F10010::R1001>>>::K0011 s001( 0, 0, 0 );

But that would be pretty easy to mess up. "What in the heck is this s001 thing supposed to be?", people would wonder in despair.

Why are CGAL's types so complicated? If you are on a quest to become a C++ expert, then you will know in about 10-15 years ;)
 
Last edited:
  • #87
yungman said:
It is very common 3 or 4 programmers working on the same project, each write a portion of the program. If anything goes wrong after integration and need to debut the whole thing. Someone need to read all the sub programs of different programmer.
This is why we have encapsulation as one of the three fundamentals of OOP, and why unit testing and integration testing are key components of the development cycle. You are now learning the fundamental principles that make these things possible; persevere and once you start to build a real application it will make more sense.

Perhaps it would be good to take a brief "time out" from the book to get a bit of perspective and inspiration by taking a look at the documentation from a real world library that you might want to #include. Here's a link to a WiFi library for the Arduino: https://www.arduino.cc/en/Reference/WiFi.
 
  • #88
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. 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.

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?

Thanks
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;
}
 
  • #89
Sorry I have not responded to the suggestions of my notes, I am all consumed by the question I posted above. I'll come back later.

Thanks
 
  • #90
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.
 
  • #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
  • #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:

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
8
Views
898
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
2
Replies
58
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top