#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string strfile="filenamewithabsolutepath.txt"; ifstream ifptr; ifptr.open(strfile); if(!ifptr.is_open()) { cout<<"Unable to open file\n"; } else{ //read the file till the end while(!ifptr.eof()) { string line; getline(ifptr,line); cout<<"line:"<<line<<"\n"; }//end of while }//end of else return 0; }//end of main
Friday, 29 June 2012
Program to Read a file line by line in C++
Friday, 22 June 2012
Program to Reverse a String
Code:
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<iostream> |
Output:
Original string:abcdefg Reverse string:gfedcba
Saturday, 21 April 2012
Arrays, 2D arrays, 3D arrays.
Array:
An array is a collection of homogeneous variables, all the variables in the array should be of same datatype.
declaration of an array:an array is declared like a normal variable but it is followed by a pair of square braces.
#include<stdio.h> void main() { //the array size is specified in square braces int a[10]; //we can assign values during declaration itself using flower braces. float f[5]={0,1,2,3,4}; }
Sample program:
//This program demonstrates //1.How to store values in an array //2.How to access values stored in array #include<stdio.h> void main() { //Array declaration int array[10]; int i=0; //storing values in the array printf("enter the values of 10 nos:\n"); for( i=0;i<10;i++ ) { scanf("%d",&array[i]); } //accessing the values stored in array for( i=0;i<10;i++ ) { printf("array location:%d has value:%d\n",i,array[i]); } }
Two dimensional arrays:
A two dimensional array can be considered as a matrix like storage structure, elements are stored in rows and columns.
general format:
variablename[m][n];
m is the number of rows
n is the number of columns.
ex:
int a[2][4]={1,2,3,4,5,6,7,8};
logical storage
[ 1 2 3 4
5 6 7 8]
Note:
The matrix like storage structure is only logical assumption, but the actual storage of variables in memory need not be so.
Sample program:
//This program demonstrates //1.How to store values in a 2Darray //2.How to access values stored in a 2D array #include<stdio.h> void main() { //Array declaration int array[2][2]; int i=0; int j=0; //storing values in the array printf("enter the values of 4 nos:\n"); for( i=0;i<2;i++ )//row traversal { for( j=0;j<2;j++)//column traversal { scanf("%d",&array[i][j]); } } //accessing the values stored in array for( i=0;i<2;i++ )//row traversal { for(j=0;j<2;j++)//column traversal { printf("array location:%d has value:%d\n",i,array[i][j]); } } }enter the values of 4 nos:1 2 3 4array location:0 has value:1 array location:0 has value:2 array location:1 has value:3 array location:1 has value:4
Three Dimensional Arrays:
Three dimensional arrays are like multiple copies of the same matrix.
Standard format:
variablename[no of matrix][no of rows][no of columns]
ex: int a[2][3][3]; two matrices with 3 rows and 3 columns.
sample program:
//This program demonstrates //1.How to store values in a 3Darray //2.How to access values stored in a 3D array #include<stdio.h> void main() { //Array declaration int array[2][2][2]; int i=0; int j=0; int k=0; //storing values in the array printf("enter the values of 8 nos:\n"); for(k=0;k<2;k++)//traverse the matrix copy { for( i=0;i<2;i++ )//row traversal { for( j=0;j<2;j++)//column traversal { scanf("%d",&array[k][i][j]); } } } //accessing the values stored in array for( k=0;k<2;k++)//traverse the matrix copy { printf("Matrix no:%d\n",k); for( i=0;i<2;i++ )//row traversal { for(j=0;j<2;j++)//column traversal { printf("array location:%d has value:%d\n",i,array[k][i][j]); }//end of j loop }//end of i loop }//end of k loop }
enter the values of 8 nos:
1 2 3 4 5 6 7 8Matrix no:0 array location:0 has value:1 array location:0 has value:2 array location:1 has value:3 array location:1 has value:4 Matrix no:1 array location:0 has value:5 array location:0 has value:6 array location:1 has value:7 array location:1 has value:8
By using three dimensional array, the elements were stored in two 2*2 matrices in the above program.
Monday, 16 April 2012
Recursion
Recursion is a concept in which a function calls itself till a certain condition is satisfied, the function call states are stored on a stack.(ie the local variables,registry values etc)
Once the required condition is satisfied, the control is returned back in order the functions were pushed into stack.
sample program:
Output:
Once the required condition is satisfied, the control is returned back in order the functions were pushed into stack.
sample program:
#include<stdio.h> void TrialRecursion(int count); void main() { int count=5; TrialRecursion(count); } //this function prints the welcome message 5times or equivalent to count void TrialRecursion(int count) { //required condition for final return if(count==0) return; //else print welcome message and call itself with count-1 as parameter else { printf("Welcome msg no:%d\n",count); //Recursive Call TrialRecursion(count-1); return; } }
Output:
Control Flow and stack during Recursion:
Function Control Flow
|
count
|
Stack
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
5
|
Action: push function state
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
4
|
Action: push function state
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
3
|
Action: push function state
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
2
|
Action: push function state
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
1
|
Action: push function state
|
|||||
void TrialRecursion(int count)
{
if(count==0)
return;
else{
printf("Welcome msg no:%d\n",count);
TrialRecursion(count-1);
return;
}
|
0
|
Action: pop all till control returns to main function
|
|||||
void main()
{
int count=5;
TrialRecursion(count);
}
|
|
|
|||||
|
|
|
Advantages:
- Can simplify a complex task by breaking into smaller repeatable tasks.
- Provides Alternative to complex looping.
- Simple code.
Disadvantages:
- More Memory is utilised to store the function state onto the stack.
- May lead to stack overflow in case of large functions with lots of variables.
- Debugging the program is difficult.
- Decreases the time efficiency of program.
Subscribe to:
Posts (Atom)