Tuesday, 16 September 2014

Compare and merge two array without using sorting procedure


/*Compare and merge  two array without using sorting procedure*/

Solution:

#include<iostream>
using namespace std;
const int size1 = 4,size2 = 4;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

 int arr1[size1] = {2,3,8,9};
 int arr2[size2] = {1,5,7,10};
 const int max = size1+size2;
 int arr3[max],i = 0,l = 0,k = 0;

 for(int m = 0; m < max-1;m++){
 
  if(arr1[l] > arr2[i]){
  
   arr3[k++] = arr2[i++];
  }
  else if(arr1[l] == arr2[i]){
  
   arr3[k++] = arr1[l++];
  }
  else{
  
   arr3[k++] = arr1[l++];
  }
 }
 if(l == size1-1){
 
  arr3[k++] = arr1[l];
 }
 else if(i == size2-1){
 
  arr3[k++] = arr2[i];
 }
 for(i = 0; i < max; i++){
 
  cout << arr3[i] << ends;
 }
 cout << endl;
}

Thursday, 11 September 2014

Check that given number is Perfect Square Or Not

/*Check that given number is Perfect Square Or Not*/

Solution:

#include<stdio.h>
#include<conio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main (){
 float i,j = 0,num;
 printf("Enter a number:");
 scanf("%f",&num);

 for (i = 2; i <= num/2; i++){
 
  j = num/i;
  if (j == i){
  
   printf("\nPerfect Square.\n\a");
   break;
  }
  else{
  
   printf("\nNot perfect Square.\n");
   break;
  }
 }
 getch();
 return 0;
}

Check Palindrome String or Not

/*Check Palindrome String or Not*/

Solution:

#include<stdio.h>
#define SIZE 100
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main (){

 int range,i,j = 0,count = 0;
 char Palindrome[SIZE],temp[SIZE];
 printf("Enter word range : ");
 scanf("%d",&range);
 printf("Enter a word : ");
 for( i = 0; i < range;i++){
 
  scanf("%s",&Palindrome[i]);
 }
 for(i = range - 1; i >= 0;i--){
 
  temp[j++] = Palindrome[i];
 }
 for(i = 0; i < range; i++){
 
  if(Palindrome[i] == temp[i]){
  
   count++;
  }
 }
 if(count == range){
 
  printf("Palindrome String\n");
 }
 else{
 
  printf("Not a Palindrome string\n");
 }
 return 0;
}

Check that given numbers are Palindromen or not

/*Check that given numbers are Palindromen or not */


Solution:

#include<stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

 int i,num,Palindromen[100],temp[100],j=0,check=0;
  printf("Enter a number=");
  scanf("%d",&num);
  for(i=0;i < num;i++){
   scanf("%d",&Palindromen[i]);
  }
  for(i=num-1;i=0;i--){
   temp[j++]=Palindromen[i];
  }
  for(i=0;i < num;i++){
   if(Palindromen[i]==temp[i]){
    check++;
   }
  }
  if(check==num){
    printf("The number is Palindromen\n");
  }
  else{
    printf("The number is not Palindromen\n");
  }
  return 0;
}

Program to Find a input number using link list C++ Solution.

/*Program to Find a input number using link list*/

Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
struct node{

 int data;
 node *next;
};

int main(){

 int n,item,found = 0,num;
 node *list,*nptr,*tptr;
 cout << "Enter a number:" << endl;
 cin >> n;
 cout << "Enter value in node: " << endl;
 list = NULL;
 for(int i = 0; i < n; i++){
 
  cin >> item;
  nptr = new(node);
  nptr->data = item;
  nptr->next = NULL;
  if(list == NULL){
  
   list = nptr;
   tptr = nptr;
  }
  else{
  
   tptr->next = nptr;
   tptr = nptr;
  }
 }
 tptr = list;
 cout << "Enter the number You want 2 find: " << endl;
 cin >> num;
 int i;
 for(i = 0; i < n; i++){
 
  if(num == tptr->data){

   found = 1;
   break;
  }
  tptr = tptr->next;
 }
 if(found){
 
  cout << "Number found in position [" << i+1 << "]" << endl;
  cout << "Number is : " << tptr->data << endl;
 }
 else{
 
  cout << "Number is not found." << endl;
 }
 return 0;
}

Tuesday, 9 September 2014

Diagonal elements of a two-dimensional array

/*Diagonal elements of a two-dimensional array*/

Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main (){
 
 int arr[100][100],sum = 0,n;
 cout << "Enter a number: " << endl;
 cin >> n;
 for(int i = 0; i < n; i++){
 
  for(int j = 0; j < n; j++){
  
   cin >> arr[i][j];
  }
 }
 for(int i = 0; i < n; i++){
 
  for(int j = 0; j < n; j++){
  
   if(i == j || i+j == n-1){
   
    sum+=arr[i][j];
   }
  }
 }
 cout << "Sum of Diagonal is: " << sum << endl;
 return 0;
}

Summation of boundary elements of an array

/*Summation of boundary elements of an array*/

Solution:

#include<iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main (){
 
 int arr[100][100],sum = 0,row,col;
 cout << "Enter Row and Col: " << endl;
 cin >> row >> col;
 for(int i = 0; i < row; i++){
 
  for(int j = 0; j < col; j++){
  
   cin >> arr[i][j];
  }
 }
 for(int i = 0; i < row; i++){
 
  for(int j = 0; j < col; j++){
  
   if(i == 0 || j == 0 || i == row-1 || j == col-1){
   
    sum+=arr[i][j];
   }
  }
 }
 cout << "Sum of boundary is: " << sum << endl;
 return 0;
}