Archive

Posts Tagged ‘c++ tutorial’

Logic to Find Exponents with Integral Powers

July 22nd, 2011 No comments

If you've noticed, In C Programming, you cannot use a^b directly to find the value of an exponent. Although, there is a pow(a, b) function that gives you the result. But, it's always nice to work with the basic logic.

If you look at the definition of an exponential:

"Exponentiation is a mathematical operation, written as a^n. When n is a positive integer, exponentiation corresponds to repeated multiplication." - Wikipedia

We're gonna work out our logic using this very basic definition [especially the part where it says - exponentiation corresponds to repeated multiplication ;)]

Code:

main()
{
int base, index;
printf("Enter the base\n"); /* say, 2 */
scanf("%d", &base);
printf("Enter the index\n"); /* say, 3 */
scanf("%d", &index);

/* always remember that 2^3 is actually 2x2x2 ie-2 multiplied by itself 3 times */

int i;
int power=base; /* power is initiallized to 2 */
for(i=1;i<index;i++)
{
power=power*base; /* 1st loop : power = 2*2, 2nd loop : power =(2*2)*2 */
}

printf("\n%d", power);
getch();
}

It's pretty easy to understand using an example. Refer to the example in comments to see how the code works. Also, this is not the only way. Think of how you could achieve the same with a different (and perhaps, better) code.

[C Tutorial 3] The First Program

July 1st, 2011 No comments

A program to display "Hello Sweety!" as the output. All the explanations are in the form of comments. And everything between /* and */ is a comment and everything else is a part of the code. To prevent confusion, the program is first written without the comments.

The Program (without comments/explanation) :

#include <stdio.h>
#include <conio.h>

int main()
{
printf("Hello Sweety!");
getch();
return 0;
}

The Program (with comments for explanation) :

#include <stdio.h> /* a header file which has the function
printf used to display text as output */

#include <conio.h> /* another header file that has the function
used below */

int main() /* main - the main function,
int - function returns an integer type,
() - for arguments, no arguments in this case */

{ /* main function begins */

printf("Hello Sweety!"); /* shows Hello Sweety! on the screen */
getch(); /* waits for a character before closing program */
return 0; /* since main function should return integer */

} /*main function ends */

It may not look like it but this is one of the most important programs you will ever learn. The syntax used in the program can be generalized as following :

#include <filename.h> is the general syntax you'll use to include any header file. You'll need to include many header files as you deal with more complex problems. stdio.h contains the printf function that we've used to display output.

return-type function-name (arguments) {...} is used to define a function. In this case the function is called main and the return type is integer with no arguments.

printf("..."); a function that shows whatever is inside the double inverts on the output. This is your first C statement. All C statements end with a semi-colon (;)

getch(); this function waits for a character input from the user and then exits the program. Try running the code without it and with it and you'll know what i mean. This function is actually part of a header file conio.h but will work without it on most compilers - but no harm adding the conio.h header

return value; Since the main function must return an integer value, we return 0. However, your program will run without it, but you should make it a practice since you'll need it in the long run.

By the way, All the code we write goes in a new source file on your compiler. (File > New > Source File) Once you type in the above code, you need to compiler & run your program. It should give the following output :

first_program_output

[Note - we added the header file conio.h for getch();]

[C++ Tutorial 1] The Basics

July 10th, 2010 No comments

Hello world! I just thought maybe a bunch of C++ tutorial could help. So here they are. To start with, a program is a simply, a set of instructions for the computer to do. The programmer is he who makes the instructions. And the language in which they are written is the programming language.

Low-Level VS High-Level Language

Now, just like you, your computer has its own languages-called the low-level languages(these are the ones your computer knows but they're hard for you to understand). Then there are the ones which resemble your language including words like if, else, while, for called the high-level languages(these are the ones you know but your computer does not).

THE COMPILER

Now, a compiler is just another program which makes your high-level languages understandable to your computer. It is a translator that translates the code written in a high-level language into assembly language(low-level). So, the first thing you need is the compiler. I use Dev C++. You can use whichever you like. (The most popular being Visual C++).

Programming PARADIGM

This is more theoretical and would not much really affect your coding skills, but anyhow-a paradigm is a style of programming. For instance, a procedural paradigm would concentrate on the sequence of the program, a generic paradigm would concentrate on generalization concepts like templates, etc. C++ is a multi-paradigm language.

[Go to Tutorial 2]

An Introduction to Pointers in C++ Programming

July 1st, 2010 1 comment

A pointer, formally called a "derived data type" defines where to get the value of a data variable. It itself, does not store the data but the memory location of the variable.

So, in short, it is a (derived)data type which points to the data of the variable but stores only the memory location of the variable.

The pointer uses the deference(*) operator & the reference/address(&) operator. Some of the stuff we can do using pointers is shown down there.. ;)

int *p1;          /* all of these can be used
int* p2;                  to declare pointers */
int*p3;

int *ptr, a;      //declared pointer and variable
ptr=&a;           //pointer now contains the address of a

cout<<ptr;        //gives the memory location of a
cout<<*ptr;       //gives the value stored at a

It's very important not to get confused between the &variable, pointer, and *pointer.

&variable is used to assign the pointer an address of the variable, pointer gives the memory location of the variable and *pointer gives the value of the variable.