Sunday, June 30, 2013

C++ (beginner) different ways of reading string type

As we all do we write cin>>s; (s is our string type)
what if we need to read a whole line which can include spaces;
cin will read the string until it reaches a space bar or enter. For that we can use a special function called getline..
The getline function can be called in 2 ways
getline(cin,s);
cin.getline(s);

getline function, as the name suggests, this will get everything on that line.

another way is to read a set of characters.
suppose we have char a[10000];
string s;
we can use scanf function to read the characters
scanf("%c",a);
this will read all he text in an array of characters. now we can write this
s=a;
though a is an array and s is a string but you can write thath . NOTE that you can't write the reverse version
a=s; this one is not legal.

C++(beginner) local and global variables.

The local variable can be used only in the place they have been declared , If you try to use them outside the place they have been declared you will get a compiler error.

The black bordered rectangle is the place for writing global variables. NOTE you can't use variable before their declaration no matter its local or global.
As we know the main part of our code is a function also (void main or int main) and the variables declared in main can't be used in other places. The variable I created on the 4th line int global_variable1 and int global_variable2 can be used everywhere in code, in any function but the variable I created in main function int local_variable1 and int local_variable2 can be used only in main function. NOTE if you declare a global variable and another variable in main with the same name, every time you call that variable you will get the value of the one you declared in main.
There are some limits , suppose you want to declare an array of integers with the lenght of 1000000 (1 million). If you try to do that in main function , after running your program you will get an error. For example this one.

You may also get the "error reporting " window.

C++(beginner) basic algorithms with arrays. Findings the smallest/biggest element in array.

Suppose we have array .
int a[1000];
we read n elements and need to find the minimum from them.
we have int min; In "min" variable we need to find the smallest element.
first we suppose that the smallest element is the first one, so we give our "min" variable the first element's value. min=a[1];
now we loop over the other elements and check if the coming element is less than the value we had in "min" then we update the value of min.

Here is the full code


LEAVE YOUR QUESTIONS IN COMMENTS

Saturday, June 29, 2013

C++(beginner) string type and its important functions

String is a set of characters.It can contain any character.We declare a string variable like this "string s;" here s is our string.For declaring a string we must first include the string class (look in an intermediate section to learn  classes) it by writing it in the top of our code. ( #include <string>)
First important thing is to know how to call a character from a string.There are a few ways to do that.First is all known [] operator.
Imagine we have string s="ABCDEF";
s[1] will be 'B'.(note that the numerating of characters is starting from 0 not 1 in other words s[0]=A, s[1]=B, s[2]=C;}
other way of calling a character is the function called "at".We write it like this s.at(int b); As we can see this function requires a parameter.
b is the parameter and it is the index of the character we want to call.this function returns character so we need to have a separate character to use this function.(char c; c=s.at(1); here c will be equal to 'B';).This way is much safer than the previous one.
Another way is to go with a loop if we need all of the characters and use the iterators to call them.This is the safest and the fastest way.(Learn about the iterators in C++ intermediate section)
Adding character to the string.
we have string s.We can add a character to the end of the string buy using a very useful and fast function called push_back.It looks like this s.push_back(char c).c must be a character.Sometimes people get errors which are very common. note that when you use a character you write it with this symbol '. With strings " symbol.char c='a' but string s="aaaa".It is also possible to add a character to the end of string by just writing s=s+c;(c is a character).It is also possible to add a character from other places of the string.We have a useful function called insert. We can use insert function to add a character or even a group of characters in the middle of a string.
This is it's construction s.insert(int index,string str), here we are adding str starting from index in our string .For example we have
s="AAAAAAA"; and we have str="BB"; If we write s.insert(1,str); now s will be ABBAAAAAA;
We have a function for checking is our string empty or not.It looks like this s.empty() (note that this function has no parameters and is a boolean function which returns true or false).
function erase.
function erase looks like this s.erase(index1,number)those are two integers which is needed to say from what point we want to erase and how many symbols we want to erase.Example string s="ABCCCDA"; s.erase(1,3), now our string will be "ACDA";
function find.
this is one of the most important functions .This function is searching for a string in a string.Example we have string s="ABCCDAA";
and string str="BCC"; now if we want to find str in s we write s.find(str); if program finds str in s it will return the index the starting point of the string found in s.
String type and its functionsAnd for checking how long our string is we write function called size. int b=s.size(); it returns an integer a number of characters in the string.

C++(beginner) writing our first functions

In C++ we can write our own functions and we can use them in our code. If we need to do the same action many different times on our code , instead of copy-pasting it we can use functions.


The space I marked red is the place when we are going to write our functions. This is the form of the function

the type of the returned value       name of the function ( parameters list on our function)
{
                       actions;
                 return something;
}


every function does something and has to return something.

example we want to calculate the sum of 2 numbers

int sum(int x, int y)
{
      return x+y;
}
the parameters are x and y. When we will call this function in our code we will have to insert 2 integers in our function and we will get the sum of those numbers. X and Y can't be used outside our function, their life starts when we call our function and ends when our functions stops.

If we want our function to do something but do not return anything, in the place where for writing the return type we are writing a word "void". (just like in our main code. "Main" is a function and we write void main() to say that our main function is not going to return anything.)


Functions make our code shorter, easier for understanding. Of course function can be without any parameters.

IF YOU HAVE ANYTHING TO SAY,ASK, SUGGEST PLEASE LEAVE THAT IN COMMENTS.

Thursday, June 27, 2013

C++ (beginner) arrays

Lets try to solve the following task.
input 5 numbers and output them separating with enter.
pretty simple right?
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e;

now, what if the task was a bit different calculate the sum of 100 numbers
I am sure that it is not comfortable to write
int a,b,c,d,e,f,g,h.....,aa,bb;

That is why we have arrays.
Arrays are sets of integers.

we declare arrays like this

type    array name [ the number of elements in array];
  example
int a[100];
int c[200];
double b [56];

The integers in array can be called using their indexes. "a" is not a variable but "a[1]" "a[50]" "a[23]" are variables.

now our task wanted to output 100 number separated with enter.

As you can see in the picture I declared an array of 101 elements. Indexing in C++ starts with 0 so a[0] is also a variable too.

There are multi dimentional arrays in C++. so you can always write
int a[10][10][10][10];
now our array has 4 indexes. imagine arrays like they are a mathematical coordinate system.
 int a[10][10];
so in a coordinate (1,2) we can keep a number.
a[1][2]=32;
int a[12][12][12];
a[1][5][3]=567;
in a coordinate (1,5,3) we have a value of 567.




Wednesday, June 26, 2013

C++ (beginner) loops and their operators

There are 2 main types of loops in C++

the "for" loop and the "while" loop

for

This is the standard form of "for" loop
for( the first value of a certain variable  ; the condition which will stop the loop if it is true;  change the variable somehow)

this might look a bit complicated so lets show it on example
Lets try to count the sum of numbers from 1 to 100 with a loop.

Lets have a look at the "for" statement

for(i=1;i<=100;i++)
as I mentioned above

for statement consists of 3 smaller statements
i=1 this is the starting value of i
i<=100 when this condition will be true the system will exit the loop
i++ we need to change i every time (increase by 1) so that we can count the sum from 1 to 100 and our loop will not be infinite.
lets try to follow system for a few steps so
from the begining sum=0;
now we enter the loop
i=1;
add i to sum, sum became 1
so now i is less than 100 so the loop will continue
i=2
iis less than 100 so the loop is going on
sum=sum+2;
sum became 3
now i=3
i is still less than 100 so continue;

the while loop
the form

while(condition)
{
      dosomething;
}

the while loop has no starting value of a variable or something like this, while has only a condition check the "while" loop will continue until his condition is true;we can count the sum from 1 to 100 using while too.






The idea of counting the sum from 1 to 100 is the same its just written in a different way, the condition checks
while the i variable is less than 100 add i to sum. and also increase i by 1 every time.

there is also another way of using the while loop, the loop will begin then the condition will be checked.

The form is the following

do
{
       actions;
}
while(condition);



you can create an infinite loops
by writing 
for( ; ; )
"for" statement without conditions , or a starting value

2 important operators which can be used in loops

those 2 are "break" and "continue"

the break operator stops the loop no matter other conditions
the continue operator proceeds to the next step.


For making this more clear lets try to count the sum of  numbers from 1 to 100 without any conditions in for loop

With the help of "break" operator the loop will exit when i will be bigger than 100.


Now lets count the sum of even numbers from 1 to 100 using the operator "continue".


As you can see basically every step is to add the variable i to the variable sum but the if(i%2==1) is doing the trick. If i modulo 2 is equal to 1 (aka the number is even) we just skip that step with the help of the operator "continue".



IF YOU HAVE ANY QUESTION  LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.

C++ (beginner) logical operators

If operator


The form of if operator

if ( conditions)
{
          actions
}

The program will do the actions between {    } brackets if the condition is true. 

As you can see in the code above we got outputed "a is bigger than b" because in our condition operator the condition was true.

if we want for our condition to do something if condition is not true we have to add "else" statement.
if(a>b)
    cout<<"a is bigger than b"<<endl;
else
    cout<<"b is bigger than a"<<endl;
now if a will be less than b the line after "else" will be completed.

there are some operators which can be used in if statement

==  (2 equal signs) we write this when we want to check if something is equal to another thing.
if(a==b)
    dosomething

<  checking of one thing is less than the other one
if(a<b)
     dosomething;
> checking if one thing is bigger than the other one.

if(a>b)
   dosomething;

<= checking if something less or equal to the other one.

if(a<=b)
   dosomething;

>= checking if something is more or equal to the otehr one.

if(a>=b)
   dosomething;

if we want to write w reverse statement in other words we want to tell the program to do something if the statement is not true we use ! sign.
example
if(  ! (a==b))
   dosomething;
now he will do something if the statement is not true.

How to check multiple conditions in one IF statement.

there are 2 operators 
&&  and ||
&& stands for the word "and"
|| stands for the word "or"
&& means that the condition is true if both statements are true.
|| means that the condition is true if at least one of the statemnets is true
example

int a=10;
int b=10;
int c=7;
if(a==10 && b==10 && c==10)
     dosomething;

for the condition to be true all of the variables must be 10, this means that the system won't do anything,.

if(a==10 || b==10 || c==10)
     dosmoething;

In this case if at least one of them is equal to 10 the System will proceed. 




  

C++ (beginner) mathematical operators

+

i think everybody knows what + does.
-
the same for -

*
This is the multiplying operator

/
This is the division operator

=
This operator is used to give a variable a value.
example

int b;
b=10;
we used the = operator to give b the value of 10.

Special cases with / operator

10 is not divisible on 3 but if you write this you will get exactly 3 when 10 /3 =3.3333333333(3)
When we divide integer on integer we will always get an integer. The computer will simply do the division and will throw away the decimal part. If you divide float on float you will get float. What to do if we want to divide an integer on integer but to  get a float number.
just write
cout<<double( a) /double(b);
double(a) will see the variable a as a float number and as we already know float number divided on float number is float number.

% operator

the % operator is the modulo operator.
a%b will return us the remainder of a divided on b.




IF  YOU HAVE ANY QUESTIONS , LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.

C++ (beginner) input and output functions

The standard input functions are
cin
cout
scanf
printf

cin

The form of cin function is

cin>>variable1>>variable2>>.....;

example
int a,b,c;
cin>>a>>b>>c;

the same goes for cout but for cout we put '<<' instead of '>>'

example

int a,b,c;
cin>>a>>b>>c;
cout<<a<<b<<c;

lets try to create this project together

open a new win32 console application, name it
then add a source file.


Type  the code from the picture above. 
Now lets run the code ( Ctrl+F5)
now input 3 numbers and press enter


As we can see we input 3 different number (1 2 3) but got 123 as a result.

The program didn't output wrong numbers he just output what we gave him, he outputed 3 numbers without separating them. for clearing this mistake we need to put spaces between numbers.

here is the corrected version, we only inserted 2 spaces betwenn the numbers.
If you want to print and enter after each number just do the following.

every time you cout endl it will hit an enter.

scanf and printf

These are a bit more complicated.
If you have a large input always use scanf and printf these function can read and write data 2 times faster than cin and cout. .
 example of scanf usage


in scanf between "  " we tell scanf what are we going t input
int this case %d means that we are going to input 1 integer,
%d%d%d means that we are going to input 3 integers.
& sigh is a special operator( see the intermediate section).
for now just remember that we need to put & sign in front of the variables we are going to read in scanf but not in front of the variables we are going to output.
as you can see there are spaces between %d in printf function.
in printf function between "  " we are telling printf what we want to output. It doesn't have to contain a variable at all , as in scanf we write %d to tell printf that we are going to print an integer then a space than an integer than a space again than an integer.
If you want to print enter use this sigh \n
example
printf("%d\n%d\n%d",a,b,c);
%d is for integers
but what if we want to input another data type variable
%f- for floats
%c- for characters
%I64d for long long type (__int64)

IF  YOU HAVE ANY QUESTIONS , LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.

C++ (beginner) variable types.how to declare a variable

This is the form of declaring a variable.
(variable type)  variable name;

There are a few variable types in C++.

char -  this is  s simple variable . It can only contain 1 character. It takes 1 byte space.
short int - this is a variable that can store a number. a number from  -32768 to 32767. It takes 2 bytes.
int - this variable is a number variable but its range is higher.  -2147483648 to 2147483647 . It takes 4 bytes
long - this can store a bigger number than the rpevious type.
long long - this can store even a bigger number than long type. it can store upto 2^64. (this variable ,as I mentioned above, long long a;   but it can also be declared __int64 a; 
double - this is a type which can store float numbers. ( double a;     or float a;)
bool - this is a basic boolean variable. It takes 1 bit memory and it has 2 possible values true or false. 

C++ is case sensitive. so int a and int A are different variables. If you write Int a (the first letter is uppercase ) you will sure get a compilation error.

There is a special key word called "unsigned". This can be written befor int,long,long long anf double. As the name suggests unsigned numbers are only the positive numbers. They can store from 0 to 2 times more than the original range of a certain type,

Example of a code

#include <iostream.h>
void main()
{
          int a;
         long b;
         long long c;
         double d;
         unsigned long e;
}



IF  YOU HAVE ANY QUESTIONS , LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.