Learning C++

Status
Not open for further replies.

ghosteye

Seniorius Lurkius
10
Guys,
I am kind of new to programming. I am trying to learn C++.

I have finished my undergraduate degree in Data Communication. So I did not really learn programming. Now I am going graduate school where I am studying computer science. Last couple of months I am trying to learn C++ but I could not. I have gone through lots of online tutorials as well as couple of books of C++. I have learned most of the syntax and semantics of C++ But I can not implement it. Whenever it comes to write a program I can not write it. I don't know how to implement syntax and semantics of C++ that I have learned.

What I find out is that I don't know how to think like a program think. I mean I can't think logically the way program do work. I don't know what to do. It's really frustrating !!! If I can not learn C++, then I will not be able write my thesis which mean I will not get my degree ultimately.

So guys please give me some advise, tips, book reference, online tutorial reference etc

*How can I can improve my logical thinking about C++?
 
ghosteye":vssi48sr said:
I do understand variables, data types, memory allocation, array etc though Sometimes I feel little bit confused about pointers.
A variable stores some information in memory. The variable has a memory location where that info is stored. The variable's type is the type of data stored at that location. So an integer-typed variable x might have memory location 123, at which the integer 10 is stored.

A pointer is simply a variable whose value is a memory address. Just as an integer-typed variable stores an integer at its memory location, a pointer variable stores a memory address at its memory location. Memory addresses are typically represented as integers but they shouldn't be confused with regular integers. For example, pointer y might have memory location 50 and have value 123. In this case, y's value is x's memory location so y "points to" x. You can x's value by "dereferencing" y.

In C you'd write this as:
Code:
int x = 10;
int * y = & x;
int z = * y;
This reads as "Let x be an int-typed variable with initial value 10. Let y be a variable of type pointer-to-int with initial value equal to the memory location of x. Let z be an int-typed variable initially equal to the value in memory that y points to, which happens to be x's value."
 

ghosteye

Seniorius Lurkius
10
newwb":2xsfd0i0 said:
ghosteye":2xsfd0i0 said:
I do understand variables, data types, memory allocation, array etc though Sometimes I feel little bit confused about pointers.
A variable stores some information in memory. The variable has a memory location where that info is stored. The variable's type is the type of data stored at that location. So an integer-typed variable x might have memory location 123, at which the integer 10 is stored.

A pointer is simply a memory address. Just as an integer-typed variable stores an integer at its memory location, a pointer-typed variable stores a memory address at its memory location. Memory addresses are typically represented as integers but they shouldn't be confused with regular integers. For example, pointer-typed variable y might have memory location 50 and have value 123. In this case, y's value is x's memory location so y "points to" x.

In C you'd write this as:
Code:
int x = 10;
int * y = &x;
This reads as "x is an int-typed variable with initial value 10. y is a variable of type pointer-to-int with initial value equal to the memory location of x."


Thanks man. My main problem is that I can't break down problem and figure out how to solve any problem. I can write simple problem like compare something or take user input and count it etc.
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
A pointer is simply a memory address.

Pre-emptive attempt to avoid this thread going off the rails:

It is more complicated than that.
For your needs you can view pointers as some opaque magic value that says 'look over there' and also allows the operations 'get the magic value which would look at the next thing' and 'get the magic value which would look at the previous thing'.

Conceptually viewing it as having a value which is the address of the thing whose value is the thing it points to is not a bad thing so long as you remember this is a conceptual, not actual view of what is happening.
 

fitten

Ars Legatus Legionis
55,639
Subscriptor++
ghosteye":398hc5mu said:
Thanks man. My main problem is that I can't break down problem and figure out how to solve any problem.

This is the very heart of programming... taking a broad problem, identifying patterns, and breaking it down into a bunch of small tasks, each one of which is understood. IMO, you should learn to do that without thinking about C++ at all. Simply get a piece of paper and a pencil and start by making an outline of what the program should do:

1. Read input
2. Compute the average
3. Write the average value as output

Then break down each one of those:

1.1 Write a prompt to the user to enter a value between 0 and 100 and use -1 to end the input
1.2 Read a value from the keyboard
1.2.1 if the value is 0-100, add it to a running total
1.2.2 increment the count of the number of values entered
1.2.3 if the value is -1, then i'm finished taking in input

2.1 Divide the running total by the count of values entered

3.1 Write a friendly message, something like "The average of your values is"
3.2 Write the calculated average

And keep breaking down these things until each is single or small number (easily visualizable) of commands. Then convert it to C++ or whatever language that you intend to use.

As you do things like that, you'll train yourself to break down problems into meaningful/solvable bits and pieces and write your programs. Later, you'll get to the point where you'll say "Oh, I'll solve this bit by putting the values into a tree and we can pull them out later. That's because a tree has O(log N) lookup and we need fast lookup later" (you'll start thinking in algorithms and data structures).
 

ghosteye

Seniorius Lurkius
10
newwb":3ckxadic said:
Do you understand the difference between the following?
Code:
int x = 1;
setToTwo(x);
printf("%d", x);
…
function setToTwo(int x) {
  x = 2;
}

Code:
int x = 1;
int * ptrToX = & x;
setToTwo(ptrToX);
printf("%d", x);
…
function setToTwo(int * x) {
  * x = 2;
}


Here I can see that in the first one u did not use any pointer but in the second one. it will store x=2 in the memory. am i right ?
 

ghosteye

Seniorius Lurkius
10
ghosteye":1zqeratv said:
newwb":1zqeratv said:
Do you understand the difference between the following?
Code:
int x = 1;
setToTwo(x);
printf("%d", x);
…
function setToTwo(int x) {
  x = 2;
}

Code:
int x = 1;
int * ptrToX = & x;
setToTwo(ptrToX);
printf("%d", x);
…
function setToTwo(int * x) {
  * x = 2;
}


Here I can see that in the first one u did not use any pointer but in the second one. it will store x=2 in the memory. am i right ?
 

ghosteye

Seniorius Lurkius
10
fitten":3v6jyxrb said:
ghosteye":3v6jyxrb said:
Thanks man. My main problem is that I can't break down problem and figure out how to solve any problem.

This is the very heart of programming... taking a broad problem, identifying patterns, and breaking it down into a bunch of small tasks, each one of which is understood. IMO, you should learn to do that without thinking about C++ at all. Simply get a piece of paper and a pencil and start by making an outline of what the program should do:

1. Read input
2. Compute the average
3. Write the average value as output

Then break down each one of those:

1.1 Write a prompt to the user to enter a value between 0 and 100 and use -1 to end the input
1.2 Read a value from the keyboard
1.2.1 if the value is 0-100, add it to a running total
1.2.2 increment the count of the number of values entered
1.2.3 if the value is -1, then i'm finished taking in input

2.1 Divide the running total by the count of values entered

3.1 Write a friendly message, something like "The average of your values is"
3.2 Write the calculated average

And keep breaking down these things until each is single or small number (easily visualizable) of commands. Then convert it to C++ or whatever language that you intend to use.

As you do things like that, you'll train yourself to break down problems into meaningful/solvable bits and pieces and write your programs. Later, you'll get to the point where you'll say "Oh, I'll solve this bit by putting the values into a tree and we can pull them out later. That's because a tree has O(log N) lookup and we need fast lookup later" (you'll start thinking in algorithms and data structures).


U got my point. I know the syntax of C++. But most of the time When I try to break down the problem to solve it then I feel like I am blur. So i think I need to practice more n more
 
ghosteye":1dxh2xsq said:
Here I can see that in the first one u did not use any pointer but in the second one. it will store x=2 in the memory. am i right ?
Yes. In the first one, the x defined initially and set to 1 is not the same x in setToTwo. When setToTwo is called, x is copied to a new x with the same value, 1. setToTwo then updates its own x but not the original x. So the first example prints out 1.

In the second example, ptrToX and setToTwo's x are again not the same variable. ptrToX's value, & x, is copied into setToTwo's x when setToTwo is called. Then setToTwo updates the value at its x's address, which is the original x. The original x is set to 2 so the second example prints out 2.
 

jbode

Ars Legatus Legionis
11,755
Subscriptor
ghosteye":au3qz7rr said:
fitten":au3qz7rr said:
ghosteye":au3qz7rr said:
Thanks man. My main problem is that I can't break down problem and figure out how to solve any problem.

This is the very heart of programming... taking a broad problem, identifying patterns, and breaking it down into a bunch of small tasks, each one of which is understood. IMO, you should learn to do that without thinking about C++ at all. Simply get a piece of paper and a pencil and start by making an outline of what the program should do:

snip example

As you do things like that, you'll train yourself to break down problems into meaningful/solvable bits and pieces and write your programs. Later, you'll get to the point where you'll say "Oh, I'll solve this bit by putting the values into a tree and we can pull them out later. That's because a tree has O(log N) lookup and we need fast lookup later" (you'll start thinking in algorithms and data structures).


U got my point. I know the syntax of C++. But most of the time When I try to break down the problem to solve it then I feel like I am blur. So i think I need to practice more n more

Programming is a skill, and like any other skill it requires non-trivial amounts of practice; you learn it by doing it. The trick is getting started. Begin with something exceedingly simple, like writing a message to standard output (which should correspond to the console):
Code:
#include <iostream>

int main(void)
{ 
  std::cout << "i can has output" << std::endl;
  return 0;
}

The next step is usually asking for some input to play with:

Code:
#include <iostream>
#include <string>

int main(void)
{
  std::string name;
  std::cout << "i can has name: " << std::flush;
  if (std::cin >> name)
    std::cout << "hello, " << name << std::endl;
  else
    std::cout << "hurrr" << std::endl;
  return 0;
}

You can build on these simple examples to play with different data types (int, float, etc.) and control structures (such as the if statement in the second example), until you get to the point where you can write this kind of code without really thinking about it. Then you move on to something a little more complicated, like taking several inputs to compute a value, or breaking operations up among different functions.

You have to walk before you can run, and you have to crawl before you can walk. Some people have to crawl a little longer than others. Eventually, it gets easier to see the big picture; you just have to start with really small pictures at the beginning.
 

ghosteye

Seniorius Lurkius
10
jbode":xxtf56de said:
ghosteye":xxtf56de said:
fitten":xxtf56de said:
ghosteye":xxtf56de said:
Thanks man. My main problem is that I can't break down problem and figure out how to solve any problem.

This is the very heart of programming... taking a broad problem, identifying patterns, and breaking it down into a bunch of small tasks, each one of which is understood. IMO, you should learn to do that without thinking about C++ at all. Simply get a piece of paper and a pencil and start by making an outline of what the program should do:

snip example

As you do things like that, you'll train yourself to break down problems into meaningful/solvable bits and pieces and write your programs. Later, you'll get to the point where you'll say "Oh, I'll solve this bit by putting the values into a tree and we can pull them out later. That's because a tree has O(log N) lookup and we need fast lookup later" (you'll start thinking in algorithms and data structures).


U got my point. I know the syntax of C++. But most of the time When I try to break down the problem to solve it then I feel like I am blur. So i think I need to practice more n more

Programming is a skill, and like any other skill it requires non-trivial amounts of practice; you learn it by doing it. The trick is getting started. Begin with something exceedingly simple, like writing a message to standard output (which should correspond to the console):
Code:
#include <iostream>

int main(void)
{ 
  std::cout << "i can has output" << std::endl;
  return 0;
}

The next step is usually asking for some input to play with:

Code:
#include <iostream>
#include <string>

int main(void)
{
  std::string name;
  std::cout << "i can has name: " << std::flush;
  if (std::cin >> name)
    std::cout << "hello, " << name << std::endl;
  else
    std::cout << "hurrr" << std::endl;
  return 0;
}

You can build on these simple examples to play with different data types (int, float, etc.) and control structures (such as the if statement in the second example), until you get to the point where you can write this kind of code without really thinking about it. Then you move on to something a little more complicated, like taking several inputs to compute a value, or breaking operations up among different functions.

You have to walk before you can run, and you have to crawl before you can walk. Some people have to crawl a little longer than others. Eventually, it gets easier to see the big picture; you just have to start with really small pictures at the beginning.

Bro, I can write simple programs like take a user input and calculate the input etc but when it come up with some critical thinking problem then I am totally stuck. I Think I need more practice which I am doing. anyway, Thanks buddy. I have to walk before you can run, and I have to crawl before I can walk. :bigdumbgrin:
 

ghosteye

Seniorius Lurkius
10
Doctor Watson":fwazf5ht said:
If you want to learn programming using C++ then I think this is the best book to start with: Programming: Principles and Practice Using C++

I second that suggestion, it is a great book for someone trying to learn programming with a C++ focus. I wish it had been around when I got started with C++ back in the day.


I am looking at the book. I hope it will help me. I really need to learn C++ as soon as I can.
thanks.
 

jbode

Ars Legatus Legionis
11,755
Subscriptor
ghosteye":15e5gltx said:
Bro, I can write simple programs like take a user input and calculate the input etc but when it come up with some critical thinking problem then I am totally stuck. I Think I need more practice which I am doing. anyway, Thanks buddy. I have to walk before you can run, and I have to crawl before I can walk. :bigdumbgrin:

So, what kinds of problems are you getting stuck on? Can you give us an example of something you're having trouble with?
 

Pyrrho

Wise, Aged Ars Veteran
136
ghosteye":231nkv7i said:
Guys,
I am kind of new to programming. I am trying to learn C++.

I have finished my undergraduate degree in Data Communication. So I did not really learn programming. Now I am going graduate school where I am studying computer science. Last couple of months I am trying to learn C++ but I could not. I have gone through lots of online tutorials as well as couple of books of C++. I have learned most of the syntax and semantics of C++ But I can not implement it. Whenever it comes to write a program I can not write it. I don't know how to implement syntax and semantics of C++ that I have learned.

What I find out is that I don't know how to think like a program think. I mean I can't think logically the way program do work. I don't know what to do. It's really frustrating !!! If I can not learn C++, then I will not be able write my thesis which mean I will not get my degree ultimately.

So guys please give me some advise, tips, book reference, online tutorial reference etc

*How can I can improve my logical thinking about C++?

What if you learned a higher-level language first? Perl, Python... I was about to say Java but I bit my tongue.

And this is why I toss this out: because there are really at least two aspects to programming, thinking about the bigger picture, and the nitty gritty details. Lower-level languages (the term has become relative over the years) like C++ give you power at the expense of having to do to a fair amount of micromanagement. But sometimes you want to spend your thinking cycles on 'how shall I structure this program logically' issues versus 'oh, how do I parse a string again' issues. The bigger picture logic and structure of your program, the design patterns. Things that rely on experience, and yet different in thinking methodology than the syntax and such issues.

This is the route I took: Perl, then Java and now C[1]. Perl lets you 'just get it done' at the expense of not knowing the lower-level details. That happened to work quite well for me. Obviously there are people for whom the mileage is gonna vary. In any case, I wanted to offer that in answer to your specific interest in 'improving your logical thinking in C++'. You might be missing the forest for the trees.

Final, unrelated suggestion. What if you spent a good amount of time examining and understanding other people's programs for a while.

1 - Yes, I know it's not C++, and that C++ has plenty of higher-level libraries and frameworks, but it has plenty of micromanagerial potential for the programmer.
 

Pyrrho

Wise, Aged Ars Veteran
136
WcW":3lbgxwav said:
If you're interested in math:

http://projecteuler.net/

If not:

You should reconsider programming.

That's a gross oversimplification.

You can be "not that interested in math" and still get by in math, and program happily and successfully.

You can be "not that interested in math" and not be great at math, and program happily and successfully.

Not everyone's a quantitative analyst producing complex algorithms for use in automated trading models.

I know plenty of people in the upper two categories who are paid pretty well and are pretty decent programmers.

Note, we're not talking about 'being number one', here, since you kept the bar low by keeping the scope within 'whether one should consider programming or not'. I know people in the third category making a fortune.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Pyrrho":b7n33xhr said:
That's a gross oversimplification.
Seeing as computer science is math, not really. Programming is about the understanding and application of algorithms, and the manners in which we reason about them are unquestionably formed by mathematics.

You can be "not that interested in math" and still get by in math, and program happily and successfully.
Perhaps, but it'd be rather unusual. However, ...

You can be "not that interested in math" and not be great at math, and program happily and successfully.

Not everyone's a quantitative analyst producing complex algorithms for use in automated trading models.
Your mistake is in what you consider mathematics. Such things are not the totality of mathematics, and basing your recommendations on such a hideously flawed worldview is really quite foolish, I think.
 
I think the OP's original question was slightly different, and referred to the more abstract part of programming.

As a very simple example, take the text 'John Smith'. Say you want to split the first name from the surname. For a human that's easy and takes milliseconds, but I believe the OP was asking about how to think about the problem like a computer needs to.

So, a computer program needs to do something like this:

1: go through the text until it finds a space;
2: cut all the characters before the space and call that the first name;
3: cut all the characters after the space and call that the surname;
4: remember to take into account the space when counting characters.

Step 3 needs even more work because it's necessary to find the length of the text before it can cut the surname. Plus, what if the person has two (or more) first names?

Thinking like this takes much longer to learn than syntax and keywords. Only after you can write this pseudo-code can you then select the right functions (ansistrleft, for example).
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
As far as the OP is concerned, things like projecteuler are a pretty good idea, at least in terms of giving you problems to practice on. Picking up a data structures & algorithms book(s), like Knuth or Sedgwick, would alos be a good idea. You may also find value in reading a text like "Structure and Interpretation of Computer Programs", which is freely available online, along w/ video lectures.
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
I also think that, based on the OP's indication that they have a problem breaking problems down into chunks and dealing with it that way, anything that forces you to do that is a good idea.

As such project euler is a good idea since it tends to have later problems relate to, or use explicitly, ideas and solutions from earlier questions. Thus building this concept up as it goes along.

But really any actual coding is a good thing, especially in an area where you have a forum for actually discussing the resulting solutions and looking at how other's have done it (albeit after you have completed it).

I would say that, some of the euler problems are *massively* easier to implement in other languages to C++. Part of that is in the libraries available by default (having a BigInteger equivalent available from the outset for example). This isn't dissing C++, just that you need to go and find/write this aspect yourself to start with. That could still be useful, especially if the purpose is to learn C++!

If you are really stuck on a problem coming back here to ask about it (showing what you have done so far) is likely to get a very positive response.
 

erikengh

Ars Tribunus Militum
2,280
Sounds like you need to learn to think about how you solve a problem and not about C++ in particular. In my experience far too many struggle with getting programming because they start with C++. C++ gives you so much syntax to deal with that it takes away focus from solving the actual programming task. I suggest starting with a much simpler language like Python, Ruby, Perl, Java, C#, Visual Basic.

But actually I am tempted to suggest the book "How to Design Programs: An introduction to Programming and Computing" by Matthias Felleisen, R.B. Findler etc. It uses DrScheme, which a programming environment for the Scheme programming language, which is very oriented towards learning and playing around. Some of the benefits with this approach is that Scheme syntax is extremely simple so most of the book is about learning how to think about programming and building abstractions.

But it might be better to start with Python, because the transition to a more mainstream programming language like C++ might be more difficult with a Scheme background. I don't really know. Maybe people who used Scheme / LISP as a teaching language could comment on how well / bad it worked out. I didn't. I started with C++ in Uni (which was ok for me because I had already learned Basic, C and Assembly in my spare time. But I saw a lot of my classmates struggle and get severely confused by pointers, references, references to pointers etc. A lot didn't enjoy or get programming until we started with perl in some classes.
 
Status
Not open for further replies.