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();

}