3 Three Nested For Loops - Very Gainful C++ Learning


Three nested for loops in cplusplus programming language to evaluate loop learning and nested loop learning methods.


#include <iostream>
using namespace std;
void main ()
{
for (int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
cout<<i<<j<<k<<",";
}
}
}
}

BUBBLE SORTING by making class and functions



Bubble sorting  by making class and functions
Bubble sorting useful technique to sort array. Make a class and some related functions and then bubble sorting ..



#include<iostream>
using namespace std;
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[ i ];
}
n = num;

}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[ i]<<endl;
}

void sort()
{
for(int i = 0; i<n-1; i++)
for(int j = 0; j<n-i-1;j++)
if(a[ j+1]<a[ j ] )
{
int temp = a[ j ];
a[j] = a[ j+1 ];
a[ j+1 ] = temp;
}
}
};



void main( )
{
array arr;
arr.read( );
cout<<"The entered values are: "<<endl;
arr.display( );
arr.sort( );
cout<<"arrays values after sorting"<<endl;
arr.display( );

BUBBLE SORTING for character array in c++


Bubble sorting for character array
Bubble sorting is easy way to sort (arrange ) an array. which is either for character or for integers.




#include <iostream>
using namespace std;
void main ()
{
          const char size=50;
          char a[size]= {'a','v','x','d','s','e','f','e','s','e','g','e','d','y','s','w','g','a','f','b','d','b','c','x','c'};
          cout<<"The unsorted array is";
          for(int i=0;i<size-1;i++)
                   cout<<a[i]<<",";
          cout<<"\n sorted array is"<<endl;
         
          for(i=0;i<size-1;i++)
          {
                   for(int j=0;j<size-i-1;j++)
                             if(a[j]>a[j+1])
                             {
                                      int temp=a[j];
                                      a[j]=a[j+1];
                                      a[j+1]=temp;
                                     
                            
                            
                             }
                             cout<<a[j]<<",";

          }

}

LINEAR SEARCH for an array in c++




Linear search for an array



Linear search is used to search out the required number from a very large array of elements




#include <iostream>
using namespace std;
void main ()
{
          const int size=5;
          int a[size]={4,5,6,7,7};
         
         
          int n;
          cout<<"enter the no which you want to search:";
          cin>>n;
          bool found=false;
          for(int i=0;i<size&&!found;i++)
                   if(a[i]==n)
                             found=true;
                   if(found ==true)
                             cout<<"Number:"<<n<< "is found at location:"<<i<<endl;
                   else
                             cout<<"Number is NOT found";
}




LINEAR SEARCH by making functions and class






Linear search by making class and functions


Linear search  which is good for small data. Make a class array and some functions to make linear search easiy.



#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;

}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
void search()
{
bool Found = false;
int key;
cout<<"enter key: "<<endl;
cin>>key;

for(int i= 0; i<n && !Found; i++)
if (key == a[i])
if(Found)
{
bool Found = true;
}
if(Found)
{
cout<<"found"<<endl;
}

else
{
cout<<"not found"<<endl;
}
}

};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
arr.search();
}

BINARY SEARCH by making functions and class in c++








Binary search by making class and functions


Binary search is faster and useful. you have to make a class and some related functions and take a binary search ..



#include<iostream>
using namespace std;
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for (int i = 0; i<size; i++)
a[i] = 0;
n = 0;
}

void read()
{
int num;
cout<<"how many values you want to enter ?"<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"Enter value:"<<endl;
cin>>a[i];
}
n = num;

}

void display()
{
for (int i = 0; i<n; i++)
cout<<a[i];
cout<<endl;
}

void search()
{
bool found = false;
int low = 0;
int high = n-1;
int key;
cout<<"enter key:"<<endl;
cin>>key;
while(low <= high && !found)
{
int mid = (low + high)/2;

if (key == a[mid])
found = true;
else if(key<a[mid])
high = mid - 1;
else
low = mid + 1;
}

if(found == true)
cout<<"Found"<<endl;
else
cout<<"not found"<<endl;
}

};

void main()
{
array arr1, arr2;
arr1.read();
cout<<"the entered array is : "<<endl;
arr1.display();
arr1.search();
}

                        

MERGING TWO ARRAYS in c++



Merging two arrays
First take two arrays sort them and merge them into third array.

·                     
·                   
#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values: "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void merge(Array a1, Array a2)
{
int i=0,j=0;
while (i< a1.n && j< a2.n )
{
if (a1.a[i] < a2.a[j])
{
a[n] = a1.a[i];
i++;
}
else
{
a[n] = a2.a[j];
j++;
}
n++;

}
while (i < a1.n)
{
a[n] = a1.a[i];
i++; n++;
}
while (j < a2.n)
{
a[n] = a2.a[j];
j++; n++;
}
}
};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
cout<<"for 2nd array ";
a2.read();
a2.display();

a3.merge(itm,a2);
a3.display();

}


BINARY SEARCH in c++




Binary search

Binary search is useful for large data. It is faster than linear search but complex.Here an example of linear search...





#include <iostream>
using namespace std;
const int size=6;
void main ()
{

          int a[size]={4,5,6,7,7,8};
         
         
          int n;
          cout<<"enter the no which you want to search:";
          cin>>n;
          bool found=false;
          int low=0,high=size-1;
          int mid=(low+high+1)/2;
          while(low<=high&&!found){
                   if(a[mid]==n)
                             found=true;
                   else if(a[mid]>n)
                             high=mid-1;
                   else
                             low=mid+1;
                   mid=(low+high+1)/2;

}
          if(found==true)
                  
                             cout<<"NO:"<<n<< "is found at location:"<<mid<<endl;
                   else
                             cout<<"NOT found";
}

STATIC DATA MEMBERS & CONSTANT MEMBER FUNCTIOS in C++


STATIC DATA MEMBERS & CONSTANT MEMBER FUNCTIONS

Static data members

static data member is a data member that is shared by all the objects of a class.Only one copy of static data member is kept in memory
By this example your idea will be clear about static data members

static data members
#include <iostream>
using namespace std;
class alpha
{
private:
            int id;
            static int count;
public:
            alpha()
            {
                        count++;
                        id=count;
            }
            void print()
            {
                        cout<<"My id is"<<id;
                        cout<<"count is"<<count;
            }
};

int alpha ::count=0;   //definition of count

void main ()
{
            alpha a1,a2,a3;
                        a1.print();
            a2.print();
            a3.print();
}


This example will clear your idea . This simple ,easy and awesome  space for beginners

....................................................................................................................................................................................


Constant data members

If you dont want to change data members then keyword const is used.

This example demonstrates const member functions

class aclass
{
private:
           int alpha;
public:
           void nonFunc()                  // non const member function
{
alpha=100;                           //ok
}
void conFunc() const                  // constant member function


{

            alpha=100;                 // error , cannot modify a member
}
};




This example surely help you in  understanding of const member function
This is a completely solved example.



#include <iostream>
using namespace std;
class rational
{
private:
            int num;
            int dnum;
public:
            rational():num(1),dnum(1)
            {}
            void get ()
            {
                        cout<<"enter numerator";
                        cin>>num;
                        cout<<"enter denomenator";
                        cin>>dnum;
            }
            void print ()  const      // const keyword is used
            {
                        cout<<num<<"/"<<dnum<<endl;
            }
            void multi(rational r1,rational r2)
            {
                        num=r1.num*r2.num;
                        dnum=r1.dnum*r2.dnum;
            }
};
void main ()
{
            rational r1,r2,r3;
            r1.get();
            r2.get();
            r3.multi(r1,r2);

            r3.print();
           
}



For practice you should solve one more programming example like distance class. Practice makes a man perfect

POINTERS in C++


Pointers

A variable that holds an address value is called a pointer variable or simply pointer.Pointers are hobgoblin of c++ programming.

What are pointers for ?          Here some common uses are

·                     Accessing array elements.
·                     Passing arguments to a function when the function needs to modify the origonal argument.
·                     Passing arrays and strings to function.
·                     Obtaining memory from the system.
·                     Creating data structures such as linked lists.

Pointers are important feature of c++ programming. But many other languages such as java , visual Basics have no pointers. You can do a  lot of programming without pointers but pointers are assentials for c++ programming language.
The idea behind pointers is not complicated.Every byte in computer memory has an address.Addresses are  numbers,just as for houses on a street.

The address of operator &:

Address occupied by a variable using the address operator &. Here is a short example that will help you in understanding that topic.


#include <iostream>

using namespace std;

int main ( )
{

int var1 = 10;

int var2 = 20;

cout << &var1<<endl;

cout << &var2<<endl;

return 0;

}

Run this program on your system and machine and then see what the address of var1 and var2 .
Address is not the value of var1 which is 10. Address would be in the form of 6to7 digits.

Address by pointer variables:

By the address we can find where things in memory.

What is the data type of pointer variable ?

A pointer to int  ,is not type int .You might think a pointer data type would be called something like pointer or p or ptr.

Example of pointer ( address variables )


#include <iostream>

using namespace std;

int main ( )
{

int var1 = 10;

int var2 = 20;

cout << &var1<<endl;

cout << &var2<<endl;

int *ptr ;

ptr = &var1 ;

cout <<ptr ;

ptr = &var2 ;

cout<< ptr;


return 0;

}


Sample run can be shown when you run this program on your system.


Pointers must have a value:

An address like 0*8ffff4ff can be thought of as a pointer constant.

When we first define a variable , it holds no value. It may hold a garbage value.But in case of pointers , a garbage value is the address of something in memory. So before a pointer is used   ,  a specific address must be placed in it.

ptr = &var;