Learning C++ with Angel
#1
Posted 09 June 2011 - 05:16 PM
Maybe it will teach other people a thing or two, if they have just started learning C++ as well.
You may comment if you like, about errors you might have spotted that I did not see, or ask questions as to why A is not B or why A is A, etc.
I will assume that you have the most BASIC of knowledge in C++.
That absolute basic. How to write a piece of code.
How to compile and run it, and how to assign variables. At least to begin with.
All for the learning purpose here people ^^
All the code I post here, is taken from an example I follow in a book, and modified by me, without help from other sources or the book itself. Or it can be requests like the first assignment presented here that does not have example code but expect that you have learned some from reading the book.
The book I am using is called "C++ Without Fear, Second Edition".
If a spoiler tag is named "Exercise x.x.x" it means that the book have given me an assignment that has this number, and the code within that spoiler tag is my answer to the assignment given above it :)
First assignment goes like this:
Assignment:
Write a program that inputs a value into a
variable n and outputs the cube (n * n * n).
[spoiler=Exercise 1.3.3][pastebin]wvsMDHbV[/pastebin][/spoiler]
#2
Posted 09 June 2011 - 06:31 PM
Look down, look down, you're standing in your grave...
#3
Posted 09 June 2011 - 08:41 PM
#4
Posted 09 June 2011 - 09:24 PM
Quote
For example:
Quote
While this:
Works excessively better.
Look down, look down, you're standing in your grave...
#5
Posted 10 June 2011 - 01:38 AM
While you are correct that the code is easily breakable because it doesn't have a fail proof mechanic to make sure it's a number that is typed in, I have yet to learn more about the language before I can make it fail proof like that.
It's kind of like showing how superior you are to the language than me and that's not really helpful when I don't understand the concepts behind it :P
#6
Posted 10 June 2011 - 01:52 AM
Quote
While you are correct that the code is easily breakable because it doesn't have a fail proof mechanic to make sure it's a number that is typed in, I have yet to learn more about the language before I can make it fail proof like that.
It's kind of like showing how superior you are to the language than me and that's not really helpful when I don't understand the concepts behind it :P
Basically, you should always do inputs as characters, because on older versions of MSVC and several other compile systems inputting something incorrect causes insane program death. So better to learn this now.
Look down, look down, you're standing in your grave...
#7
Posted 10 June 2011 - 02:01 AM
I am not using MSVC just so you know.
I am using a free IDE called Dev-C++
It's pretty neat and easy to use :3
#8
Posted 10 June 2011 - 11:51 AM
In this segment I was taught about If, Else and modulus calculation.Then given a piece of example code.
[spoiler=Example Code][pastebin]Wp5UdZTP[/pastebin][/spoiler]
Assignment:
Write a program that reports whether a number input is divisible by 7.
[spoiler=Excersice 2.1.1][pastebin]CS324FYG[/pastebin][/spoiler]
#9
Posted 10 June 2011 - 03:04 PM
In this segment I was taught about While loops and given example code.
[spoiler=Example Code][pastebin]crEBSKU9[/pastebin][/spoiler]
Assignment:
Write a program to print all the numbers from n1 to n2, where n1 and n2 are two numbers specified by the use.
[spoiler=Exercise 2.2.1][pastebin]U3KqBaiM[/pastebin][/spoiler]
Assignment:
Alter the example so that it prints all the numbers from n to 1 in reverse order, as in 5 4 3 2 1.
[spoiler=Exercise 2.2.2][pastebin]ymb37hyB[/pastebin][/spoiler]
Assignment:
Alter the example so that it prints only even numbers, as in 0, 2, 4.
[spoiler=Exercise 2.2.3][pastebin]u9bzZkJB[/pastebin][/spoiler]
Assignment:
Alter the example so that it initializes both i and n in the same line. Initialize n to 0. Although this isn’t required in this case, it is good programming practice.
[spoiler=Exercise 2.2.4][pastebin]LdxWY3gG[/pastebin][/spoiler]
#10
Posted 10 June 2011 - 04:30 PM
Quote
Alter the example so that it prints all the numbers from n to 1 in reverse order, as in 5 4 3 2 1.
[spoiler=Exercise 2.2.2][pastebin]KnT89u09[/pastebin][/spoiler]
Quote
Alter the example so that it prints only even numbers, as in 0, 2, 4.
[spoiler=Exercise 2.2.3][pastebin]dkfpHDKG[/pastebin][/spoiler]
I just did these for fun. :D
Look down, look down, you're standing in your grave...
#12
Posted 11 June 2011 - 09:04 AM
&& (and), || (or) and ! (not).
I was also taught about the incremention operators, but I already knew about these.
var++: Pass along the current value of var; then add 1 to var
++var: Add 1 to var; then pass along the result.
var--: Pass along the current value of var; then subtract 1 from it.
--var: Subtract 1 from var; then pass along the result.
The example code makes a simple program that will check whether the age you give it, is in the teenager "range" of age.
[spoiler=Example Code][pastebin]DGfYJQr5[/pastebin][/spoiler]
Assignment:
Write a program to test a number for being in the range 0 to 100, inclusive.
[spoiler=Exercise 2.3.1][pastebin]TksMGjfE[/pastebin][/spoiler]
#13
Posted 11 June 2011 - 09:51 AM
Also, are these assignments for a class? Or from the book?
#14
Posted 11 June 2011 - 11:15 AM
The book does not contain solutions for the exercises.
#15
Posted 11 June 2011 - 01:05 PM
I was also shown how I could use "break" to optimize the code, because it would then stop calculating as soon as it hit jackpot on either condition.
The example code below can tell you if the number you wrote, is a prime number or not. One thing have to be noted though. The original example code did NOT work.
The function is called sqrt(), also known as Square Root. The problem is that the example code tells you, to make all variables like ints. Whole numbers, no commas.
But Sqrt() only takes floats (x.0f), doubles (x.00f) and other comma values.
Egari helped me find a solution, and that was to do something called "casting".
In this case it means that when the while loop runs, we take "i" and treat it as a float to make the calculation work out, but the end result will not matter in terms of commas, since we won't be printing the variable i at any point.
[spoiler=Example Code][pastebin]eq7fCppm[/pastebin][/spoiler]
Assignment:
Optimize the program by calculating the square root of n just once, rather than over and over. You’ll need to declare another variable and set it to the square root of n. The type should be double. You can then use this variable in the while condition.
[spoiler=Exercise 2.4.1][pastebin]Wx5RiTK5[/pastebin][/spoiler]
#16
Posted 17 July 2011 - 09:55 AM
I changed my compiler so it is now Microsoft Visual C++ 2010.
So that also means my stuff looks a bit different. But mostly just what files to include, like the stdafx.h file.
So, this is not an assignment I was given.
This is something I made myself using what I had learned.
It should be fairly fool proof when compiled and used
Similar Topics
![]() |
DuoLingo.com - Anyone learning a language?Started by SickBoy, 08 Jan 2013 |
|
|
|
![]() |
Fun learning?Started by Död, 06 Nov 2011 |
|
|
|
[Java] Learning Object Orientated CodingStarted by AngelSandy, 27 Apr 2011 |
|
|
||
![]() |
Anime Recommendation: Angel Beats!Started by Ave Jettom, 29 Dec 2010 |
|
|
|
![]() |
[LUA] Learning a spell on quest completion.Started by Grathares, 19 Sep 2010 |
|
|
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users















