PDA

Tam Sürümünü Görmek İçin : search ve sort sorusu


CaCao
04/11/2007, 21:57
selamlar..
sorunum su
iki tane farkli array yapicam bunlara girdigim sayilar negatif sayi olana kadar devam edicek
sonra bu iki arraydeki sayilari bubble sort yapicam
en son islemde ise merge ile birlikte bu iki elementi sort edilmis sekilde 3. bir arraye yerlestircem.
yani soyle:

Enter the elements of array1:
5 7 1 9 -1
Enter the elements of array2
12 55 17 8 -1
Merged array:
1 5 7 8 9 12 17 55

merged kismi calismadi bende =(
yardimci olursaniz sevinirim..

#include<stdio.h>
#include<stdlib.h>
#define SIZE1 20
#define SIZE2 30

int
main(void)
{
int ary1[SIZE1],
ary2[SIZE2],
merge[SIZE1+SIZE2],
i,
j,
k,
l,
num1,
num2,
num3,
pass,
temp;

j=0;
k=0;
l=0;
i=0;
temp=0;

//get the first array elements
printf("\nEnter the elements of array1: ");
scanf("%d",&ary1[j]);

//continue till the negatşve value
while(ary1[j]>0)
{
j++;
scanf("%d",&ary1[j]);
}

//sort the first array as bubble

for(pass=1;pass<j-1;pass++)
{
for(i=0;i<j-1;i++)
{
//make swamp
if(ary1[i]>ary1[i+1])
{
temp=ary1[i];
ary1[i]=ary1[i+1];
ary1[i+1]=temp;
}
}
}

//get the second array elements
printf("\n");
printf("Enter the elements of array2:\n");
scanf("%d",&ary2[k]);

//continue till the negative value
while(ary2[k]>0)
{
scanf("%d",&ary2[k]);
}

//sort the second array as bubble

for(pass=1;pass<k-1;pass++)
{
for(i=0;i<k-1;i++)
{
//make swamp
if(ary2[i]>ary2[i+1])
{
temp=ary2[i];
ary2[i]=ary2[i+1];
ary2[i+1]=temp;
}
}
}

//merge these two arrays and put them into the third array

printf("\nMerged array:\n");

for(i=0; i<num3; i++)
{

while(ary1[j]>0 && j<=num1 &&ary2[k]>0 && k<=num2)
{
if(ary1[j]<ary2[k])
{
merge[i]=ary1[j];
j++;
i++;

}
if(ary1[j]>ary2[k])
{
merge[i]=ary2[k];
k++;
i++;

}
}

//put the other elements into the third array

while(j<num1)
{
merge[i]=ary1[j];
j++;
i++;
}

while(k<num2)
{
merge[i]=ary2[k];
k++;
i++;
}


printf("%d ",merge[i]);

}

system("PAUSE");
return(0);
}