C++ Learning Center
#1
Yo, so this is gonna be a thread going step-by-step throughout the process of C++. When it comes to Programming, I am an believer in "practice makes perfect", so we're gonna learn by completing practice problems. I will post links to all practice problems, and I will provide my own solutions with explanations of the new parts of C++ utilized in each program.

The structure of this guide will be:
----------
Chapter X

Practice Problem Link:

Problem Text:

New stuff covered:

Code: (This will include comments with why I used what)

Summary/Extra Problems: 
----------
This thread will be continuously edited for new chapters, unless Zak decides to make a new Topic for HBC dev/C++. 

Without any further a-do, here is

Chapter 1: "Hello World"



Problem Text: use either printf or cout to print the string  to stdout.


New stuff covered: Headers, Functions


Headers:
So a header is basically pre-written code that is compiled before any other code is ran. 
1. In this case iostream provides basic console input/output, and a few other things. 
2. cstudio on the other hand is used to communicate with other devices that the machine can use. ie. Mouse/Keyboard/etc.


Functions:
A function is a collection of statements that the compiler runs when called upon. 
1. "int". Here we define the function as needing to return an integer. Keep in mind that returning a value/string, is NOT the same as outputting to the console.
2. "main". In this case we have the loop "main" followed by brackets to symbolize where the function begins, but more impotently, where it ends. 
3. "printf("Hello, World!");". This simply prints what is within the quotations to the console.
4.  "return 0;". This simply returns an integer, which would signify that the function is over. 


Code:
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
   printf("Hello, World!");
   return 0;
}


Summary/Extra Problems: So now you know what a header is, what a variable is, what a function is, and how to print stuff to the console!
Problem: Set 2 variables as integers (a and b), then have another integer variable © equal the sum of the two variables! (Do not have the user input to the console, just have the console output the number you desire! Hint: You may not be able to use a 'printf("")' on this one!
Leave your responses in the replies sections with the following format:
Extra Question 1 Code:
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
 int a = 5;
 int b = 10;
 int c = a + b;
 << cout c;
}
-----------------------------------------------------------------------------------------

Chapter 2: "Input and Output"



Problem Text: (I am going to slightly modify this problem) Input 3 numbers to the console, and have their sum equal 10.
Note: This problem is different than the Extra Problem in Chapter 1 because the user in inserting their own numbers.


New stuff covered: cin, cout, and operands.


cin: cin is a fancy way of saying: Console "c" Input "in". Whenever you write "cin", you need to use ">>", which implies that the action is going to the console.


cout: This means: Console Output. Whenever you write "cout", you need to use "<<", which implies that the action is going out of the console.


operand: This is a fancy way of saying: + - * / etc. However, different programming languages use different operands for different functions. 
For a rather extensive list of operands for c++, visit: https://www.geeksforgeeks.org/operators-c-c/


Code:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() { // start our function
 int a; // define variable a
 int b; // define variable b
 int c; // define variable c

 cin >> a; // Have the user input a value for variable a
 cin >> b; // Have the user input a value for variable b
 cin >> c; // Have the user input a value for variable c
 int d = a + b + c; // Define variable b as an integer, and set it equal to the sum of a, b, and c
 cout << d; // Ouput variable d
 return 0;  // end the function
}


Summary/Extra Problems: So now you know how to get input from the user, and c++ operands!!!
Problem #2: Before you get input from the user, output text asking for the users first, second, and third number. When you print the sum of the 3 numbers print text that says: "The sum of your three numbers is X".
Problem #2 Code:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() { // start our function
 int a;     // define variable a
 int b;     // define variable b
 int c;     // define variable c
 printf("What is your first number?");
 cin >> a; // Have the user input a value for variable a
 printf("What is your second number?");
 cin >> b; // Have the user input a value for variable b
 printf("What is your third number?");
 cin >> c;          // Have the user input a value; for variable c
 int d = a + b + c; // Define variable b as an integer, and set it equal to the
                    // sum of a, b, and c
 printf("The sum of your three numbers is: ");
 cout << d; // Ouput variable d
 return 0;  // end the function
}
-----------------------------------------------------------------------------------------

Chapter 3: "Conditional Statements" 

Practice Problem Link: https://www.hackerrank.com/challenges/c-...se/problem

Problem Text: Give a positive integer denoting n, do the following:

1. If 1 =< n =< 9, then print the lowercase English word corresponding to the number (print "one" for 1, "two", for two, etc.

2. If n > 9, print "Greater than 9"

Constraints: 1 =< n =< 10^9

New stuff covered: if, else, and else-if

if: This means: "If (x) happens, then DO y.

example (not using c++ syntax): if the variable x = 1, then execute: x + 1.

else: This means: if (x) doesn't happen, then DO z.

example (not using c++ syntax): if the variable x does NOT = 1, then execute: x + 2.

(if-)else-if: This means: if (x) doesn't happen, but (a) does happen, then DO b.

example (not using c++ syntax): if the variable x does NOT = 1, but the variable x = 5, then execute: x + 7.

Code:

#include <bits/stdc++.h>

using namespace std;

int main() // Start of Function
{
 int n;    // Define n as an integer
 cin >> n; // Send input to the console
 cin.ignore(numeric_limits<streamsize>::max(), '\n');
 if (n == 1) { // If statement for 1
   printf("one");
 } else if (n == 2) { // If statement for 2
   printf("two");
 } else if (n == 3) { // If statement for 3
   printf("three");
 } else if (n == 4) { // If statement for 4
   printf("four");
 } else if (n == 5) { // If statement for 5
   printf("five");
 } else if (n == 6) { // If statement for 6
   printf("six");
 } else if (n == 7) { // If statement for 7
   printf("seven");
 } else if (n == 8) { // If statement for 8
   printf("eight");
 } else if (n == 9) { // If statement for 9
   printf("nine");
 } else if (n > 9) { // If statement for greater than 9
   printf("Greater than Nine!");
 }
 return 0;
}

Summary/Extra Problems: So you've learned about conditional statements. Now I want you to ask the user for their name and their birthday, as well as a friend of theirs' name and birthday. I then want you to compare their ages and print: "%their name%, is older than %your name% by %4 years%... or vic versa.
Sample input:
Tim
17
John
18
Sample Output:
John is older than Tim by 1 year(s)!

Problem #3 Code:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main() // Start of Function
{
char str [100]; // creating a string with the name "str"
cin >> str; // Asking the user for the value of the string
int a1;    // First Age input
cin >> a1; // Send input to the console
char stri [100]; // creating a string with the name "stri"
cin >> stri; //asking the user for the calue of the string
int a2;    // Second age input
cin >> a2; // Send input to the console
int dif; // declaring the variable for the difference in age.

cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (a1 > a2) { // If statement for 1
   dif = a1 - a2; // getting the difference in age
  cout << str << " Is older than " << stri << " by " << dif << " year(s)" << endl; //output
} else if (a1 < a2) { // If statement for 2
   dif = a2 - a1; // getting the difference in age
  cout << stri << " Is older than " << str << " by " << dif << " year(s)" << endl; //ouput
} else if (a1 == a2) { // If statement for 3
  printf("You two are the same age!");
}
return 0;
}

-----------------------------------------------------------------------------------------

Chapter 4: "For Loops"

Practice Problem Link: https://www.hackerrank.com/challenges/c-...op/problem

Note: Before I even get started with this one, the practice problem provided by the site is explained HORRIBLY. I've taken a lot of math, and it still took me ages to solve the problem, and even after solving it... I'm not even sure I FULLY understand it.

Problem Text: (I am going to slightly modify this problem) Input 2 different numbers (at the same time) (The first number is ALWAYS smaller than the second). Then, if the first number is >= 9, run a loop that prints its written value up to nine. Afterwards, take the second number, and from (10 to the second number), loop and display if it is odd, or even.

Sample input:
3, 15
Sample output:
three
four
five
six
seven
eight
nine
even
odd
even
odd
even
odd

New stuff covered: for(a, b, c)

for(a;b; c):

a: Set a variable for the for loop. example: for(int i, ...
b: Set a parameter for which the loop runs. ie. i < 10... only run if integer i is less than 10: for(int i; i < 10 ...
c: How will you increment a? If a never goes beyond the parameters of b, then how will the loop stop? That's why you need c:
 for(int i; i < 10; i++)
Note: i++, just means that i will increase by 1 every time it loops. --i would make i decrease by 1 every time it loops. There are multiple ways to increment your variable.

Also, we need the "%" operand. This gives us the remainder of a division problem. For example: 10 % 5 = 0.        15 % 3 = 0.

Code:
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
 int n = 10;
 int d;
 int i;
 int z;
 int y;

 cin >> y;
 z = y;
 cin >> i;
 d = i;

for (int z = y; z <= 9; z++)
  if (z == 1) {
    cout << "one" << endl;
  } else if (z == 2) {
    cout << "two" << endl;
  } else if (z == 3) {
    cout << "three" << endl;
  } else if (z == 4) {
    cout << "four" << endl;
  } else if (z == 5) {
    cout << "five" << endl;
  } else if (z == 6) {
    cout << "six" << endl;
  } else if (z == 7) {
    cout << "seven" << endl;
  } else if (z == 8) {
    cout << "eight" << endl;
  } else if (z == 9) {
    cout << "nine" << endl;
  }

for(int d = i; d >= n; n++)
   if (n % 2 == 0) {
   cout << "even" << endl;
   } else if (n % 2 != 0) {
   cout << "odd" << endl;
   }
}


There isn't going to be a BONUS problem this time, because if you could solve this problem, you're fine Smile
-----------------------------------------------------------------------------------------
Chapter 5: "Functions"

Practice Problem Link: https://www.hackerrank.com/challenges/c-tutorial-functions/problem

Problem Text: Create a function that inputs 4 numbers and outputs the largest number.

New stuff covered: Functions (kinda)

Function: A function is a just a collection of lines of code that can be called upon later.
Syntax:
int NAME_OF_FUNCTION(ELEMENT1, ELEMENT2, ETC){
     CODE
return 0;
}

Code:

#include <iostream>
#include <cstdio>
using namespace std;

int max(int a, int b, int c, int d){ //This is our function. It's named: max, and has 4 elements
   cin >> a; //input the value for int a
   cin >> b; // input the value for int b
   cin >> c; //input the value for int c
   cin >> d; // input the value for int d
   if (a > b and a > c and a > d){ //if statement to see if a is the largest number
       cout << a; //If it is the largest, output the value of a
   }
   if (b > a and b > c and b > d){ //if statement to see if b is the largest number
       cout << b; //If it is the largest, output the value of b
   }
   if (c > a and c > b and c > d) { //if statement to see if c is the largest number
     cout << c; // If it is the largest, output the value of c
   }
   if (d > a and d > b and d > c) { //if statement to see if d is the largest number
     cout << d; // If it is the largest, output the value of d
   }
   return 0; //Since I gave the function the int property, we must return a value. If this function was void, we wouldn't have to do this.
}
int main() {
   int z; // declare z
   z = max(0, 0, 0, 0); // set z equal to the result of the function.
}

-----------------------------------------------------------------------------------------
Reply
#2
Chapter three added Smile
Reply
#3
Chapter Four Added Smile
Reply
#4
Additional formatting/notes added
Reply
#5
Chapter Five Added Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)