C++ Arrays&Strucks (Merge Sorted Arrays)
-
arkadaslar soyle birsey yapmaya calisiyorum.
Iki array var bunlari birlestirip kucukten buyuge dogru siralamaya calisiyorum. Asagida su ana kadar geldigim yer var. Bir goz atipta nerde hata yaptigimi soylerseniz sevinirim.
#include <iostream>
//#include <stdlib.h>
using namespace std;
const int size = 100;
int main()
{
//Example Arrays to test
double array1[] ={-70, 3, 5123, 4.32, 595, -586, 293, 29581, 293.948, 0.235, -395981};
double array2[] ={-930, -42, -542, -90.2, -0.32, 0.54, 2394, 59123, 5872};
//Find number of items in arrays
int size1 = sizeof(array1)/sizeof(array1[0]);
int size2 = sizeof(array2)/sizeof(array2[0]);
int i, j;
//Create third array with size equal to two original combined
double array3[size];
//Copying first array into first part of new array
for (i = 0; i < size1; i++)
{
array3[i] = array1[i];
}
//Copy second array into new array so it comes right after copy if first array
for (j = size1; j < (size1 + size2); j++)
{
array3[j] = array2[j];
}
// outputs for testing
for (i = 0; i < size1; i++)
{
cout << array1[i] << " ";
}
cout << endl;
for (j = 0; j < size2; j++)
{
cout << array2[j] << " ";
}
cout << endl;
int size = size1+size2;
i = 0;
j = 0;
for (int t = 0; t < size ; t++)
{
if (array1[i] < array2[j])
{
array3[t] = array1[i];
i++;
}
else if (array2[j] < array1[i])
{
array3[t] = array2[j];
j++;
}
}
//display output array
for (i = 0; i < (size1+size2); i++)
{
cout << array3[i] << " ";
}
system ("Pause");
return 0;
}
-
neyse hatami buldum gerek kalmadi sagolun
-
#include <iostream> //#include <stdlib.h> using namespace std; const int size = 100; int main() { //Example Arrays to test double array1[] ={-70, 3, 5123, 4.32, 595, -586, 293, 29581, 293.948, 0.235, -395981}; double array2[] ={-930, -42, -542, -90.2, -0.32, 0.54, 2394, 59123, 5872}; //Find number of items in arrays int size1 = sizeof(array1)/sizeof(array1[0]); int size2 = sizeof(array2)/sizeof(array2[0]); int i, j,a; int size = size1+size2; //Create third array with size equal to two original combined double array3[size]; //Copying first array into first part of new array for (i = 0; i < size1; i++) { array3[i] = array1[i]; } //Copy second array into new array so it comes right after copy if first array for (j = size1,a=0; j < (size1 + size2); j++,a++) { array3[j] = array2[a]; } cout << "Array 1" << endl; // outputs for testing for (i = 0; i < size1; i++) { cout << array1[i] << " "; } cout << endl; cout << "Array 2" << endl; for (j = 0; j < size2; j++) { cout << array2[j] << " "; } cout << endl; //display output array cout << "Array 3\n"; for (i = 0; i < (size1+size2); i++) { cout << array3[i] << " "; } cout << endl; // simple bubble sort.. cout << "Sorted Array3\n"; for (int t = 0; t < size ; t++) { for (int x=0; x < size-1; x++) { if (array3[x] > array3[x+1]) { double temp = array3[x]; array3[x] = array3[x+1]; array3[x+1] = temp; } } } //display output array for (i = 0; i < (size1+size2); i++) { cout << array3[i] << " "; } return 0; }
Hocam 2. Arrayı atarken ve sıralama yaparken hata yapmıştın.. Bu işini görür sanırım (:
-
ZoktriP bunu yazdı:
-----------------------------
-----------------------------const int size = 100;
..
..
..
int size = size1+size2;
..
...
..Yukarda const tanımlanmış, aşağıda yeniden tanımlanmış. Çalışıyor mu böyle olunca?
-
JPriest bunu yazdı:
-----------------------------
ZoktriP bunu yazdı:
-----------------------------
-----------------------------const int size = 100;
..
..
..
int size = size1+size2;
..
...
..Yukarda const tanımlanmış, aşağıda yeniden tanımlanmış. Çalışıyor mu böyle olunca?
-----------------------------Yukardakini görmemiştim bile :D Kodu direk kopyalayıp üzerinde derleme yaptım..
Çalışıyo bi sıkıntı çıkarmıyo, sonuçta farklı scope ta aynı değişkeni tekrar oluşturdum. Main içinde sıkıntı vermez yani (:
-
Yukardakini silsek bir sıkıntı çıkarmaz o halde. Hatta bence daha iyi olur ;)