Pages

Saturday, June 29, 2013

Recursive solution of determining whether a string is plaindrome or not in C++

/*
QUESTION:
Write a recursive solution of determining whether a string is palindrome or not. Note: a palindrome string is that which can be read the same forward or backward e.g; racecar, mom etc
*/
// CODE:
#include <iostream>
using namespace std;

bool palindrome(char[], int size);
//MAIN STARTS
int main()
{
 cout << "Enter a stringing: ";

 char string[50];

 cin.getline(string,50,'\n');

 cout << "The entered stringing " << ( (palindrome(string,strlen(string)+1))? "is":"is not" ) << " a Palindrome string." << endl;
 return 0;
}

//MAIN ENDS
bool palindrome(char string[], int size)
{
 if (string[0] != string[size-2]) //check for first and last element
  return false;
 if (strlen(string) <= 1)
  return true;
 else
  return palindrome(string+1,size-2);
}

Recursively calling the main function

/*
QUESTION:
Can main function be called recursively on your system? Write a program that contains only the recursive case , and show the counter each time the main function is called recursively
*/

//CODE:

#include <iostream>
#include <conio.h>
using namespace std;


//MAIN STARTS
int main(int a){
  static int b=0;

  cout<<"FUNCTION IS CALLED"<<b<<"th time\n";
  b++;
 
  main(b);
 


 return 0;
}

//MAIN ENDS 

Recursive function to find greatest common divisor

/*
QUESTION:

Write a recursive function that will find the greatest common divisor of two inputs. Basically the greatest common divisor of two integers x and y is generally the largest number that evenly divides both numbers.

*/

CODE:

#include <iostream>
using namespace std;

int gcd(int x, int y);
//MAIN STARTS
int main(){
 int a,b;
cout<<"ENTER FIRST NUMBER : ";
cin>>a;

cout<<"\nENTER SECOND NUMBER : ";
cin>>b;

cout<<"\n\n THE GREATEST COMMON DIVISOR IS : "<<gcd(a,b)<<endl;

 return 0;
}

//MAIN ENDS
int gcd(int x, int y){
if (x==0)
return y;
if(y==0)
return x;
if (x == y)
return y;

else
return gcd(y,x%y);


}




 

RECURSIVE function to find minimum element of an array in C++

/*
QUESTION:
 write a recursive function that finds and returns the minimum element in an array, where the array and its size are given as parameters

*/

//CODE:

#include <iostream>
using namespace std;

int m_find(int,int);
//MAIN STARTS
int main(){
    int size;
 cout<<"ENTER THE SIZE OF ARRAY : ";

    cin>>size;

 cout<<"\n\nENTER THE VALUES IN THE ARRAY ";
    int * arr = new int[size];

 for(int i=0; i < size ; i++){
    cin>>arr[i];
 }
 
 cout<<"\nMinimum value is : "<<m_find(arr,size)<<endl;
 return 0;
}

//MAIN ENDS

int m_find(int *arr,int size)
{
 if (size == 0)
  return arr[0];
 if(arr[size-1] < m_find(arr , size -1))
  return arr[size -1];
 else
  return (m_find(arr , size -1));



}

Recursive power function in C++

/*
QUESTION:

WRITE a recursive function int Power(int a , int b) that computes the product of two integers a  and b. The only arithmetic operation that you are allowed is "+"(addition)
*/

// CODE:

#include <iostream>
using namespace std;

int Power(int a,int b);
//MAIN STARTS
int main(){
int a,b;
cout<<"ENTER the NUmber : ";
cin>>a;

cout<<"\nEnter the power of number : ";
cin>>b;

cout<<"\n\nREsult is :"<<Power(a,b)<<endl;

 return 0;
}


//MAIN ENDS
int Power(int a,int b){
if (b==0)
return 1;
if (a == 0)
return 0;

else
return ( a * Power(a,b-1) );

}

Recursive function for multiplication of two numbers using addition in C++

/* QUESTION :

Write a function int Mul(int a,int b) that computes the product of two integers a and b. THE ONLY ARITHMETIC OPERATION THAT YOU ARE ALLOWED TO USE IS ADDITION '+' USING RECURSION
*/


// CODE :
#include <iostream>
using namespace std;

int Mul(int a,int b);
//MAIN STARTS
int main(){
int a,b;
cout<<"Enter first number : ";
cin>>a;


cout<<"\nEnter second number : ";
cin>>b;

cout<<"\n\nTHE RESULT AFTER MULTIPLICATION IS : "<<Mul(a,b)<<endl;
 return 0;
}

//MAIN ENDS
int Mul(int a , int b){
if (b==0)
return 0;

if (a==0)
return 0;

else
return (a + Mul(a,b-1));
}

 

SUM OF NUMBERS USING RECURSION IN C++

/*QUESTION :
Write a recursive function that computes the sum of all numbers from 1 to n , where n is given as parameter by the user.
*/

//CODE:

#include <iostream>
using namespace std;
int sum(int x);

//MAIN STARTS
int main(){
 cout<<"ENTER A NUMBER : ";
 int a;
 cin>>a;
 cout<<"The required sum is : "<<sum(a)<<endl;


 return 0;
}


// MAIN ENDS
int sum(int x){
 if (x == 1)
  return 1;
 else
  return ( x + sum (x-1));
}

FINDING MEDIAN AND AVERAGE IN C++

/* QUESTION :

Write a program to store integer values to a table (length of table during program execution)

using double pointer. Pass the table as arguments to a function. Print Median and average

values.

Hint: Your will use new operator to allocate memory.

Note: You have to release the memory using delete operator before the end of program. */



// CODE :


 #include<iostream>
using namespace std;

void find_median_average(double **ptr,int a,int b);

//MAIN STARTS
int main(){
 int a,b;
 cout<<"Enter the no of rows";
 cin>>a;
 double **ptr = new double *[a];

 cout<<"Enter the no of coloumns";
 cin>>b;
 for (int i=0;i<a;i++){
  ptr[i] = new double[b];
 }
 cout<<"\n\nEnter the values\n";
 for (int j=0;j<a;j++){
  for(int k=0;k<b;k++){
   cin>>ptr[j][k];
  }
 }

 find_median_average(ptr,a,b);


 for(int p=0; p< a;p++)
  delete [] ptr[p];
 delete [] ptr;
        ptr = NULL;
 return 0;
}


//END MAIN

void find_median_average(double **ptr,int a,int b){
 double average = 0;

 for (int l=0;l<a;l++){
  for(int m=0;m<b;m++){
   average += ptr[l][m];

  }
 }
 average /= (a*b);
 cout << "Average: " << average << endl;
 double median = 0;

 int positionTerm1=(a*b)/2;
 int positionTerm2=(a*b)/2+1;
 

 int count1 = 1;//How many times the loop has Run.
 int count2 = 1;
 int js = 0;
 bool breakFromInnerMostLoop = false;
 for (int i = 0; i<a; i++){
  for (int j = 0; j<b; j++){
   js = j;
   if (count1==positionTerm1){
    breakFromInnerMostLoop = true;
    break;
   }
   count1++;
  }
  if (breakFromInnerMostLoop){
    break;
  }
 }
 breakFromInnerMostLoop = false;//reset

 cout << "i: " << i << endl;
 cout << "js: " << js << endl;


 int ys = 0;

 for (int x = 0; x<a; x++){
  for (int y=0; y<b; y++){
   ys = y;
   if (count2==positionTerm2){
    breakFromInnerMostLoop = true;
    break;
   }
   count2++;
  }
  if (breakFromInnerMostLoop){
    break;
  }
 }


 cout << "positionTerm1: " << positionTerm1 << "\npositionTerm2:" << positionTerm2 << endl;
 cout << "x: " << x << endl;
 cout << "ys: " << ys << endl;
 if ((a*b)%2==0)
  cout << "Median: " << (ptr[i][js] + ptr[x][ys])/2 << endl;
 else
  cout << "Median: " <<   (ptr[x][ys]) << endl;
}

 

Double pointer 2d array c++

/* QUESTION:
Double pointers use write a program to store alphabets to a table (length of table during program execution) using

double pointer. Pass the table as arguments to a function. Print the vowels to the screen.

Hint: Your will use new operator to allocate memory.

Note: You have to release the memory using delete operator before the end of program.
*/

 // CODE :

 #include<iostream>
using namespace std;

void find_vowels(char **ptr,int a,int b);
int main(){
int a,b;
cout<<"Enter the no of rows your table for storing alphabets ";
cin>>a;
 char **ptr = new char *[a];

 cout<<"Enter the no of coloumns";
cin>>b;
 for (int i=0;i<a;i++){
  ptr[i] = new char[b];
 }
 cout<<"\n\nEnter the characters\n";
 for (int j=0;j<a;j++){
  for(int k=0;k<b;k++){
      cin>>ptr[j][k];
  }
 }
 cout<<"\n The vowels out of table are\n\n";
 find_vowels(ptr,a,b);
for(int p=0; p< a;p++)
delete [] ptr[p];
delete [] ptr;
 return 0;
}
void find_vowels(char **ptr,int a,int b){
 for (int l=0;l<a;l++){
  for(int m=0;m<b;m++){

   if (ptr[l][m] == 'a'||ptr[l][m] =='e'||ptr[l][m] =='i'||ptr[l][m] =='o'|| ptr[l][m] =='u'){
    cout<<ptr[l][m]<<endl;

   }


  }


 }
}

Friday, June 28, 2013

C++ new operator example

//QUESTION :

/*
Write a program to allocate memory locations for four integer values using ‘new‘operator.

Stores the addresses of allocated memory locations into array of pointers. Find out the

minimum values from four values.
Hint: You will use integer pointer to create integers and array of pointers to store addresses of

integers. You are required to use array of pointers to select minimum value integer.
 
Note: You have to release the memory using delete operator before the end of program.

*/


// CODE :

#include <iostream>
using namespace std;
int main(){

 int *ptr = new int [4];
 cout<<"Enter the values in array\n\n\n\n";
 for (int j = 0; j<4; j++){
  cin >> ptr[j];
 }
 int **arrayOfPtr = new int* [4];
 for (int i = 0; i<4; i++){
  arrayOfPtr[i] = (ptr+i);
 }
 int min = **arrayOfPtr;
 for (int k = 0; k<4; k++){
  if ( min>**(arrayOfPtr+k) ){
   min = **(arrayOfPtr+k);
  }
 }
 cout << "Minimum: "<< min << endl;
 for (i=0; i<4; i++)
  delete arrayOfPtr[i];
 delete [] arrayOfPtr;
 arrayOfPtr = NULL;
 return 0;
}

Thursday, June 27, 2013

C++ CODE FOR DYNAMIC MEMORY ALLOCATION (DMA)

QUESTION:
Write a program to allocate memory locations for an array of ‘int’ type using ‘new‘ operator.

The program should input values for the length of array during program execution. Enter values

into array and find maximum values of an array.

Note: You have to release the memory using delete operator before the end of program.
 
 


CODE:

#include <iostream>
using namespace std;
int main(){
int size;
cout<<"Enter the size of the array\n";
cin>>size;
int *ptr = new int[size];
cout<<"\nEnter the values in the array\n\n";

for(int i=0;i<size;i++){
cin>>ptr[i];
}
int max = ptr[0];
for(i=0;i<size;i++){
 if(ptr[i] > max){
    max = ptr[i];
 }

}
cout<<"Maximum value of array is : "<<max<<endl<<endl;
delete [] ptr;
ptr = NULL;

 return 0;
}

 

C++ SORTING IN STRUCTURES

QUESTION :
Sorting Employees


Make a structure named as Employee, containing two member variables name of type

char[] and salary of type float. Make an array dynamically querying the size from the

user to store the Employee. Input each Employee data from user and store it in this array.

Sort the Array with respect to the salary of the Employee. Finally display the output in

ascending order as follows:

Ali‟s Salary is: xxxxxx

Abc*s Salay is: xxxxxx

Def‟s Salary is :xxxxxx

CODE :

 ////////////////////////////////////////////////////
#include<iostream>
using namespace std;
struct Employee{

 char name[30];
 float salary;



};
int main(){
 int size;

 cout <<"\nEnter the size\n";
 cin>>size;
 Employee *ptr = new Employee [size];

 for (int i=0; i<size ; i++){
  cout<<"\nEnter the name:\n";
  cin>>ptr[i].name;//
 
  cout<<"\nEnter the salary\n";
  cin>>ptr[i].salary;//
 }

 for(int j=0;j<size-1;j++){
  for(int k=0;k<size-j-1;k++){//
   if (ptr[k].salary > ptr[k+1].salary){
    Employee temp;//
    temp = ptr[k];
    ptr[k] = ptr[k+1];
    ptr[k+1] = temp;
   }
  
  }
 
 
 }

 for (int x=0;x<size;x++){
  cout<<ptr[x].name<<"'s salary is : "<<ptr[x].salary<<endl;
 }

 return 0;
}
/////////////////////////////////////////////////// 

C++ Structures Time Addition code

QUESTION :
Time Addition
Use a time structure having three members hours, min and seconds of type int. Write a

program that obtain two time values form user in format 12:59:59, store them in struct

time. Convert each time to seconds and add theses quantities, and convert the result back

to hours, min and seconds and final display the result in 12:59:59 format.

CODE:
/////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
struct time{
int hours;
int mins;
int seconds;
};
int main(){
long int sum,sum1,sum2;
time s1,s2;
long int h,m,s;
cout<<"\nEnter Hours\n";
cin>>s1.hours;
cout<<"\nEnter Mins\n";
cin>>s1.mins;
cout<<"\nEnter seconds\n";
cin>>s1.seconds;
sum = (s1.hours*3600)+(s1.mins*60)+(s1.seconds);
cout<<"\nEnter Hours\n";
cin>>s2.hours;
cout<<"\nEnter Mins\n";
cin>>s2.mins;
cout<<"\nEnter seconds\n";
cin>>s2.seconds;
sum += (s2.hours*3600)+(s2.mins*60)+(s2.seconds);
h = sum/3600;
sum1 = sum%3600;
m = sum1/60;
sum2 = sum1%60;
s =sum2;
cout<<"Time is"<<h<<":"<<m<<":"<<s<<endl;
 return 0;
}




/////////////////////////////////////////////////////////////////////////////////////////

INTRODUCTION TO STRUCTURES IN C++

A structure is a collection of different variable types grouped together.
You can refer to a structure as a single variable and to its parts as members of that 
variable by using the dot (.) operator.
For Example:
// struct1.cpp
struct PERSON { // Declare PERSON struct type
int age; // Declare member types
float weight;
char name[25];
}; 
int main() {
PERSON sister;
PERSON brother; // C++ style structure declaration
sister.age = 13; // assign values to members
brother.age = 7;
return 0;
}
Difference between Array and structures:
Although arrays greatly improved our ability to store data, there is one major drawback to 
their use ... each element (each box) in an array must be of the same data type.  It is 
often desirable to group data of different types and work with that grouped data as one 
entity.  We now have the power to accomplish this grouping with a new data type called 
a structure.
Struct initialization:
You can initialize a structure by:
1.  PERSON boy={20,65,”Ali”};   //age 20, weight 65, name Ali
// initializes are used when contiguous members may be given
2.  PERSON boy;
boy.age=20; boy.weight=65;

Wednesday, June 26, 2013

Structure c++ implementation code

Phone Number Example


A phone number can be thought of three parts: the area code (051), the exchange (767),
and the number (8977). Write a program that uses a structure to store these three parts of
phone number separately. You have to ask user how many phone numbers he/she wants
to store. Save these phone numbers dynamically.
Then Program should ask user to enter the area code.
Output should be the phone numbers that matches with the given area code in the
following format.

(area code) exchange-number
(051) 767-8977


CODE:
#include <iostream>
using namespace std;

struct p_no{
   long int area_c;
   long int exchange;
   long int no;

};


int main(){
cout<<"How many phone number's you want to enter : ";
int ph_no;
cin>>ph_no;


p_no *ptr = new p_no[ph_no];

for (int i=0; i<ph_no ; i++){
cout<<"\nENTER AREA CODE\n";
cin>>ptr[i].area_c;

cout<<"\nENTER EXCHANGE NUMBER\n";
cin>>ptr[i].exchange;

cout<<"\nENTER THE NUMBER\n";
    cin>>ptr[i].no;
}


for (int j=0; j<ph_no ; j++){
cout<<"\nEnter the area code\n";
long int area_code;
cin>>area_code;
if (area_code == ptr[j].area_c)
cout<<"\n\n("<<ptr[j].area_c<<")"<<ptr[j].exchange<<"-"<<ptr[j].no<<endl<<endl;



}

return 0;
}