Super new at learning C++, what math should I know/be learning?

Hello. I'm very very new at C++ and trying to navigate this abstract and confusing language. I know you need math to complete a major in computer science and now I'm starting to learn how it is linked with programming, haha.

I have been going through C++ Primer Fifth Edition by Stanley B. Lippman. When the while statement was mentioned, an example of "Sum of 1 to 10 inclusive is 55" was mentioned. I understood how the code worked I think, but I tried to do the math manually (sum+val=sum, val+1=val, repeat) and did not come up with 55, so I don't know how it equals 55. Someone else asked about this on this forum and one reply said:

"So when n = 10 you get ( 10 * ( 10 + 1 ) ) / 2 = 55."

Which makes sense, if you know this formula and how it corresponds to the example. Which I didn't. How do you know what type of math to use in certain circumstances like this? I took some pre-algebra classes in my first semester in January, but I've forgotten it all since then. I'm thinking I'm going to have to learn some math if I hope to understand this language and how it operates.

So my questions:
#1: What kind of math is "Sum of x to y is"? I tried googling but couldn't come across what concept it is under. Assuming it's algebra, but which concept? So I can look it up and learn it.
#2: How do you know what type of math to use in what circumstance?
#3: What math should I be learning that is related to C++? Especially for beginners? What is most prevalent?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return 0;
}
"So when n = 10 you get ( 10 * ( 10 + 1 ) ) / 2 = 55."

It's the closed form of a finite arithmetic series. You can google the proof, but essentially it's just grouping the terms so you get (n + 1) + (n-1 + 2) ... which simplifies to (n+1)*(n/2).

As for the code itself, here's a little runthrough:
sum = 0; val = 1 //before loop executes
For the code below, let the line number be the iteration of the while loop:
1
2
3
4
5
6
7
8
9
10
11
val = 1; sum = 0 + 1 = 1; val = 2 after increment;
val = 2; sum = 1 + 2 = 3; val = 3 after increment;
val = 3; sum = 3 + 3 = 6; val = 4 after increment;
val = 4; sum = 6 + 4 = 10; val = 5 after increment;
val = 5; sum = 10 + 5 = 15; val = 6 after increment;
val = 6; sum = 15 + 6 = 21; val = 7 after increment;
val = 7; sum = 21 + 7 = 28; val = 8 after increment;
val = 8; sum = 28 + 8 = 36; val = 9 after increment;
val = 9; sum = 36 + 9 = 45; val = 10 after increment;
val = 10; sum = 45 + 10 = 55; val = 11 after increment;
val = 11; //loop condition fails, terminate loop 

As you can see, the final sum is 55, which agrees with the closed form of a finite arithmetic series.

For most of the basic stuff you don't need too much math, though you obviously need to know some multiplication and addition. Learning how to work in binary is obviously very useful too.
Last edited on
You don't need to much math to do simple stuff. However, if you go more and more into programming, some maths knowledge may be useful, or required.

Right now just learn programming. 15 year old or 13 can start learning without much problem. Depending on what you keep doing, you will need some kind of more advanced math or not. Depends on what you are doing.

However, learning math is good since it develops your imagination.
I know I'm a few days late, haven't been doing any programming since thursday. :P I just wanted to let you guys know I appreciate your help.
You don't need to much math to do simple stuff. However, if you go more and more into programming, some maths knowledge may be useful

I agree with this.
OP, if it helps you could look at it the other way around: I have a theoretical physics degree and i'm crap at programming so i wouldnt say maths and programming is that tightly linked :)
Theoretical physics? That sounds so cool ~ it's so inspiring to know others further off in life than I am and are still new at programming, as someone who just finished her first year of college. It shows it's never too late to learn something new! Hopefully I can learn from topics on this forum and maybe even get good enough some day to pass on my knowledge to newbies.
Topic archived. No new replies allowed.