DirectorySoftwareBlog Details for "Learning Computer Programming"

Learning Computer Programming

Learning Computer Programming
I post articles mostly on Learning Computer Programming in general and specifically using C plus plus. This blogsite is created as a part of my hobby of computer programming, I also share my experiences in a teaching manner.
Articles: 1, 2, 3, 4, 5, 6

Articles

Macros with Arguments
2007-07-18 15:05:00
Have a look at the following code: #include<iostream.h> #define MAX 10 void main(void) { for(int i=1;i<=MAX;i++) cout<<i<<endl; } Above we have used the type of macro expansion that we learnt in the article Introduction to C++ Preprocessor Directives Do you know that just like functions we can have arguments in the macros too! The following code shows how: #include<iostream.h> // this is how macros with // arguments are defined // NOTE: THERE SHOULD NOT BE // ANY SPACE BETWEEN THE // MACRO TEMPLATE (i.e. AREA) // AND THE PARANTHESIS // CONTAINING THE ARGUMENTS // (i.e. (x)) #define AREA(x) (x*x) void main(void) { // calling a macro // is almost similar // to calling a function cout<<AREA(10); } It is that simple! However, keep one thing in mind as stated in the comments not to put any space between the macro template and the braces containing the argument. #define AREA(...
More About: Macros , Acro
Hurray! My 50th Post
2007-07-18 15:02:00
I am so glad to inform you all that this is my 50th post on this blog. In the span of 57 days this blog has grew in terms of both content and readership. I won’t take much of your time saying it out loud but I want to thank all of my readers and subscribers for their valuable co-operation! Thanking You Arvind Gupta
More About: Personal , Post
Introduction to Basic Encryption and Decryption
2007-07-18 08:34:00
Encryption is a familiar sounding word which means to convert readable data in such a form that it becomes un-understandable or un-meaningful. It is employed almost everywhere where any confidential data is needed to be kept or transferred. Encryption goes hand in hand with decryption which means to convert un-meaningful encrypted data to its original meaningful form. Here in this article we are going to design two functions, one for encryption and other for decryption, to illustrate the basic concept of encryption and decryption. Please note that the example program provided in this article is for illustrative purpose only, there are a few limitations in the program which limits its practical use. How encryption and decryption works? The main concept behind encryption is to convert the readable data into something which looks un-meaningful to us. It could be achieved in various ways but the simplest one is to change the ASCII code of the data. Ex. #incl...
More About: Encryption , Tricks , Introduction , Basic , Methods
Changing the case (lower, upper) of Strings
2007-07-17 14:25:00
In this article, we will be designing two functions to change the case of strings. One would change a string from lower case to upper case while the other would do the opposite. Although we have pre-defined functions for doing this in a header file, but this article is for those who dare to know how all these operations are done internally. Changing the case: How is it done? The main theory lies in the way C++ treats character constants and strings. Have a look at the following code: #include<iostream.h> void main(void) { char first='A'; char second=65; cout<<first; cout<<endl; cout<<second; cout<<endl; } whose output is: A A Press any key to continue This is because ?A? and its ASCII code 65 are equivalent to the compiler and in c++ we can manipulate it in whatever way we like. Now look at the following code: #include<iostream.h> void main(void) { char arr[4]="ABC"; char arr2[4]={65,66,67}; ...
More About: Tricks , Case , Lower , Methods
Merging Two-Dimensional Arrays (Matrices)
2007-07-16 11:54:00
In the article Merging One Dimensional Arrays , we discussed how to merge one-dimensional arrays, in this article we will be discussing about the merging of two-dimensional arrays. Merging as you know is the process of combining two similar things. In the context of arrays, it means to form a big array from two smaller arrays which has all the elements from both the arrays. In case of one-dimensional arrays there is only one way in which two arrays can be merged but in case of two-dimensional arrays there are two ways. Suppose, we have the following two 2d arrays (matrices): mat1={ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } and mat2={ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} } then they can be merged in the following two ways: merge_row={ {1, 2, 3, 10, 11, 12}, {4, 5, 6, 13, 14, 15}, {7, 8, 9, 16, 17, 18} } merge_col={ {1, 2, 3}, {4, 5, 6}, {7, 8, ...
More About: Methods , Matrices , Rays
Introduction to Linked Lists Part II
2007-07-16 09:02:00
In the previous article Introduction to Linked Lists , we introduced the basic concept of linked list. To make the program (an the article) as simple as possible, we discussed only the addition and display of nodes in the linked list although necessary we didn't’t discussed the deletion of node in that article. In this article we will be discussing about the deletion of nodes from linked lists. Deletion of node (elements) from a linked list The node to be deleted can be represented by many ways but here we will be representing it by its info. So if we have the following linked list And we want to delete node1 then we will express it by its info part (i.e. 10). The main theory behind deletion of nodes is pretty simple. We need to make the link pointer of the node before the target node (to be deleted) to point at the node after the target node. Suppose if we wish to delete node having info as 10 from the above linked list then it will be accompl...
More About: Tricks , Functions , Part
Introduction to Linked Lists
2007-07-15 08:49:00
We have been using arrays to store similar data linearly. While arrays are simple to understand and easy to implement in common situations, they do suffer from some drawbacks which are listed below: Arrays have fixed dimensions, even if we dynamically allocate the dimension it remains constant throughout. So there is a limit to the number of elements it can store. Operations such as insertion and deletion are pretty much difficult to implement and increases the overhead because these operations require elements in the array to be physically shifted. Linked lists overcome these drawbacks and are commonly used to store linear data. Actually elements of linked lists (called as nodes) store two information, data and the link (pointer) pointing to the next elements (node). The elements (nodes) are linked sequentially with the help of link pointers. So we can say that linked lists are collection of nodes which have data and ...
More About: Lists , Introduction , Methods , Algorithms , Intro
Introduction to Recursive Functions
2007-07-14 08:28:00
Recursion is a process of defining something in terms of itself. Function recursion therefore means to define a function in terms of itself, in other words a function that calls itself inside its body is known as recursive functions. Example: void func (something) { something… something… func(something); } Notice how the function func () is calling itself! Why use recursive Functions ? In most cases recursive functions can be replaced by iterative statements, then why use recursive functions? Here are a few points that justify its use: Recursive functions make the code easier and simpler to understand. There are certain algorithms that could be very easily implemented using recursion but are pretty much difficult to implement using iterative or other non-recursive methods. Some of the people tend to think recursively, so their thoughts can be better implemented using recursion. Below is...
More About: Tricks , Introduction , Methods , Intro
Pointers to Function
2007-07-13 07:50:00
Function Pointers is a rather confusing yet powerful feature of C++ programming language. Even if you have programming for a while I bet you have seen nothing like it, simply because they aren't’t needed in everyday programming. Their most common use is in writing compilers and interpreters. The main theory behind function pointers is, just like the contents of a variable can be accessed by a pointer, much the same way functions can also be invoked (called) by referencing it by a pointer to that function. Although variables and functions are two separate identities, both of these are stored at some memory address which can be pointed (and hence accessed) by a pointer. Going in detail of the working of function pointers will only confuse you so we skip that for now and move on to a simple example program. The function defined in the program is made as simple as possible to reduce confusions. Please note that the program only illustrates how function poi...
More About: Tricks , Functions , Function
Your Questions my Answers
2007-07-11 08:48:00
[I have started this new section in which will answer your questions publicly. Therefore, if you are having any confusions or problems, you can e-mails me at one.arvind@gmail.com. Please note that… read more] Problem: I want to sort each row of a matrix in ascending order, after which I need to display another matrix whose elements are the column indices of the elements of the original matrix with respect to the corresponding elements of the sorted matrix. Please give a program to do so. eg. Original matrix={ {9,4,2}, {7,3,8} }; Sorted Matrix= { {2,4,9}, {3,7,8} }; Column Index= { {2,1,0}, {1,0,2} }; Dominic Nzegbule, Student Nigeria Solution: Nice question Dominic, here is the solution. I will not discuss anything here as I have included enough comments in the program itself. // C++ program to so...
More About: Questions , Answers , Problems , Questions and Answers
Ask Your Questions
2007-07-11 08:38:00
Few days back when I was going through my inbox, I saw a few e-mails of peoples asking for solutions to their problems. Some of questions were so generic that it could come up in anybody’s mind. So I thought of answering a few generic questions publicly so that it could benefit others also. Therefore, if you are having any confusions or problems, you can e-mails me at one.arvind@gmail.com Please note that only selected questions will be answered here; all the others will be answered by e-mail (as far as possible). After writing this the number of e-mails will definitely increase, so please be patient as it may take a little while before I could answer them. The questions will be published along with your Name, Country and e-mail address (if only you wish it to be published), so please include them with your e-mail. Don’t hesitate E-mail me at: one.arvind@gmail.com Good-Bye! [NOTE: Before you ask, please use the search feature of this blog, who...
More About: Questions , Questions and Answers
Merging One Dimensional Arrays
2007-07-10 08:08:00
Merging of two arrays means to form one big array from two small arrays, which has element from both the arrays linearly. Ex. if we have the following two arrays: array1 [5] = {1,2,3,4,5} array2 [5] = {6,7,8,9,10} and we merge these two arrays to form an array merge_array, then it would have these elements: merge_array [5] ={1,2,3,4,5,6,7,8,9,10} So, from this example we understand that the array which will hold the merged elements should have a dimension equal to the sum of the dimensions of the two discrete arrays. It is no big deal to merge two arrays, we just need to sum up the dimension of the arrays, and then allocate an array having that dimension. Below is the example program that illustrates this. Please go through the program to understand every bit of it. It is accompanied with enough comments to make everything clear. While two arrays has been merged here, you can easily modify it to merge as many arrays as you wish. // -- Array Merging -- //...
More About: Tricks , Methods , Arrays , Merging , Dimension
Computer Programming: An Article for Beginners
2007-07-09 08:07:00
-Article of the Month You might have read many so-called ?the art of computer programming an? and many ?how to ?? on computer programming, but this one is different. It doesn't?t deals with the technical details of programming rather it deals with what is more important to beginners. This article would help beginners who are having a tough time with programming to get to the right track. It is also for newcomers who want to start programming and want to choose the right programming language. It is not just another guide to learn computer programming; it helps you to minimize your confusions while learning computer programming. It is not an article which talks about the history or something like that, it is a practical guide. This article is written keeping in mind what the beginner?s confusions are and how to minimize them. Careers in Computer Programming As a professional having a degree from any reputed institution you have a good chance of making a career in computer programmi...
More About: Introduction , Ming
Sorting Two-Dimensional Arrays
2007-07-08 07:56:00
Do you know how a 2D array is stored in the memory while the memory is only one-dimensional? The answer is simple, all the arrays are stored linearly in the memory, be it 2D array or 3D, only the representation is such that to make it easy to reference. Therefore, if a two-dimensional array has the following elements: 1 2 3 4 5 6 7 8 9 in the memory, it will be like this: 1 2 3 4 5 6 7 8 9 just because memory is linear, and cannot have dimensions. It is the language that represents 2 D arrays as such while in the memory it is always linear. This property of 2D arrays will be used to sort them, because sorting linear data is much easier. We don’t need much discussion on this, so here is the example program, please read the comments where all things are elaborated // --Sorting Program-- // ------------------- // Example Program to sort // 2D array using linear // representation of the array #include<iostream.h> #define MAX 3 void main(v...
More About: Tricks , Arrays , Dimension , Rays
Data Structures: Introduction to Queues
2007-07-04 15:43:00
Queue is a linear data structure in which data can be added to one end and retrieved from the other. Just like the queue of the real world, the data that goes first into the queue is the first one to be retrieved. That is why queues are sometimes called as First-In-First-Out data structure. In case of queues, we saw that data is inserted both from one end but in case of Queues; data is added to one end (known as REAR) and retrieved from the other end (known as FRONT). The data first added is the first one to be retrieved while in case of queues the data last added is the first one to be retrieved. A few points regarding Queues: Queues: It is a linear data structure; linked lists and arrays can represent it. Although representing queues with arrays have its shortcomings but due to simplicity, we will be representing queues with arrays in this article. Rear: A variable stores the index number in the array at which the new data will be added (in the queue). Front: It is a variab...
More About: Data , Introduction , Class , Methods , Algorithms
Introduction to C++ Preprocessor Directives
2007-07-03 14:51:00
As the name suggest, C++ Preprocessor is an integrated program in C++ that preprocesses every program we write before compilation. Preprocessor commands or directives are special functions that are executed or processed before the compilation of the program. Although we can write program without the knowledge of preprocessor directives but why underutilize features when we have them? Every preprocessor directive starts with a # symbol. While we can have them anywhere in the program but they are almost always used at the starting of the program before any function is defined. Knowingly or unknowingly, we have been using one preprocessor directive. It is the #include directive, which is most commonly used to include header files in the program. While there are many types of preprocessor directives, here we will only be discussing about Macro Expansion directives and that also in the simplest sense! Macro Expansion Directives Have a look at the code below: #define MAX 10 cout<&l...
More About: Introduction , Variables , Intro , Esso , Process
Simple Problems in C++ Part IV
2007-07-01 14:38:00
Take a break and have a look at these problems! Problem 1: Write a Program in C++ to print the following pattern. Example: ********** ******** ****** **** ** Solution: // Example Program in C++ // to print a pattern // that looks like a reverse // pyramid of '*' #include<iostream.h> void main(void) { int i,j,k; for(i=0;i<=10;i++) { for(j=5;j<i+5;j++) cout<<" "; for(j=5;j>i;j--) cout<<"*"; for(j=5;j>i;j--) cout<<"*"; cout<<endl; } } Problem 2: Write a program in C++ to find the row sum and column sum of a two dimensional array. Example: 1 2 3 = 6 4 5 6 = 15 7 8 9 = 24 12 15 18 Solution: // Example Program in c++ to find the // row sum and column sum of a 2 // dimensional array #include<iostream.h> void main(void) { int arr[3][3]={1,2,3,4,5,6,7,8,9}; int row_sum[3], col_sum[3]; int i,j; for(i=0;i<3;...
More About: Tricks , Problems , Simple , Part
Insertion and Deletion of elements in a Sorted Array
2007-07-01 08:37:00
In the article Insertion and Deletion of elements in an Array, we saw how data is inserted and deleted in an unsorted array. In that case, we needed two information, the element as well as the position, for insertion while for deletion we needed the position. In the case of sorted arrays insertion and deletion takes pace in a slightly different way. The following example will clarify this: Suppose we have the following array: arr[5]={1,2,3,4,5} And, we need to insert the element 6, so where it should be inserted? We can?t insert it at any place because then the array might not remain sorted. Therefore, we let the program to automatically calculate the position suitable for the new element, so that the array remains sorted even after insertion. Now, arr[5]={1,2,3,4,6} Now, suppose we wish to delete the element 6, in this case we don?t use the position for deletion because we don?t know where the element was placed by the program, so rather than referencing the position for deletion, ...
More About: Elements , Sorting , Methods , Algorithms , Arrays
Data Structures: Introduction to Stacks
2007-06-30 15:30:00
In the previous article, we saw how data is inserted and deleted in an array. In that case we could insert data at any place throughout the array. However, there are situations when we only need to add and retrieve data form the ends of the array. Stacks are one of the examples of this. Stacks are data structures in which data could be added and retrieved only from one end (also known as the TOP of the stack). Suppose we insert 5, 6, 9 to the stack consecutively then while retrieving the first one to be retrieved will be 9 then 6 and then 5. That is why stacks are also known as Last-In-First-Out (or LIFO) structure. A few terms regarding stacks: Stack: Stack is a user-defined data structure. It is most commonly represented by linked-lists and arrays. In this article, we will be representing stacks with arrays. Push: Adding data to the stack is known as pushing. Pop: Retrieving data from the stack is known as popping. Let us have look at this process with the help of an examp...
More About: Data , Introduction , Methods , Algorithms , Data Structures
Insertion and Deletion of elements in an Array
2007-06-30 07:57:00
Suppose you are storing temperature data for a few months and you forgot to store the temperature of a particular day (say 5th day) then you need to INSERT that temperature after the 4th element of the array and in the other case if you accidentally stored duplicate data then you need to DELETE the duplicate element. Apart from these simple examples, there are many other uses of insertion and deletion The array to which the element is to be inserted or deleted can be of two types unordered (unsorted) and ordered (sorted). Here we will be discussing about the insertion and deletion of element in an unordered or unsorted array. For insertion in these types of arrays, we need to have two information, the element to be inserted and the position to which it will be inserted. For deletion, we only need the position. Suppose we have the following array: arr[5]={5,7,2,1,3} And we need to insert the element 6 at the 2nd position, after insertion: arr[5]={5,6,7,2,1} Notice how the last elemen...
More About: Elements , Methods , Algorithms , Arrays , Array
Algebra of Matrices (2D Arrays) Part II
2007-06-29 11:58:00
As you know from the previous article, matrices are 2D arrays. In the previous article, we saw how two matrices are added and subtracted. In this article, we will continue our discussion on algebra of two matrices by discussing how two matrices are multiplied. Multiplication of two Matrices Multiplication of two matrices mat1[a x b] and mat2[p x q] is only valid if b=p. While there are many algorithms by which two matrices can be multiplied, here I?ll give you the most simple algorithm. Others are used when efficiency matters. Algorithm for Multiplication of two Matrices Suppose, Two 2D arrays to be mat1 [p][p] and mat2 [p][p] having same number of rows and columns. A third 2D array, mul [p][p] to store the result. Here is the algorithm: FOR I = 0 TO (p-1) FOR J = 0 TO (p-1) mul [I][J] = 0 FOR K = 0 TO (p-1) mul [I][J] += (mat1 [I][K] * mat2 [K][J]) END OF INNER LOOP END OF OUTER LOOP Below is a example program which illustrates the multiplication of two ma...
More About: Part , Methods , Algorithms , Arrays
Algebra of Matrices (2D Arrays)
2007-06-26 07:58:00
In the programming sense, Matrices are Two Dimensional or 2D arrays. Just as Matrices have rows and columns, similarly 2D arrays too have rows and columns. There are many mathematical operations like addition, subtraction, multiplication etc. which can be performed on matrices, and therefore to 2D arrays also. In this article, we will be discussing about the addition and subtraction of two 2D arrays (Matrices). Addition of two Matrices (2D arrays) For addition of two matrices both the matrices must have the same dimension. Ex. if matrice one has the dimension p x q then matrice two must have the dimension p x q. In the addition process, each of the element of the first matrice is added to the corresponding element of the second matrice and result is stored in the third matrice having the same dimension (i.e. p x q). Below is the algorithm for adding two matrices. Algorithm for adding two Matrices Suppose, Two 2D arrays to be mat1 [p][q] and mat2 [p][q] having p rows and q columns...
More About: Methods , Algorithms , Arrays , Rays
Binary Search: A Method of Searching
2007-06-25 12:41:00
Binary Search method is popular for searching a specific item in an ordered array (sorted). It can perform the search in minimum possible comparisons, but it needs the array to be sorted in any order. Practically, it is used when the data is itself sorted initially or needs to be sorted for other activities also. This is because you don?t want to first sort the data and then use binary search, in that case use of linear search would be practical. Binary Search Algorithm Suppose, The array to be AR[SIZE] having SIZE number of elements. L is the index number of the lower element. We take it to be 0. U is the index number of the upper (last) element. It will be (SIZE-1). ITEM is the data that needs to be searched. beg, last and mid are variables of type int(eger). Here is the algorithm: LET beg = L AND last = U REPEAT STEPS 3 THROUGH 6 TILL beg<=last mid = ( (beg+last)/2) IF AR[mid] = ITEM THEN ITEM IS AT POSITION mid BREAK THE LOOP IF AR[mid] <...
More About: Searching , Methods , Algorithms , Arrays
Sorting an Array using Bubble Sort
2007-06-25 12:39:00
In this article we will see how an array can be sorted using Bubble Sort Technique. Sorting , as you know, is the method of arranging the elements of an array in an order (ascending or descending). The basic idea behind bubble sort method of sorting is to keep on comparing adjoining elements of the array from the first until the last and interchanging them if they are not in proper order. The whole sequence is repeated several times when the array becomes sorted. Bubble Sort Algorithm Suppose, The array (to be sorted) to be AR[SIZE] having SIZE number of elements. L is the index number of the lower element. We take it to be 0, since the whole array has to be sorted. U is the index number of the upper (last) element. It will be (SIZE-1). Here is the algorithm of sorting the array using bubble sort FOR I = L TO U FOR J = L TO (U-1) IF AR[J] > AR[JA1] THEN temp = AR[J] AR[J] = AR[J+1] END OF INNER LOOP END OF OUTER LOOP Now that you know the algorith...
More About: Methods , Algorithms , Arrays
Classes & Objects: Dynamic Memory Allocation
2007-06-24 08:19:00
Just as variables of pre-def data type are dynamically allocated, same way objects of class can also be dynamically allocated. While the method of doing this is no different but the way members of such objects are accessed is a bit different. This is why I thought of writing a separate article on it. Before discussing any further, look at the program below: // Example Program in C++ #include<iostream.h> // class class myclass { // by default everything is // private to the class int var1, var2; void add(); public: int sum; void input(int,int); }; // function definitions void myclass::input(int a, int b) { var1=a; var2=b; add(); } void myclass::add() { sum=var1+var2; } // main function starts void main(void) { myclass obj; // simple object myclass *pobj; // pointer // of type 'myclass' pobj=&obj; // now 'pobj' pointer // points to the object 'obj' of // class 'myclass...
More About: Memory , Classes , Class , Objects , Arrays
Features of C++, we haven't discussed...
2007-06-23 08:38:00
In this article, we will discuss about certain features of C++ Programming Language, which we haven?t discussed so far. All the features are discussed with the help of example programs so that you could straightaway learn to use them. Let us have a look at them one-by-one: (Things are discussed in the comments wherever necessary) EXAMPLE 1: Variation of for-loop // Example Program in C++ #include<stdio.h> #include<conio.h> void main(void) { char ch; for(;;) { // this loop will run // infinitely till // 'q' is pressed ch=getch(); if (ch=='q' ch=='Q') break; } printf("out of loop "); } EXAMPLE 2: Variation of for-loop // Example Program in C++ #include<iostream.h> void main(void) { char str[20]="learn C++"; for(int i=0;str[i];i++) ;// notice that there // is nothing in the // body of the loop cout<<"length of str :"<<i; cout<<endl; } O...
More About: Features , Functions , Disc , Haven , Feat
Reference Variables in Detail
2007-06-23 08:35:00
A reference is an alias or alternate name for an object. It is just like having two variables having the same memory address, if one of them changes the other will also change. Actually, a reference is an implicit pointer. Suppose, we have an int(eger) variable num and its reference refnum, then both will always have the same value no matter which one gets alerted. We can have independent references, reference parameters in functions, functions returning references etc. that are illustrated below with the help of example programs:- EXAMPLE 1: Independent Reference to a variable // Example Program in C++ #include<iostream.h> void main(void) { int num; // notice the declaration of a reference // & has nothing to do with the 'address // of' operator (&amp;) int &refnum=num; cout<<"enter value for num: "; cin>>num; cout<<"now refnum="<<refnum; cout<<" enter value for refnum: "; cin>>refnum; cout...
More About: Functions , Variables , Ferenc , Tail
A Multi-Purpose String Class in C++
2007-06-22 09:27:00
NOTE: Here I present you with a String Class in C++. We have pre-defined string class (CString in Microsoft Visual C++) which has similar but more powerful to the one presented here but using something is one thing and learning how it works is another. Here I show you how string functions actually work. All the functions are programmed from the scratch without using any other standard library function. Look at the program (Class) carefully and try to understand how each of the function is working. //----------------------------- //-----------myClass----------- //----A String Class in C++---- #include<iostream.h> #include<stdlib.h> class myString { private: // these functions are not // needed outside the class void allocate(int); void copy(char *, char *); public: char *string; // member functions myString(); myString(char *); int getLength(); int getLength(char *); void empty(); bool isEmpty(); void putStr...
More About: Multi , Functions , Purpose , Methods
Destructor Functions in Detail
2007-06-20 08:02:00
If you don?t know what destructor functions are, read Introduction to Constructor and Destructor functions of a Class. As the example programs in this article makes use of the dynamic memory allocation of C++, so please read Introduction to Dynamic Memory Allocation in C++ , in case you missed it. When does the destructor function gets invoked The examples below illustrates when the destructor function gets invoked: Example 1: //Example Program in C++ #include<iostream.h> class myclass { public: ~myclass() { cout<<"destructed "; } }; void main(void) { myclass obj; cout<<"inside main "; } OUTPUT: inside main destructed Press any key to continue As I said in the other article, destructors get invoked when the object of a class goes out of scope. In this case, the object goes out of scope as the program terminates. So the destructor gets invoked just before the program?s termination. Example 2: //Example Pro...
More About: Classes , Functions , Function , Tail
Introduction to Dynamic Memory Allocation in C++
2007-06-19 08:29:00
Suppose you are making a C++ program to store a particular number (specified by the user at runtime) of phone numbers. What will you do? You cannot declare array of 10 or 20 elements because you don’t know how many numbers the user will enter. He may want to enter 1, 10 or even 100 phone numbers. So what will you do? Actually, you can do two things to achieve this, that are listed below: You can create a very large int(eger) array (say, of 500 elements) , in this case whether the user wants to store 1 or 500 phone numbers, the memory used will always be the same (large!). You can make use of C++’s dynamic memory allocation to allocate only the needed amount of memory. The first choice is a very bad way of programming, nevertheless it works fine, you should never employ such an inefficient method of programming. So, you are left with only one choice to use the Dynamic Memo ry Allocation of C++ programming language,...
More About: Introduction , Pointers , Intro
More articles from this author:
1, 2, 3, 4, 5, 6
46843 blogs in the directory.
Statistics resets every week.


Contact | About
© Blog Toplist 2008 - Supported by Web Catalog - SEO by FeWorks
eXTReMe Tracker