![]() |
|
#11
|
||||
|
||||
VII.The if else StatementsThese are extremely simple.
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
||||
|
#12
|
||||
|
||||
Example Programe #1EP1:
->Make a programme to find all the prime numbers from 2 to a given number CPP / C++ / C Code:
|
|
#13
|
||||
|
||||
IX.ArraysSuppose you had 50 people and you wanted to store all their ages, according to their roll numbers. So the 'obvious' thing to do is declare 50 int variables and assign them, perhaps like this:
int _0=17,_1=45,_2=12, .... etc. Note: The _ is required as a variable cannot start with a number. Now, to avoid this cumbersome and unwieldy code, we have the concept of arrays. An array is basically a collection of variables of the SAME type. They are declared and accessed in the following way. CPP / C++ / C Code:
Please note that numbering starts from 0. So if you want to assign the age 17 to the first variable, age[0]=17; And obviously the last variable is accessed by the number one less than the total number of variables. age[49]=18; Arrays can be initialized when declared in the following manner: CPP / C++ / C Code:
And by the way, and array of characters is a string. It can be declared in the previous fashion: char name[8]={'P','h','a','n','t','o','m'}; Or: char name[8]="Phantom"; Remember the " " for strings and ' ' for characters? You might be wondering why I've used 8 as the number of variables instead of 7 as Phantom has 7 characters. In the case of strings, a NULL character is always added to the end of the string to show that the string ends here. The reason why we require a null character is the characters are actually stored in consecutive memory blocks one-byte-wide. The only information given is the address of the block which contains the first character. cout stops only when it encounters the null character. So, when we declare the array, we have to make an allowance for that. And the null character is added by the compiler, as you can see, I just used "Phantom". {More on how arrays work in Pointers} __________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
|
#14
|
||||
|
||||
Example Programme #2EP2:
->Make a programme to input an array of intergers and sort it in the ascending order CPP / C++ / C Code:
In EP2 I've used a sorting algorithm known as a SELECTION sort. Here, we start with the first value, array[0] and check it with array[1]. If array[0] is greater that array[1] then we swap the values as we want it in ascending order. Once that swap is done, we will be comparing the old array[1] with the rest of the variables and will keep repeating it until all the variables are compared with each other. |
|
#15
|
||||
|
||||
Example Programme #3EP3:
->Make a programme to merge 2 arrays into one array and make sure that none of the values exceed 50 and is greater than 0. CPP / C++ / C Code:
|
|
#16
|
||||
|
||||
X.Multi-dimensional ArrayWe can extend the concept of arrays in the following manner. In, the previous example the array was 1-dimensional, or just extended in a straight line. We can also declare a 2-dimensional array so that we have a matrix of variables of the same data type.
Example: int chessboard[8][8]; I've used the chessboard as an example as it's easy to point out how this 2-D array works --------------------------------- ¦0,0|0,1|0,2|0,3|0,4|0,5|0,6|0,7¦ ¦1,0|1,1|1,2|1,3|1,4|1,5|1,6|1,7¦ ¦2,0|2,1|2,2|2,3|2,4|2,5|2,6|2,7¦ ¦3,0|3,1|3,2|3,3|3,4|3,5|3,6|3,7¦ ¦4,0|4,1|4,2|4,3|4,4|4,5|4,6|4,7¦ ¦5,0|5,1|5,2|5,3|5,4|5,5|5,6|5,7¦ ¦6,0|6,1|6,2|6,3|6,4|6,5|6,6|6,7¦ ¦7,0|7,1|7,2|7,3|7,4|7,5|7,6|7,7¦ --------------------------------- As you can see from this crude chessboard, it has 8 rows and 8 columns, just like the 2-D array. It's called 2 dimensions as we require 2 variables to point out a variables' location on it. For example, suppose we're on a straight road, then we only need 1 variable, to show where we are taken some point as the origin CPP / C++ / C Code:
Therefore by saying 25km from the origin to the right is sufficient. So only one dimension exists: length. But, suppose we wanted to fix a point on the paper ( 2-dimensions) , then we'll need 2 variables | | | | | |----------X | | | | | | |_ _ _ _ _ _|_ _ _ _ Therefore 2 dimensions are there: length and breadth | | | | | |_ _ _ _ _ _ _ _ / / / / / And the world we live in is 3-dimensional: length, breadth & height. Mathematically, these 3 dimensions are called, x,y & z co-ordinates. Alright, back to the chessboard. Suppose I wanted to place a value on the square (4,3): chessboard[4][3]=val; And suppose you wanted to declare the 2-D array and initialize it at the same time: CPP / C++ / C Code:
Note how a 2D array is initialized. The number of programmes you can make out of this concept are endless. You can simulate matrix operations too. Now, let's try a 3-D array. Here we can consider the array to be as pages of a book: Example: int book[ page no ][x co-ordinate ][y-coordinate]; Similarly, you can make an n-dimensional array if you want to, but arrays chew up a lot of memory so most people stop at 3. |
|
#17
|
||||
|
||||
XI.StructuresXI.a)What is an object?
Consider a blue print for a digital diary was made. Using this we can make hundreds of objects based on the same model, with the same memory capacity, games, power options, scheduler, phone book etc. But is each model the same? Of course not, each model depends on what the user puts into it. For example, the 2 phone books must contain different phone nos. not the same ones! So would the scheduler.. And some users use more memory than the others.. Basically, an object is the basic model created from a blue-print. XI.b)Defining a object An array holds variables of the same type, but a structure can hold variables of any type. Consider a table, it contains many drawers. Suppose each drawer contained 3 objects, how would you access the object? First you would access the drawer, and then any one of the objects. CPP / C++ / C Code:
XII.b.i)Accessing the elements So, how would I use this structure? Simple, we use the DOT operator. According to Appendix B the DOT operator is a C++ component selector. Basically, it is used to select the components within objects of a structre or class such as the variables INSIDE it. {More on classes later}. So, I wish to gain all the information of a student, this is how I'd do it. CPP / C++ / C Code:
Now, suppose I wish to store the entire class' information, what would I do? Declare an array, of course!! CPP / C++ / C Code:
Alright, now suppose you wished to use the structure for only one variable. Then, struct { (..) } (name) ; |
|
#18
|
||||
|
||||
XII.Functions :: Intro, Calling & DefinitionFunctions are the second-most important concept in a language. A function is basically a block of code which does some work. Got that? A function basically allots a group of program statements under a name which can be called any time within the programme.
Consider the following: CPP / C++ / C Code:
Alright, consider a rock-crushing machine; the machine is the function, the rocks are the data or the ARGUMENT, and the powdered rock is the output or RETURN. Well, hope that concept is clear... CPP / C++ / C Code:
Hope you understood that...it's really very simple. XII.a)Definition To define a function we employ the above syntax. Thus whenever we call the function (next topic) the compiler automatically inserts the code in the function definition. XII.b)Calling a function Calling a function is simple. CPP / C++ / C Code:
Note, add(a,b). As you can see we are working on the 2 variables, a & b, hence they are the arguments. We can always consider the entity add(a,b) as the return value, i.e. c or a+b. So WHEREVER we use a value, we can use the function. CPP / C++ / C Code:
The return data type, can be anything. A special one has been provided by the compiler, it's called VOID. As you can guess, it's void, or returns nothing. I hope the signifiance of main is visible now. main() is a function run by the compiler, and as we don't return any value to anything, we have void main(). Functions are of infinite use when it comes to simplifying a programme. My first function: CPP / C++ / C Code:
|
|
#19
|
||||
|
||||
XII.Functions :: Some Things You Ought to Know About FunctionsXII.c)Some Things You Ought to Know About Functions
XII.c.i)Inline Functions Inline functions basically store the function code directly "in line" with the main programme. The benefits of this is that even though it hogs memory, it's faster than the usual function as the usual function is stored at a separate memory block, and is accessed by a "jump" from the main programme involving many steps (which doesn't have to be explained) which generally slows down the programme. CPP / C++ / C Code:
Obviously the only difference is that we add an inline before the return type. Btw, inline is nothing but a request to the compiler, i.e. it may choose to ignore it if the function is too large, etc.. XII.c.ii)Function Prototypes A function prototype states the return type, argument no and type, and name of the function without actually defining it. This is used to clean up the code and also speeds up the compiler. CPP / C++ / C Code:
XII.c.iii)Global & Local Variables Definition A global variable is accessible to ALL functions whereas a local variable is only accessible to the function it's declared in. For example, in the silly math problems, we can eliminate the need for an argument by making the variables gloabal. CPP / C++ / C Code:
As you can see, the variables a,b, & c were accessible to main() and add() as they were globally declared. Scope Resolution Operator (: This operator has allowed many a sadistic teacher to set ridiculously hard programmes for exams. If you remember (III.c.i), you can't have 2 variables with the same name. Well, not exactly...you can't have 2 GLOBAL or 2 LOCAL variables with the same name. CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#20
|
||||
|
||||
Example Programme #4EP4:
->Make a calculator with addition, multiplication, subtraction, and division abilities. CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The