Now, how do we use variables in C/C++.
We first need to declare these variables, then we may
designate it with some value or have some input pushed into it. Look at the
following example:
#include
<iostream>
using
namespace std;
int
main(){
int a;
cin>>a;
cout<<”The Number you entered
was: ”<<a<<endl;
}
NOTE:
‘cin’ is a function which is followed by ‘>>’ signs to get some input
stored in a variable in C++.
In
this example, what we really did was simply declare a variable, get some input
into it and then print it out. If we want, we may manipulate the Variables to
do a host of things. For Example, we could have:
#include
<iostream>
using
namespace std;
int
main(){
int a,b,c;
cout<<”Please Enter three
Numbers!”
cin>>a>>b>>c;
cout<<”The Sum of the Numbers
you entered is:”<<a+b+c<<endl;
cout<<”The Product of the
Numbers you entered is:”<<a*b*c<<endl;
cout<<”The product of the
first and second number, divided by the third number will be:”<<(a+b)/c<<endl;
cout<<”The Difference of the
first and second numbers is:”<<a-b<<endl;
cout<<”The Remainder when the
second number is divided by the third is:”<<b%c<<endl;
}
NOTE:
Here we make use of a host of operators to manipulate numbers. We add, multiply,
divide, get the Remainder and subtract using brackets whenever necessary.
You
may try similar programs on your own Computer as well.
Formally,
there are 5 main operators in C/C++, (apart from the equal, greater than and
less than operators),
These
are:
1)
‘+’ operator: To add
2)
‘–‘ operator: To Subtract
3)
‘*’ operator: To Multiply
4)
‘/’ operator: To Divide
5)
‘%’ operator: To get the Remainder after dividing a certain number from another
number. Eg: 5%2 = 1, 10%3 = 1, 25%7 = 4 etc.
NOTE:
You can only perform the operations on numbers(ints and floats), Furthermore,
the 5th Operator is meant only for Ints.You could not use any of
these operators on chars.
Have you wondered how do you store lists of numbers
in a Computer?
You do it using what’s called a Container in C/C++.
In C, There is just one type of Container i.e. Array.
You may declare an array as: ‘array data type’ ‘name of array’[‘size of array’],
and you can know whatever value is stored as the ith member of an
array by using the ‘[]’ operator as: ‘array name’[i], Look at the following
example:
#include
<iostream>
using
namespace std;
int
main(){
int n;
cout<<”Please enter how many
numbers would you like to store in an array?”<<endl;
cin>>n;
int array[n];
for(int i = 0;i<n;i++){
cin>>array[i];
}
for(int j = 0;j<n;j++){
cout<<”The
”<<j<<”th Number you entered was: ”<<array[j]<<endl;
}
}
Unlike
C, In C++ you are provided with a host of other containers as well, of course
Arrays are still available in C++, but apart from arrays you also get to use:
Vectors (arrays with no size), Sets(Arrays which are already sorted in
ascending or descending order just as you enter numbers into the set).
We
shall look more closely on these containers in the next section.
Now,
Remember,
We told you earlier that cout, cin, printf, scanf are functions that someone
else has defined for us and stored in the computer for our convenience?
As
it turns out, We could define functions on our own as well!
At
this point, you should know that generally a function takes an input and returns
an output but not in all cases.
Remember
we told you that the main() function with which itself is a function in C/C++,
which gets compiled/executed first before all other parts of the programs.
(Here, by other parts of the program we mean other functions.)
NOTE:
We shall also tell you about Scope through the following example!
Look at the following piece of code:
#include <iostream>
using namespace std;
int x,y;
void Initialize(){
cout<<”Please
Enter Two Numbers:”<<endl;
}
void Input(){
cin>>x>>y;
}
int add(int a, int b){
return
a+b;
}
int main(){
Initialize();
Input();
cout<<”The
Sum of the Numbers is: ”<<add(x,y)<<endl;
}
Now, Here you may notice how we’ve
defined two new functions in the very same way in which we had previously been
defining main(). The General Syntax for a function is:
‘data type of what the function returns’
‘function name’(‘input variables needed by the function’){
‘statements,
expressions part of the function’
return
‘output value as variable or constant’
}
You should get familiar and friendly to
the Code above and see how it does so.
NOTE: We write ‘void’, when any function
doesn’t needs to take inputs or return outputs in respective places.
Finally, Scope of a variable is somewhat
complicated in practice but very simple in theory, that is when we declare any
variable outside of any function, it’s said to be global and its value will be
the same across all functions which use this variable, whereas if some variable
with the same name is declared and is present in two different functions, then
it will have different values in the execution of both the functions and is
called a Local Variable.
Comments
Post a Comment