الموضوع حول خوارزمية الترتيب الفقاعي, لكن هنا التعامل مع مصفوفة ثنائية. النتائج تعرض على الـ Console و تخزن في ملف matrix.txt
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
using namespace std;
void convertToArray(int);
void matrixSort(int *,int);
void swap (int *,int *);
int convertToMatrix();
int arrayValue();
int array[100];
int matrix[10][10];
int main()
{
FILE *F;
F = fopen("matrix.txt","a+");
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
matrix[j] = 1 + rand() % 9;
}
}
cout<<"Matrix[10][10], has the following elemnts:"<<endl;
fprintf(F,"Matrix[10][10], has the following element:\n");
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
cout<<" ["<<matrix[j]<<"]"<<setw(2);
fprintf(F,"[%i] ",matrix[j]);
}
cout<<endl;
fprintf(F,"\n");
}
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
int holder;
holder = matrix[j];
convertToArray(holder);
}
}
cout<<endl;
matrixSort(array,100);
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
matrix[j] = convertToMatrix();
}
}
cout<<endl<<"Your matrix after sorting: "<<endl;
fprintf(F,"\nYour matrix after sorting:\n");
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
cout<<" ["<<matrix[j]<<"]"<<setw(2);
fprintf(F,"[%i] ",matrix[j]);
}
cout<<endl;
fprintf(F,"\n");
}
cout<<endl;
cout<<endl;
fclose(F);
getch();
return 0;
}
void convertToArray(int x)
{
static int i = 0;
array = x;
i++;
}
void matrixSort(int *array,int size)
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size-1; j++)
{
if(array[j] > array[j+1])
swap(&array[j],&array[j+1]);
}
}
}
void swap (int *element1,int *element2)
{
int temp;
temp = *element1;
*element1 = *element2;
*element2 = temp;
}
int convertToMatrix()
{
int static i = 0;
int holder;
holder = array;
i++;
return holder;
}الناتج لدينا:
Matrix[10][10], has the following element: [6] [9] [8] [5] [9] [2] [4] [1] [8] [3] [9] [3] [8] [7] [8] [6] [8] [9] [4] [1] [1] [7] [6] [1] [5] [8] [7] [6] [9] [6] [3] [1] [3] [1] [7] [5] [9] [2] [8] [4] [3] [7] [3] [4] [7] [3] [4] [8] [3] [2] [6] [6] [2] [7] [4] [8] [3] [4] [8] [5] [5] [3] [6] [7] [1] [2] [5] [6] [5] [5] [6] [1] [6] [7] [8] [6] [4] [7] [4] [3] [1] [6] [1] [2] [1] [6] [8] [6] [9] [2] [7] [4] [3] [2] [3] [2] [9] [4] [7] [9] Your matrix after sorting: [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [2] [2] [2] [2] [2] [2] [2] [2] [2] [3] [3] [3] [3] [3] [3] [3] [3] [3] [3] [3] [3] [3] [4] [4] [4] [4] [4] [4] [4] [4] [4] [4] [4] [5] [5] [5] [5] [5] [5] [5] [5] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [6] [7] [7] [7] [7] [7] [7] [7] [7] [7] [7] [7] [7] [8] [8] [8] [8] [8] [8] [8] [8] [8] [8] [8] [8] [9] [9] [9] [9] [9] [9] [9] [9] [9]
موضوع بسيط و سهل جدا, أحببت فقط فكرة ترتيب مصفوفة ثنائية, و التعامل مع الملفات.