Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Saturday, 3 March 2012

Memory Allocation in cpp with 2 Dimensional Arrays

Operation on matrices with pointers/Pointerlar ve Diziler Üzerinde hafıza işlemleri

/***********************************************************
Memory Allocation and Matrices Operations with pointers
************************************************************/
#include <iostream>
#include <ctimegt>
#include <cstdlibgt>
using namespace std;
/**********************************************************************************
*FONKSİYONUN ADI: multip_matrix
*ALDIĞI PARAMETRELER: int** ve int
*DÖNÜŞ DEĞERİ: void
*AMACI: İki boyutlu 2 diziyi matris ve işaretçi aritmetiğine göre çarpan fonksiyon
**********************************************************************************/
void multip_matrix(int **a, int **b, int **c, int k, int m, int n){

 int i,j,l;
 int sum=0;
 for(l=0;l<n;l++)
  for(i=0;i<k;i++){
   for(j=0;j<m;j++){
    sum+=*(*(b+j)+l)**(*(a+i)+j);
   }
   *(*(c+i)+l) = sum;
   sum = 0;
  }
}
/**********************************************************************************
*FONKSİYONUN ADI: multip_array
*ALDIĞI PARAMETRELER: int* ve int
*DÖNÜŞ DEĞERİ: void
*AMACI: Tek boyutlu 2 diziyi matris ve işaretçi aritmetiğine göre çarpan fonksiyon
**********************************************************************************/
void multip_array(int *a, int *b, int *c, int k, int m, int n){
 int i,j,l;
 int sum = 0;
 for(l=0;l<n;l++)
  for(i=0;i<k;i++){
   for(j=0;j<m;j++)
   sum += *(a+j+i*k)**(b+j*n+l);
   *(c+i*n+l) = sum;
   sum = 0;
  }
}
int main(){
srand(time(NULL));
 int k,m,n;
 int x = 0;
 int i,j,z,l;
 int sayac = 0;
 cout <<  "Matrislerin Boyutlarini Girin (k,m,n seklinde)" <<endl;
 cin >> k >> m >> n;
 
 //a dizisi için hafızadan yer alınıyor ve dizi oluşturuluyor
 int **a;
 a = new int*[k];
 for(i=0;i<k;i++)
  a[i] = new int[m];
 for(j=0;j<k;j++)
  for(z=0;z<m;z++)
   a[j][z] = rand()%6;
 //a dizisinin elemanlar ekrana bastırılıyor
 cout << "1.matris"<<endl;
 for(i=0;i<k;i++)
  for(j=0;j<m;j++){
   cout << a[i][j]<<" ";
   sayac++;
   if(sayac == m){
    cout<<endl;
    sayac = 0; 
   }
  }
 cout << endl;

 //b dizisi için hafızadan yer alınıyor ve dizi oluşturuluyor
 int **b ;
 b = new int*[m];
 for(i=0;i<m;i++)
  b[i] = new int[n];
 for(j=0;j<m;j++)
  for(z=0;z<n;z++)
   b[j][z] = rand()%6;
 //b dizisinin elemanlar ekrana bastırılıyor
 cout << "2.matris"<<endl;
 for(i=0;i<m;i++)
  for(j=0;j<n;j++){
   cout << b[i][j]<<" ";
   sayac++;
   if(sayac == n){
    cout<<endl;
    sayac = 0; 
   }
  }
  
 cout << endl;

 //c dizisi için hafızadan yer alınıyor ve dizi oluşturuluyor
 int **c = new int*[k];
 for(i=0;i<k;i++)
  c[i] = new int[n];
 
 system("pause");
 //multip_matrix fonksiyon çağrısı
 multip_matrix(a,b,c,k,m,n);

 //multip matrix fonksiyonu ile oluşturulan iki boyutlu çarpım dizisinin ekrana bastırılması
 cout << "carpim matrisi"<<endl;
 for(i=0;i<k;i++)
  for(j=0;j<n;j++){
   printf("%2d ",c[i][j]);
   sayac++;
   if(sayac == n){
    cout<<endl;
    sayac = 0; 
   }
  }
 //tek boyutlu diziler icin hafızadan yer alınıyor
 int *dizi1 = new int[k*m];
 int *dizi2 = new int[m*n];
 int *dizi3 = new int[k*n];
  
 for(i=0;i<k;i++)
  for(j=0;j<m;j++){
   dizi1[x] = a[i][j];
   x++;
  }
  x=0;
 for(i=0;i<m;i++)
  for(j=0;j<n;j++){
   dizi2[x] = b[i][j];
   x++;
  }
 cout<<endl;
 system("pause");
 //multip_array fonksiyon çağrısı
 multip_array(dizi1,dizi2,dizi3,k,m,n);
 //multip array fonksiyonu ile oluşturulan tek boyutlu çarpım dizisinin ekrana bastırılması
 for(i=0;i<k*n;i++)
  cout<< *(dizi3+i) <<" ";
  cout<<endl;
  
 //Her bir dizi için bellekten alınan yerler geri veriliyor 
for(i=0;i<k*m;i++)
 delete  a[i];//Try This
 delete [] a;
for(i=0;i<m*n;i++)
 delete  b[i];//Try This
 delete [] b;
 delete [] dizi1;
 delete [] dizi2;
 delete [] dizi3;

 system("pause");
 
return 0;
}

Ekran çıktısı aşağıdaki gibidir

Monday, 16 January 2012

How to create binary tree

#include<iostream>
#include<stdio.h>
#include<time.h>
#include<iomanip>
#include<conio.h>
#define dimension 11
using namespace std;

struct tree{
 int value;
 tree *leftptr;
 tree *rightptr;
};
typedef tree Tree;
typedef Tree *treePtr;
void create(treePtr *,int);
void silme(treePtr *);
void remove(treePtr);
void inorder(treePtr);
void postorder(treePtr);
void preorder(treePtr);
void go_array(treePtr,int[],int *);
void main(){
 srand(time(NULL));
 int val,i,key;  //val means value, i means counter , key is keyboard input
 int counter=0;
 int arrayx[dimension]={0};
 treePtr root = NULL;
 for(i=0;i<11;i++){

  val = rand()%999+1;
  create(&root,val);
 }

 go_array(root,arrayx,&counter);
 cout << "inorder scanning..." << endl;
 inorder(root);
 cout <<endl<<"Preorder scanning..."<<endl;
 preorder(root);
 cout <<endl<<"Postorder scanning..."<<endl;
 postorder(root);
 cout<<endl;
 cout << "Array : "<<endl;
 for(i=0;i<11;i++)
  cout <<arrayx[i] <<"  ";
 cout<<endl;
 cout <<"Sayac = "<<counter<<endl;
 cout <<endl<< "Press 1 to delete a node from to tree or 0 to pass and exit : ";
 cin.ignore(0,'\n');
 cin>>key;
 cout << key<<endl;
 if(key==1)
 remove(root);
 inorder(root);
 getch();
}
void create(treePtr *proot,int qvalue){
 treePtr tara,yeni;
 yeni = new Tree;
 tara = *proot;
 yeni->value=qvalue;
 yeni->leftptr = NULL;
 yeni->rightptr = NULL;
 bool bulundu = false;
 if((*proot)==NULL){//ilk düğüm ekleniyor
  *proot = yeni;
 }
 while(tara!=NULL && bulundu!=true){
  if(qvalue<tara->value){
   if(tara->leftptr!=NULL)
    tara=tara->leftptr;
   else{
    tara->leftptr = yeni;
    bulundu = true;
   }     
  }
  else if(qvalue>tara->value){
    if(tara->rightptr!=NULL)
     tara=tara->rightptr;
    else{
     tara->rightptr = yeni;
     bulundu = true;
    }
  }
  else 
   break;
 }
}
void inorder(treePtr p){
 if(p){
  inorder(p->leftptr);
  cout << p->value <<"  ";
  inorder(p->rightptr);
 }
}
void postorder(treePtr p){
 if(p){
  inorder(p->leftptr);
  inorder(p->rightptr);
  cout << p->value <<"  ";
  
 }
}
void preorder(treePtr p){
 if(p){
  cout << p->value <<"  ";
  inorder(p->leftptr);
  inorder(p->rightptr);
 }
}
void go_array(treePtr p,int q[dimension],int *s){

 if(p){
  
  go_array(p->leftptr,q,s);
  q[*s]=p->value;
  (*s)++;
  go_array(p->rightptr,q,s);
  
  
 }
}
void remove(treePtr p){
 
 int sil;
 cout << "NE SILMEK ISTIYORSUN : ";
 cin.ignore(2,'\n');
 cin >> sil;
 char yon;
 treePtr tara,ust;
 tara = p;
 bool var = false;
 while(tara && var!=true){
  if(sil<tara->value){
    ust=tara;
    yon = 'l';
    tara=tara->leftptr;
  }else if(sil>tara->value){
    ust=tara;
    yon = 'r';
    tara=tara->rightptr;
  }else
   var = true;
 }
 if(var==true){
  if(yon=='l')
   silme(&(ust->leftptr));
  else if(yon=='r')
   silme(&(ust->rightptr));
  else 
   silme(&p);
 }
 else
  cout << "HATA" <<endl;

}
void silme(treePtr *s){
 treePtr r,q;
 r=*s;
 if(r==NULL)
  return;
 else if(r->rightptr==NULL){
  *s=r->leftptr;
  delete r;
 }
 else if(r->leftptr==NULL){
  *s=r->rightptr;
  delete r;
 }
 else {
  for(q=r->rightptr;q->leftptr;q=q->leftptr);
  q=r->leftptr;
  *s=r->rightptr;
  delete r;
 }

}