Friday, November 28, 2014

C++ using pointers pro



A simple program using pointers

 
#include 
#include
using namespace std;
int main()
{
   int intnum; 
   float floatnum;
   int *pintnum;
   float *pfloatnum;
   intnum = 10;
   floatnum = 12.34;
   pintnum = &intnum;
   pfloatnum = &floatnum;
             cout << "Before increment: " << endl;
   cout << "\t IntNum is: " << intnum << endl;
   cout << "\t FloatNum is: " << floatnum << endl;
   cout << "\t pIntNum contains: " << *pintnum << endl;
   cout << "\t pFloatNum contains: " << *pfloatnum << endl;
   (*pintnum)++;  
   (*pfloatnum)++;
   cout << "After increment: " << endl;
   cout << "\t IntNum is: " << intnum << endl;
   cout << "\t FloatNum is: " << floatnum << endl;
   cout << "\t pIntNum contains: " << *pintnum << endl;
   cout << "\t pFloatNum contains: " << *pfloatnum << endl;
   return 0;
}

Pointer to characters

 
#include 
#include  
using namespace std;
int main()
{
   char *buffer = "dummy content.";
   cout << "Original content of buffer:--> " << buffer << endl;
   cout << "Enter a sentense: " << endl;
   gets(buffer);
   cout << "Now the Buffer contains:--> " << endl;
   cout << buffer << endl;
   return 0;
}

Pointer Arithmetic

 
#include 
#include
using namespace std;
int main()
{
   int intarray
[10]; 
   int        *pInt;        
   int        i;            
   for(i = 0; i < 10; i++)
   {
               intarray[i] = i+1;
   }
   pint = intarray;
   for (i = 0; i < 10; i++)
   {
               cout << *pint << ' ' ;
               pInt++;            
               cout << *(pint++) << ' ' ;
   }
   cout << endl;
   return 0;
}

Array of Pointers

 
#include 
#include 
using namespace std;
int main()
{
            const char* const Fortune[5] = 
            {" Earth is a great funhouse without the fun.",
                 " There is always more hell that needs raising.",
                 " Confession is good for the soul, but bad for the career.",
                 " Live in a world of your own, but always welcome visitors.",
                 " The man who laughs has not yet been told the terrible news."
                 };
   cout << "Here are the 5 fortunes: " << endl;
   for(int i = 0; i < 5; i++)
   {
               cout << Fortune[i] << endl;
      }
   }                    

A program demonstrating modularity in C++

 
#include 
#include 
#include
using namespace std;
 
int main()
{
   int number;
   int abs_number;
   cout << "This program finds the absolute value of an integer." << endl;
   cout << "Enter an integer (positive or negative): ";
   cin >> number;
   abs_number = Abs(number);
   cout << "The absolute value of " << number << " is " << abs_number;
   cout << endl;
   return 0;
}

Variables and Constants

 
#include 
#include
using namespace std;
const int Const_val = 5;        
int main()
{
   int    iValue;  
   float  fValue;  
   char   cValue;  
   
   iValue = 1234;    
   fValue = 1234.56; 
   cValue = 'A';     
   
   cout << "Integer value is: " << iValue << endl;
   cout << "Float value is: " << fValue <<  endl;
   cout << "Character value is: " << cValue << endl;
   cout << "The constant is: " << Const_val << endl;
   return 0;
}

Basic IO

 
#include 
#include
using namespace std;
int main()
{
  float gallons, liters;
  cout << "Enter number of gallons: ";
  cin >> gallons; 
 
  liters = gallons * 3.7854; 
  cout << "Liters: " << liters << endl; 
  return 0;
}

No comments:

Post a Comment