Thursday, January 29, 2015
C++ Program for Tic-Tac-Toe Game
#include<iostream>
#include<iomanip>
using namespace std;
const int ROW=3,
COL=3;
char fGame[ROW][COL];
void show_board();
void row_col_input(int &);
void position_filled_input(int &,int &);
bool first_winner();
bool second_winner();
void main_tasks();
int main()
{
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
fGame[i][j]='*';
}
}
cout << "\t\t\tTIC-TAC-TOE\n"
<< "\t\t\t-----------\n\n";
cout << "Instructions:\n"
<< "\t\tYou will enter the row and column number to make a tick at that position."
<< "Players 1's tick will be represented by an 'X' and player 2's tick will be represented by an 'O'";
show_board();
main_tasks();
return 0;
}
void show_board()
{
cout << "\n----------------------------------------------------\n";
cout << "\nHere is the board:\n\n";
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
cout << setw(10) << fGame[i][j];
}
cout << "\n\n";
}
cout << "----------------------------------------------------";
cout << "\n";
}
void row_col_input(int &position)
{
cin >> position;
while(position<1||position>3)
{
cout << "\nWrong input! Please enter a valid number(from 1to3)!\n"
<< "Enter again: ";
cin >> position;
}
}
void position_filled_input(int &row,int &col)
{
while( fGame[row][col]=='X'||fGame[row][col]=='O')
{
cout << "\nRequired position is already filled!";
show_board();
cout << "\nEnter again:\n";
cout << "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
}
}
bool first_winner()
{
bool flag=0;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[i][j]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[j][i]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
if(fGame[i][i]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
int temp=ROW;
for( int i=0 ; i<ROW ; i++ )
{
temp--;
if(fGame[i][temp]=='X')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
return 0;
}
bool second_winner()
{
bool flag=0;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[i][j]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
for( int j=0 ; j<COL ; j++ )
{
if(fGame[j][i]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
break;
}
if(flag)
return 1;
for( int i=0 ; i<ROW ; i++ )
{
if(fGame[i][i]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
int temp=ROW;
for( int i=0 ; i<ROW ; i++ )
{
temp--;
if(fGame[i][temp]=='O')
flag=1;
else
flag=0;
if(!flag)
break;
}
if(flag)
return 1;
return 0;
}
void main_tasks()
{
int row,col;
bool flag,win=0;
for( int i=0 ; i<5 ; i++ )
{
cout << "\nPlayer 1's Turn:\n"
<< "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
position_filled_input(row,col);
fGame[row][col]='X';
show_board();
flag=first_winner();
if(flag)
{
cout << "\nCongratulations! "
<< "Player 1 has won the game!\n";
win=1;
break;
}
if(i==4)
break;
//Player 2 starts from here
cout << "\nPlayer 2's Turn:\n"
<< "Enter Row: ";
row_col_input(row);
cout << "Enter column: ";
row_col_input(col);
row--;
col--;
position_filled_input(row,col);
fGame[row][col]='O';
show_board();
flag=second_winner();
if(flag)
{
cout << "\nCongratulations!"
<< " Player 2 has won the game!\n";
win=1;
break;
}
}
if(!win)
cout << "\nThe game has tied.\n"
<< "No player wins\n";
cout << "\nFinal position of the game...\n";
show_board();
cout << endl;
}
Sunday, January 25, 2015
C++ Program for finding the mode of an array
#include<iostream>
using namespace std;
/*Function Prototype*/
int getMode(int arr[], int size); //for getting the mode of a value
int main()
{
int Array[50]; //defining array of large size(50)
int size,index;
cout << "Enter the array size(should be between 1to50): ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the range of 1 to 50
{
cout << "Wrong Input(should be between 1to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the Array elements:\n";
for( int i=0 ; i<size ; i++ ) //for getting individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> Array[i];
}
index=getMode(Array,size); //calling function by passing array and its size
if( index==-1 ) //if array has no mode
cout << "\nAll the numbers exist the same number of times.\n";
else //if the array had a mode
{
cout << "\nThe number that occurs most often in the array is: "
<< Array[index]
<< endl;
}
return 0;
}
/*Functio Definition*/
int getMode(int arr[], int size)
{
int occur[50]; //array for getting the occurance of each element
int index=-1;
if(size==1) //if size is '1' then it is the only mode of the array
return 0;
//getting the occurance of each element
for( int i=0 ; i<size-1 ; i++ )
{
for( int j=i+1 ; j<size ; j++ )
{
if( arr[i]==arr[j] )
occur[i]+=1;
}
}
//checking if the array has a mode or not
for( int i=1 ; i<size ; i++ )
{
if( occur[i]!=occur[i-1] )
{
index=1;
break;
}
}
//will execute if the array has a mode
if(index==1)
{
index=0;
int large=occur[0];
//for getting the largest occurance
for( int j=1 ; j<size ; j++ )
{
if( occur[j]>large )
{
large=occur[j];
index=j; //stores the index of the element that has largest occurance
}
}
}
return index; //returns the index of the element that has largest occurance
}
using namespace std;
/*Function Prototype*/
int getMode(int arr[], int size); //for getting the mode of a value
int main()
{
int Array[50]; //defining array of large size(50)
int size,index;
cout << "Enter the array size(should be between 1to50): ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the range of 1 to 50
{
cout << "Wrong Input(should be between 1to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the Array elements:\n";
for( int i=0 ; i<size ; i++ ) //for getting individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> Array[i];
}
index=getMode(Array,size); //calling function by passing array and its size
if( index==-1 ) //if array has no mode
cout << "\nAll the numbers exist the same number of times.\n";
else //if the array had a mode
{
cout << "\nThe number that occurs most often in the array is: "
<< Array[index]
<< endl;
}
return 0;
}
/*Functio Definition*/
int getMode(int arr[], int size)
{
int occur[50]; //array for getting the occurance of each element
int index=-1;
if(size==1) //if size is '1' then it is the only mode of the array
return 0;
//getting the occurance of each element
for( int i=0 ; i<size-1 ; i++ )
{
for( int j=i+1 ; j<size ; j++ )
{
if( arr[i]==arr[j] )
occur[i]+=1;
}
}
//checking if the array has a mode or not
for( int i=1 ; i<size ; i++ )
{
if( occur[i]!=occur[i-1] )
{
index=1;
break;
}
}
//will execute if the array has a mode
if(index==1)
{
index=0;
int large=occur[0];
//for getting the largest occurance
for( int j=1 ; j<size ; j++ )
{
if( occur[j]>large )
{
large=occur[j];
index=j; //stores the index of the element that has largest occurance
}
}
}
return index; //returns the index of the element that has largest occurance
}
C++ Program for finding the median of a number
#include<iostream>
using namespace std;
int main()
{
int i;
//const int SIZE=10;
const int SIZE=9;
int Array[SIZE];
int occur[SIZE]={0};
cout << "Enter the Array:\n";
for( i=0 ; i<SIZE ; i++ )
{
cin >> Array[i];
}
for( i=0 ; i<SIZE-1 ; i++ )
{
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Array[i]==Array[j] )
occur[i]+=1;
}
}
for( i=0 ; i<(SIZE-1) ; i++ )
{
int temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Array[j]<Array[i] )
{
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
}
}
double Median;
if( SIZE%2==0 )
{
int M1,M2;
M1=SIZE/2;
M2=M1-1;
Median=(static_cast<double>(Array[M1])+Array[M2])/2;
cout << "\nThe Median is: "
<< Median
<< endl;
}
else
{
int Mid=SIZE/2;
Median=Array[Mid];
cout << "\nThe Median is: "
<< Median
<< endl;
}
return 0;
}
using namespace std;
int main()
{
int i;
//const int SIZE=10;
const int SIZE=9;
int Array[SIZE];
int occur[SIZE]={0};
cout << "Enter the Array:\n";
for( i=0 ; i<SIZE ; i++ )
{
cin >> Array[i];
}
for( i=0 ; i<SIZE-1 ; i++ )
{
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Array[i]==Array[j] )
occur[i]+=1;
}
}
for( i=0 ; i<(SIZE-1) ; i++ )
{
int temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Array[j]<Array[i] )
{
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
}
}
double Median;
if( SIZE%2==0 )
{
int M1,M2;
M1=SIZE/2;
M2=M1-1;
Median=(static_cast<double>(Array[M1])+Array[M2])/2;
cout << "\nThe Median is: "
<< Median
<< endl;
}
else
{
int Mid=SIZE/2;
Median=Array[Mid];
cout << "\nThe Median is: "
<< Median
<< endl;
}
return 0;
}
C++ Program for counting the frequency of a number in an array
#include<iostream>
using namespace std;
/*Function Prototype*/
int countFrequency(int arr[], int size, int value); //function for conunting how many times a number exists in an array
int main()
{
int array[50], //defining array of large size(50)
size,value,
occurance;
cout << "Enter the size of array(should be between 0to50): ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the available range
{
cout << "Wrong Input(should be between 0to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the array elements\n";
for( int i=0 ; i<size ; i++ ) //for getting input for individual elements
{
cout << "Enter the element "
<< (i+1)
<< ": ";
cin >> array[i]; //getting input for each element
}
cout << "Enter the value to find its frequency: ";
cin >> value;
occurance=countFrequency(array,size,value); //calling function by passing array , its size and value to be searched
if( occurance==0 ) //if the desired value dioes not exist
cout << "The number "
<< value
<< " does not exist in the array.\n";
else //if the desired value existed
{
cout << "The number "
<< value
<< " exists in the array "
<< occurance
<< " time(s).\n";
}
return 0;
}
/*Function Definition*/
int countFrequency(int arr[], int size, int value)
{
int occur=0;
for( int i=0 ; i<size ; i++ )
{
if(arr[i]==value) //testing each element
occur++; //calculating how many times the given number existed
}
return occur;
}
using namespace std;
/*Function Prototype*/
int countFrequency(int arr[], int size, int value); //function for conunting how many times a number exists in an array
int main()
{
int array[50], //defining array of large size(50)
size,value,
occurance;
cout << "Enter the size of array(should be between 0to50): ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the available range
{
cout << "Wrong Input(should be between 0to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the array elements\n";
for( int i=0 ; i<size ; i++ ) //for getting input for individual elements
{
cout << "Enter the element "
<< (i+1)
<< ": ";
cin >> array[i]; //getting input for each element
}
cout << "Enter the value to find its frequency: ";
cin >> value;
occurance=countFrequency(array,size,value); //calling function by passing array , its size and value to be searched
if( occurance==0 ) //if the desired value dioes not exist
cout << "The number "
<< value
<< " does not exist in the array.\n";
else //if the desired value existed
{
cout << "The number "
<< value
<< " exists in the array "
<< occurance
<< " time(s).\n";
}
return 0;
}
/*Function Definition*/
int countFrequency(int arr[], int size, int value)
{
int occur=0;
for( int i=0 ; i<size ; i++ )
{
if(arr[i]==value) //testing each element
occur++; //calculating how many times the given number existed
}
return occur;
}
C++ Program for searching a value in an array
#include<iostream>
using namespace std;
/*Function Prototype*/
int linearSearch(int arr[], int size, int value); //for finding a particular value by linear search method
int main()
{
int array[50], //defining array of large size(50)
size,value,
index;
cout << "Enter the size of array: ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the available range
{
cout << "Wrong Input(should be between 0to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the array elements\n";
for( int i=0 ; i<size ; i++ ) //for getting input for individual elements
{
cout << "Enter the element "
<< (i+1)
<< ": ";
cin >> array[i]; //getting input for each element
}
cout << "Enter the value to search in the array: ";
cin >> value;
index=linearSearch(array,size,value); //calling function by passing array , size and value to be searched
if( index==-1 ) //if the desired value dioes not exist
cout << "The number "
<< value
<< " does not exist in the array.\n";
else //if the desired value existed
{
cout << "The number "
<< value
<< " exists in the array at index "
<< index
<< ".\n";
}
return 0;
}
/*Function Definition*/
int linearSearch(int arr[],int size,int value)
{
int index=-1; //intializing index with -1(because -1 can neither be the index)
for( int i=0 ; i<size ; i++ )
{
if(arr[i]==value) //checking if the value desired exists or not
{
index=i; //assigning index the index of the element at which the value exists
break; //break the loop id value is found
}
}
return index; //returning the index
}
using namespace std;
/*Function Prototype*/
int linearSearch(int arr[], int size, int value); //for finding a particular value by linear search method
int main()
{
int array[50], //defining array of large size(50)
size,value,
index;
cout << "Enter the size of array: ";
cin >> size;
while(size<1||size>50) //testing if the size is not in the available range
{
cout << "Wrong Input(should be between 0to50)!"
<< "\nEnter again: ";
cin >> size;
}
cout << "Enter the array elements\n";
for( int i=0 ; i<size ; i++ ) //for getting input for individual elements
{
cout << "Enter the element "
<< (i+1)
<< ": ";
cin >> array[i]; //getting input for each element
}
cout << "Enter the value to search in the array: ";
cin >> value;
index=linearSearch(array,size,value); //calling function by passing array , size and value to be searched
if( index==-1 ) //if the desired value dioes not exist
cout << "The number "
<< value
<< " does not exist in the array.\n";
else //if the desired value existed
{
cout << "The number "
<< value
<< " exists in the array at index "
<< index
<< ".\n";
}
return 0;
}
/*Function Definition*/
int linearSearch(int arr[],int size,int value)
{
int index=-1; //intializing index with -1(because -1 can neither be the index)
for( int i=0 ; i<size ; i++ )
{
if(arr[i]==value) //checking if the value desired exists or not
{
index=i; //assigning index the index of the element at which the value exists
break; //break the loop id value is found
}
}
return index; //returning the index
}
C++ Program for comparing two arrays
#include<iostream>
#include<iomanip> //for setw()
using namespace std;
/*Function prototype*/
int compareArrays(int arr1[], int size1, int arr2[], int size2); //Function for comparing arrays
int main()
{
int arr1[50],arr2[50], //defining arrays of large sizes(50 each)
size1,size2,
result,
i;
cout << "Enter size of Array 1: ";
cin >> size1; //getting size for array 1
while(size1<1||size1>50) //applying condition if the entered size is in the available range
{
cout << "Wrong input(should be between 1to50)!\n"
<< "Enter again: ";
cin >> size1;
}
cout << "Enter size of Array 2: ";
cin >> size2; //getting size for array 2
while(size2<1||size2>50) //applying condition if the entered size is in the available range
{
cout << "Wrong Input(should be between 1to50)!\n"
<< "Enter again: ";
cin >> size2;
}
cout << "Enter elements of Array 1\n";
for( i=0 ; i<size1 ; i++ ) //for getting input for individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> arr1[i]; //getting value for each elemnt of array
}
cout << "Enter elements of Array 2\n";
for( i=0 ; i<size2 ; i++ ) //for getting input for individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> arr2[i]; //getting value for each elemnt of array
}
result=compareArrays(arr1,size1,arr2,size2); //calling function by passing arrays and their sizes
//printing the array elements in a proper formate
cout << "\nThe arrays are...\n";
cout << setw(25) << "Array1\n"; //for printing at a proper place
cout << "Index: ";
for( i=0 ; i<size1 ; i++ )
{
cout << setw(9) << i; //printing indexes
}
cout << endl
<< "Values: ";
cout << setw(8);
for( i=0 ; i<size1 ; i++ )
{
cout << arr1[i] << setw(9); //printing array 1 elements
}
cout << endl;
cout << setw(25) << "Array2\n";
cout << "Index: ";
for( i=0 ; i<size2 ; i++ )
{
cout << setw(9) << i; //printing indexes
}
cout << endl
<< "Values: ";
cout << setw(8);
for( i=0 ; i<size2 ; i++ )
{
cout << arr2[i] << setw(9); //printing array 2 elements
}
cout << endl;
//testing condition if the arrays are equal or some other condition
if(result==0)
cout << "Both the arrays are equal.\n";
else if(result==1)
cout << "Array 1 is greater than array 2.\n";
else
cout << "Array 2 is greater than array 1.\n";
return 0;
}
/*Function Definition*/
int compareArrays(int arr1[], int size1, int arr2[], int size2)
{
if( size1>size2 ) //testing if first array is greater than second array
return 1;
else if(size1<size2) //testing if second array is greater than first array
return -1;
else //will execute if sizes of both arrays are equal
{
for( int i=0 ; i<size1 ; i++ )
{
if(arr1[i]>arr2[i]) //if first array element is greater than that of second array then return 1
return 1;
else if(arr1[i]<arr2[i]) //if second array element is greater than that of first array then return -1
return -1;
}
return 0; //return 0 if elements of both arrays are equal
}
}
#include<iomanip> //for setw()
using namespace std;
/*Function prototype*/
int compareArrays(int arr1[], int size1, int arr2[], int size2); //Function for comparing arrays
int main()
{
int arr1[50],arr2[50], //defining arrays of large sizes(50 each)
size1,size2,
result,
i;
cout << "Enter size of Array 1: ";
cin >> size1; //getting size for array 1
while(size1<1||size1>50) //applying condition if the entered size is in the available range
{
cout << "Wrong input(should be between 1to50)!\n"
<< "Enter again: ";
cin >> size1;
}
cout << "Enter size of Array 2: ";
cin >> size2; //getting size for array 2
while(size2<1||size2>50) //applying condition if the entered size is in the available range
{
cout << "Wrong Input(should be between 1to50)!\n"
<< "Enter again: ";
cin >> size2;
}
cout << "Enter elements of Array 1\n";
for( i=0 ; i<size1 ; i++ ) //for getting input for individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> arr1[i]; //getting value for each elemnt of array
}
cout << "Enter elements of Array 2\n";
for( i=0 ; i<size2 ; i++ ) //for getting input for individual array elements
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> arr2[i]; //getting value for each elemnt of array
}
result=compareArrays(arr1,size1,arr2,size2); //calling function by passing arrays and their sizes
//printing the array elements in a proper formate
cout << "\nThe arrays are...\n";
cout << setw(25) << "Array1\n"; //for printing at a proper place
cout << "Index: ";
for( i=0 ; i<size1 ; i++ )
{
cout << setw(9) << i; //printing indexes
}
cout << endl
<< "Values: ";
cout << setw(8);
for( i=0 ; i<size1 ; i++ )
{
cout << arr1[i] << setw(9); //printing array 1 elements
}
cout << endl;
cout << setw(25) << "Array2\n";
cout << "Index: ";
for( i=0 ; i<size2 ; i++ )
{
cout << setw(9) << i; //printing indexes
}
cout << endl
<< "Values: ";
cout << setw(8);
for( i=0 ; i<size2 ; i++ )
{
cout << arr2[i] << setw(9); //printing array 2 elements
}
cout << endl;
//testing condition if the arrays are equal or some other condition
if(result==0)
cout << "Both the arrays are equal.\n";
else if(result==1)
cout << "Array 1 is greater than array 2.\n";
else
cout << "Array 2 is greater than array 1.\n";
return 0;
}
/*Function Definition*/
int compareArrays(int arr1[], int size1, int arr2[], int size2)
{
if( size1>size2 ) //testing if first array is greater than second array
return 1;
else if(size1<size2) //testing if second array is greater than first array
return -1;
else //will execute if sizes of both arrays are equal
{
for( int i=0 ; i<size1 ; i++ )
{
if(arr1[i]>arr2[i]) //if first array element is greater than that of second array then return 1
return 1;
else if(arr1[i]<arr2[i]) //if second array element is greater than that of first array then return -1
return -1;
}
return 0; //return 0 if elements of both arrays are equal
}
}
C++ Program for getting maximum number in an array
#include<iostream>
using namespace std;
/*Function Prototype*/
int getMaxIndex(int arr[], int size); //function for getting index of max number in the array passed
int main()
{
int values[50], //defining array of large size(50)
size,index;
cout << "Enter the array size: ";
cin >> size; //getting size of array
while(size<1||size>50) //checking if the size entered is in the range of 1 through 50
{
cout << "Wrong input(should be between 1to50)!"
<< "\nEnter again: ";
cin >> size;
}
for( int i=0 ; i<size ; i++ ) //for getting input in array
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> values[i]; //getting array individual elements
}
index=getMaxIndex(values,size); //calling function by passing array and its size
cout << "The largest elemnt is "
<< values[index]
<< " and its index is: "
<< index
<< ".\n";
return 0;
}
/*Function definition*/
int getMaxIndex(int arr[],int size)
{
int index=0, //initializing index with 0
large=arr[0]; //inititalizing large(for largest value) with first element
for( int i=0 ; i<size ; i++ ) //for checking eash element
{
if( arr[i]>large ) //testing each element if it is larger than 'large'
{
large=arr[i]; //assigning larger element to large
index=i; //assigning the index of larger element to 'index'
}
}
return index; //returning the index of largest element
}
using namespace std;
/*Function Prototype*/
int getMaxIndex(int arr[], int size); //function for getting index of max number in the array passed
int main()
{
int values[50], //defining array of large size(50)
size,index;
cout << "Enter the array size: ";
cin >> size; //getting size of array
while(size<1||size>50) //checking if the size entered is in the range of 1 through 50
{
cout << "Wrong input(should be between 1to50)!"
<< "\nEnter again: ";
cin >> size;
}
for( int i=0 ; i<size ; i++ ) //for getting input in array
{
cout << "Enter element "
<< (i+1)
<< ": ";
cin >> values[i]; //getting array individual elements
}
index=getMaxIndex(values,size); //calling function by passing array and its size
cout << "The largest elemnt is "
<< values[index]
<< " and its index is: "
<< index
<< ".\n";
return 0;
}
/*Function definition*/
int getMaxIndex(int arr[],int size)
{
int index=0, //initializing index with 0
large=arr[0]; //inititalizing large(for largest value) with first element
for( int i=0 ; i<size ; i++ ) //for checking eash element
{
if( arr[i]>large ) //testing each element if it is larger than 'large'
{
large=arr[i]; //assigning larger element to large
index=i; //assigning the index of larger element to 'index'
}
}
return index; //returning the index of largest element
}
Thursday, January 22, 2015
C++ Program for getting number in a given range
#include<iostream>
#include<cstdlib> //for rand function
#include<ctime> //for time function
using namespace std;
int generateRandomNumber(int,int); //function prototype
int main()
{
int lower, //declaration of variables
upper,
randomNum;
cout << "Enter lower bound for random number: ";
cin >> lower;
cout << "Enter upper bound for random number: ";
cin >> upper;
randomNum=generateRandomNumber(lower,upper); //calling function by passing arguments
cout << "Random number between " //displaying output
<< lower
<< " and "
<< upper
<< " is: "
<< randomNum
<< ".\n";
return 0;
}
//function definition
int generateRandomNumber(int lowerB,int upperB)
{
int randomNumber; //defining variable for random number
unsigned int seed=time(0); //calling time function by passing 0 as argument
srand(seed); //calling srand function
randomNumber=lowerB+rand()%(upperB-lowerB+1); //calling rand function and defining the range for random number
return randomNumber; //returning random number value
}
#include<cstdlib> //for rand function
#include<ctime> //for time function
using namespace std;
int generateRandomNumber(int,int); //function prototype
int main()
{
int lower, //declaration of variables
upper,
randomNum;
cout << "Enter lower bound for random number: ";
cin >> lower;
cout << "Enter upper bound for random number: ";
cin >> upper;
randomNum=generateRandomNumber(lower,upper); //calling function by passing arguments
cout << "Random number between " //displaying output
<< lower
<< " and "
<< upper
<< " is: "
<< randomNum
<< ".\n";
return 0;
}
//function definition
int generateRandomNumber(int lowerB,int upperB)
{
int randomNumber; //defining variable for random number
unsigned int seed=time(0); //calling time function by passing 0 as argument
srand(seed); //calling srand function
randomNumber=lowerB+rand()%(upperB-lowerB+1); //calling rand function and defining the range for random number
return randomNumber; //returning random number value
}
C++ Program for counting 'ones' in the binary of a given decimal number
#include<iostream>
using namespace std;
/*Function Prototypes*/
int find_binary(int); //for finding binary of decimal number
int countOnes(int,int &); //for counting ones in the decimal number
void show_result(int,int,int); //for showing results
int main()
{
int num,binary,ones;
cout << "Enter a Decimal number: ";
cin >> num;
ones=countOnes(num,binary); //calling function for getting the number of ones ones
show_result(num,binary,ones); //calling function to show the output
return 0;
}
/*Definitions of functions*/
int find_binary(int num )
{
int calD,calB, //for calculation
binary; //to store binary number
calD=num;
calB=1;
while( calD!=0 )
{
calB=calB*10+calD%2; //getting the reverse of binary number
calD/=2;
}
binary=0;
while( calB>=10 )
{
binary=binary*10+calB%10; //reversing the number to get the original binary number binary number
calB/=10;
}
return binary;
}
int countOnes(int num,int &binary)
{
int ones=0,
cal; //for calculation
if( num<0 )
{
binary=find_binary(abs(num)); //calling the function inside another function by passing absolute value
binary=-binary; //ataching -ve sign to get the original number
}
else
binary=find_binary(num); //calling the function inside another function
cal=abs(binary); //storing absolute value
while(cal!=0)
{
/*The number of ones are counted if the remainder is 1*/
if(cal%10==1)
{
ones++; //counted the number of 1s
}
cal/=10;
}
return ones;
}
void show_result(int num,int binary,int ones)
{
/*Showing the output*/
cout << "Binary form of "
<< num
<< " is: "
<< binary
<< ".\n";
cout << "And the number of ones in "
<< binary
<< " are: "
<< ones
<< endl;
}
using namespace std;
/*Function Prototypes*/
int find_binary(int); //for finding binary of decimal number
int countOnes(int,int &); //for counting ones in the decimal number
void show_result(int,int,int); //for showing results
int main()
{
int num,binary,ones;
cout << "Enter a Decimal number: ";
cin >> num;
ones=countOnes(num,binary); //calling function for getting the number of ones ones
show_result(num,binary,ones); //calling function to show the output
return 0;
}
/*Definitions of functions*/
int find_binary(int num )
{
int calD,calB, //for calculation
binary; //to store binary number
calD=num;
calB=1;
while( calD!=0 )
{
calB=calB*10+calD%2; //getting the reverse of binary number
calD/=2;
}
binary=0;
while( calB>=10 )
{
binary=binary*10+calB%10; //reversing the number to get the original binary number binary number
calB/=10;
}
return binary;
}
int countOnes(int num,int &binary)
{
int ones=0,
cal; //for calculation
if( num<0 )
{
binary=find_binary(abs(num)); //calling the function inside another function by passing absolute value
binary=-binary; //ataching -ve sign to get the original number
}
else
binary=find_binary(num); //calling the function inside another function
cal=abs(binary); //storing absolute value
while(cal!=0)
{
/*The number of ones are counted if the remainder is 1*/
if(cal%10==1)
{
ones++; //counted the number of 1s
}
cal/=10;
}
return ones;
}
void show_result(int num,int binary,int ones)
{
/*Showing the output*/
cout << "Binary form of "
<< num
<< " is: "
<< binary
<< ".\n";
cout << "And the number of ones in "
<< binary
<< " are: "
<< ones
<< endl;
}
C++ Program getting sum of digits of a given number
#include<iostream>
#include<cmath> //for abs function
using namespace std;
int sum_of_digits(int); //function prototype
int main()
{
int num,sum; //defining variables
cout << "Enter any number: ";
cin >> num; //getting number from user
sum=sum_of_digits(num); //calling function
sum = abs(sum); //getting absolute value of sum
cout << "Sum of digits of number " //producing output
<< num
<< " is: "
<< sum
<< endl;
return 0;
}
int sum_of_digits(int number) //function definition
{
int sum=0;
while( number!=0 )
{
sum+=number%10; //getting sum of digits
number/=10;
}
return sum; //returning sum of digits
}
#include<cmath> //for abs function
using namespace std;
int sum_of_digits(int); //function prototype
int main()
{
int num,sum; //defining variables
cout << "Enter any number: ";
cin >> num; //getting number from user
sum=sum_of_digits(num); //calling function
sum = abs(sum); //getting absolute value of sum
cout << "Sum of digits of number " //producing output
<< num
<< " is: "
<< sum
<< endl;
return 0;
}
int sum_of_digits(int number) //function definition
{
int sum=0;
while( number!=0 )
{
sum+=number%10; //getting sum of digits
number/=10;
}
return sum; //returning sum of digits
}
Thursday, January 15, 2015
C++ Program for showing a rectangular pattern
#include<iostream> //for input/output
using namespace std;
int main()
{
//decalaring variables
int height, //for height of the output pattern
width; //for width of the output pattern
cout << "The value of height and width should be an odd number and greater than two." //prompting the user
<< endl;
cout << "Enter the height of the output to be printed: "; //prompting the user for height
cin >> height;
while( height < 3 || height%2==0 ) //loop for validating the input for height
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> height;
}
cout << "Enter the width of the output to be printed: "; //prompting the user for width
cin >> width;
while( width < 5 || width%2==0 ) //loop for validating the input for width
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> width;
}
int i=1, //declaring and intializing loop control variable
j;
cout << "The desired output pattern is...\n"; //prompting the user to show output
while( i <= height )
{
j=1; //initializing nested loop control variable
//applying conditions for printing the desired formate
if( i%2 != 0 ) //validating the height as odd
{
while( j<=width )
{
cout << "+"; //print a '+' sign
j++;
}
}
else
{
while( j<=(width/2+1) )
{
cout << "+ "; //printing '+' with a space
j++;
}
}
cout << endl;
i++;
}
return 0;
}
using namespace std;
int main()
{
//decalaring variables
int height, //for height of the output pattern
width; //for width of the output pattern
cout << "The value of height and width should be an odd number and greater than two." //prompting the user
<< endl;
cout << "Enter the height of the output to be printed: "; //prompting the user for height
cin >> height;
while( height < 3 || height%2==0 ) //loop for validating the input for height
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> height;
}
cout << "Enter the width of the output to be printed: "; //prompting the user for width
cin >> width;
while( width < 5 || width%2==0 ) //loop for validating the input for width
{
cout << "Invalid input!"
<< "Enter again: ";
cin >> width;
}
int i=1, //declaring and intializing loop control variable
j;
cout << "The desired output pattern is...\n"; //prompting the user to show output
while( i <= height )
{
j=1; //initializing nested loop control variable
//applying conditions for printing the desired formate
if( i%2 != 0 ) //validating the height as odd
{
while( j<=width )
{
cout << "+"; //print a '+' sign
j++;
}
}
else
{
while( j<=(width/2+1) )
{
cout << "+ "; //printing '+' with a space
j++;
}
}
cout << endl;
i++;
}
return 0;
}
C++ Program for showing Multiplication table of size N
#include<iostream> //for input/output
#include<iomanip> //for input/output formate
using namespace std;
int main()
{
//declaring variables
int i,j, //i and j are loop control variables
N; //N is for taking input from user
//prompting and taking input from the user
cout << "Enter any Positive Integer number to get the Table: ";
cin >> N;
if( N>=1 )
{
i=1; //initializing loop variable
cout << "The desired output table is..\n"; //prompting the user for showing output
cout << " "; //for formating output six spaces added
while( i<=N )
{
cout << setw(4) //printing numbers from 1 to the entered number
<< i;
i++;
}
cout << endl
<< setw(6) //+ symbol printed after five spaces
<< "+";
i=1; //loop variable agin initialized to 1
while( i<=N )
{
cout << "----"; //for formating Line ouput...here 4 dots are printed N times to take print in th desired formate
i++;
}
cout << endl;
i=1;
while( i<=N )
{
j=1; //intilizing nested loop control variable
cout << setw(4) //for formating output
<< i
<< " |";
while ( j<=N) //nested loop
{
cout << setw(4) //for formating output
<< i*j; //for printing the product
j++;
}
cout << endl;
i++;
}
}
else
cout << "Error: Wrong Input!" << endl;
return 0;
}
#include<iomanip> //for input/output formate
using namespace std;
int main()
{
//declaring variables
int i,j, //i and j are loop control variables
N; //N is for taking input from user
//prompting and taking input from the user
cout << "Enter any Positive Integer number to get the Table: ";
cin >> N;
if( N>=1 )
{
i=1; //initializing loop variable
cout << "The desired output table is..\n"; //prompting the user for showing output
cout << " "; //for formating output six spaces added
while( i<=N )
{
cout << setw(4) //printing numbers from 1 to the entered number
<< i;
i++;
}
cout << endl
<< setw(6) //+ symbol printed after five spaces
<< "+";
i=1; //loop variable agin initialized to 1
while( i<=N )
{
cout << "----"; //for formating Line ouput...here 4 dots are printed N times to take print in th desired formate
i++;
}
cout << endl;
i=1;
while( i<=N )
{
j=1; //intilizing nested loop control variable
cout << setw(4) //for formating output
<< i
<< " |";
while ( j<=N) //nested loop
{
cout << setw(4) //for formating output
<< i*j; //for printing the product
j++;
}
cout << endl;
i++;
}
}
else
cout << "Error: Wrong Input!" << endl;
return 0;
}
C++ Program for showing pythagorean triples
#include<iostream> //for Input/Output
#include<iomanip> //for Input/Output formate
using namespace std;
int main()
{
int PythaTriples=0; //variable for calculating the total number of Pythgorean triples in first 200 numbers
//appying nested for loops
for( int h=1 ; h<=200 ; h++) //using 'h' variable for hypotenuse
{
for( int b=1 ; b<=200 ; b++) //using 'b' variable for base
{
for( int p=1 ; p<=200 ; p++) //using 'p' variable for perpendicular
{
if( (h*h) == (b*b) + (p*p) ) //applying condition for pythagorean triple
{
PythaTriples+=1;
cout << "Sides " //printing the result in a proper formate
<< setw(3)
<< h
<< " , "
<< setw(3)
<< b
<< " and "
<< setw(3)
<< p
<< " form the Pythagorean Triple"
<< endl;
}
}
}
}
cout << "\nTotal Pythagorean Triples in first 200 numbers are : " //printing total number of pythagorean triples
<< PythaTriples
<< endl;
return 0;
}
#include<iomanip> //for Input/Output formate
using namespace std;
int main()
{
int PythaTriples=0; //variable for calculating the total number of Pythgorean triples in first 200 numbers
//appying nested for loops
for( int h=1 ; h<=200 ; h++) //using 'h' variable for hypotenuse
{
for( int b=1 ; b<=200 ; b++) //using 'b' variable for base
{
for( int p=1 ; p<=200 ; p++) //using 'p' variable for perpendicular
{
if( (h*h) == (b*b) + (p*p) ) //applying condition for pythagorean triple
{
PythaTriples+=1;
cout << "Sides " //printing the result in a proper formate
<< setw(3)
<< h
<< " , "
<< setw(3)
<< b
<< " and "
<< setw(3)
<< p
<< " form the Pythagorean Triple"
<< endl;
}
}
}
}
cout << "\nTotal Pythagorean Triples in first 200 numbers are : " //printing total number of pythagorean triples
<< PythaTriples
<< endl;
return 0;
}
C++ Program for finding your age title
#include<iostream> //for input/output
#include<cmath> //for abs funtion
using namespace std;
int main()
{
int AgeYears; //declaring variable for age
cout << "Enter the age in years: "; //promting the user to enter age
cin >> AgeYears;
AgeYears = abs(AgeYears); //getting absolute variable
//appling different conditions and poducing different outputs
if( AgeYears <= 3 ) //for baby
cout << "\nYou are a Baby\n";
else if( AgeYears <= 7 ) //for toodler
cout << "\nYou are a Toodler\n";
else if( AgeYears <= 12 ) //for child
cout << "\nYou are a Child\n";
else if( AgeYears <= 19 ) //for teenager
cout << "\nYou are a Teenager\n";
else //for adult or old ager
cout << "\nYou are a Adult or and Old ager\n";
return 0;
}
Tuesday, January 13, 2015
C++ Program for Linear and Binary Search
#include<iostream>
#include<iomanip>
using namespace std;
bool linear_search_flag(double [],int);
void result(bool);
int linear_search(double [],int);
void Result(int);
void more_positions(double [],int );
void binary_search_flag(double [],int );
void binary_search(double [],int );
int main()
{
const int SIZE = 18;
bool flag;
double Numbers[SIZE]={5658845, 8080152, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
flag=linear_search_flag(Numbers,SIZE);
result(flag);
int position;
position=linear_search(Numbers,SIZE);
Result(position);
more_positions(Numbers,SIZE );
binary_search_flag(Numbers,SIZE);
binary_search(Numbers,SIZE);
return 0;
}
bool linear_search_flag(double Array[],int size )
{
double Num;
bool flag=0;
cout << "Enter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
flag=1;
break;
}
}
return flag;
}
void result(bool flag)
{
if( flag )
cout << "\nThe number exists in the list.\n";
else
cout << "\nThe number does NOT exist int the list.\n";
}
int linear_search(double Array[],int size )
{
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ )
{
if( Num==Array[i] )
{
position=i;
break;
}
}
return position;
}
void Result(int position)
{
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number exists at position "
<< (position+1)
<< ".\n"
<< "And its subscript number in the array is "
<< position
<< endl;
}
void more_positions(double Array[],int size )
{
double Num;
bool position[18]={0};
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
position[i]=1;
}
}
bool exist=0;
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
exist=1;
break;
}
}
if(exist)
{
cout << setprecision(2) << fixed;
cout << "\nThe number "
<< Num
<< " exists at position(s)";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << (i+1)
<< " ";
}
}
cout << ".\n";
cout << "And its subscript number is ";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << i
<< " ";
}
}
cout << ".\n";
}
else
cout << "\nThe number "
<< Num
<< " does NOT exist.\n";
}
void binary_search_flag(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
bool flg=0;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Ascending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]<Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";
first=0;
last=SIZE-1;
while( first<=last && !flg )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
flg=1;
else if( Num>Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( flg )
cout << "\nThe number exists.\n";
else
cout << "\nThe number does NOT exist.\n";
}
void binary_search(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Descending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]>Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";
first=0;
last=SIZE-1;
while( first<=last && position==-1 )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
position=mid;
else if( Num<Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number Exists at position "
<< (position+1)
<< ".\n"
<< "And at its subscript value in the Array is "
<< position
<< ".\n";
}
#include<iomanip>
using namespace std;
bool linear_search_flag(double [],int);
void result(bool);
int linear_search(double [],int);
void Result(int);
void more_positions(double [],int );
void binary_search_flag(double [],int );
void binary_search(double [],int );
int main()
{
const int SIZE = 18;
bool flag;
double Numbers[SIZE]={5658845, 8080152, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
flag=linear_search_flag(Numbers,SIZE);
result(flag);
int position;
position=linear_search(Numbers,SIZE);
Result(position);
more_positions(Numbers,SIZE );
binary_search_flag(Numbers,SIZE);
binary_search(Numbers,SIZE);
return 0;
}
bool linear_search_flag(double Array[],int size )
{
double Num;
bool flag=0;
cout << "Enter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
flag=1;
break;
}
}
return flag;
}
void result(bool flag)
{
if( flag )
cout << "\nThe number exists in the list.\n";
else
cout << "\nThe number does NOT exist int the list.\n";
}
int linear_search(double Array[],int size )
{
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ )
{
if( Num==Array[i] )
{
position=i;
break;
}
}
return position;
}
void Result(int position)
{
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number exists at position "
<< (position+1)
<< ".\n"
<< "And its subscript number in the array is "
<< position
<< endl;
}
void more_positions(double Array[],int size )
{
double Num;
bool position[18]={0};
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
position[i]=1;
}
}
bool exist=0;
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
exist=1;
break;
}
}
if(exist)
{
cout << setprecision(2) << fixed;
cout << "\nThe number "
<< Num
<< " exists at position(s)";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << (i+1)
<< " ";
}
}
cout << ".\n";
cout << "And its subscript number is ";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << i
<< " ";
}
}
cout << ".\n";
}
else
cout << "\nThe number "
<< Num
<< " does NOT exist.\n";
}
void binary_search_flag(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
bool flg=0;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Ascending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]<Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";
first=0;
last=SIZE-1;
while( first<=last && !flg )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
flg=1;
else if( Num>Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( flg )
cout << "\nThe number exists.\n";
else
cout << "\nThe number does NOT exist.\n";
}
void binary_search(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Descending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]>Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";
first=0;
last=SIZE-1;
while( first<=last && position==-1 )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
position=mid;
else if( Num<Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number Exists at position "
<< (position+1)
<< ".\n"
<< "And at its subscript value in the Array is "
<< position
<< ".\n";
}
Subscribe to:
Comments (Atom)