Λιγο βοήθεια με τη C

Chameleon cham_gss at hotmail.com
Fri Jun 2 00:17:35 EEST 2006


An lew malakies einai epeidh eimai piwmenos. Exw katebasei ikanh 
posothta ouzou.


Auto poy xrhsimopoieis einai C++
an 8eleis C sketh, xrhsimopoihse printf anti gia cout.

Anti na xrhsimopoieis auto to blakwdes algori8mo ta3inomhshs, 
xrhsimopoihse quick sort.

Den xreiazetai na ton ylopoihseis. yparxei etoimos sthn ANSI C.

apo to MSDN:
----------------------
Performs a quick sort.


void qsort(
    void *base,
    size_t num,
    size_t width,
    int (__cdecl *compare )(const void *, const void *)
);



Parameters
base
Start of target array.	****** matrix

num
Array size in elements.	****** sizeof( matrix )/sizeof( matrix[0] )

width
Element size in bytes.	****** sizeof double

compare
Comparison function. The first parameter is a pointer to the key for the 
search and the second parameter is a pointer to the array element to be 
compared with the key.
-----------------------

prepei na ylopoihseis mia synarthsh poy na elegxei tous dyo double. 
(mesw pointer o elegxos)





An telika to 8eleis se C++ xrhsimopoihse ena map<double, int>. Auto den 
8elei kan ta3inomhsh.

An to 8eleis se C, gia kalytera, ta3inomhse ta kai meta kane ena struct 
san ki auto:

struct {
	double key;
	int freq;
} map_element;

mporeis na epistrefeis ena pointer se array apo map_element apo thn 
freq_array. Prepei na kaneis allocation mesa sth freq_array kai 
deallocation ektos auths. Auto den einai kalh texnikh programmatismoy.
Protimhse na kaneis allocation sthn main me osa stoixeia exei to arxiko 
array (ektos ki an 3ereis kati allo gia thn poikilia twn stoixeiwn) kai 
deallocation pali sth main (den 8a exoun xrhsimopoih8ei ola ta stoixeia 
tou neou array pi8anon)


Asxeto, alla eisai ligo off-topic.
dokimase to comp.lang.c h comp.lang.c++ h akoma kalytera to 
alt.comp.lang.learn.c-c++



Dimitris Mexis wrote:
> Exo ton parakato kodika, pistevo oti tha nai efkolo poli, se kapoion na to
> trexi me ena gcc apla...
> To provlima einai oti stelno loipon, mia matrix[] array stin sinartisi
> selsort(), mou sortari tin array, pame kala, stelno meta tin matrix[]
> array sto freq_array() kai pragmati petixeno na perno, poses times ehei
> vrei...OMOS. Exo kolisi sto pos telika na paro mia struct(?), array(?),
> list(? den xero kai STL ), me tis metavlites kai tin sixnotita apla pou
> emfanizontai...diladi mia nea isos matrix, pou na leei oti telika to 1 to
> de 1 , to 2 to vrike 3, klp klp, kai oxi 1,1,2,2,2,3,3,4 klp klp...
> Prospatho na vgalo mia routina pou apla na mou epistrefi tin sixnotita
> emfaniseis ton apotelesmaton tis matrix....(Me para poli apla logia)
> :-) 
> Efharisto!
> 
> void freq_array( double mtx[], int elements ){
> //The mtx is a ghost of the matrix I transfer, VORSICHT!!!
> 
> 	selsort( mtx, elements );
> 
> 	int found = 0;
> 	
> 	cout << "...after!" << endl;
> 	for ( int i = 0; i < elements; i++ ){
> 		for ( int x = 0; x < elements; x++ ){
> 			if ( mtx[x] == mtx[i] ){
> 				found++;
> 				}
> 		}
> 		cout << "Found value :" << mtx[i] << ", times :" << found << endl;
> 		found = 0;
> 	}
> }
> 
> //Selection Sort function
> //Takes two parameters:
> //int *array- the array with the numbers to be sorted
> //int size- the size of the array
> void selsort( double *array,int size){
> 	int min;int b;
> 	//This loop goes through the whole array
> 	for( int a = 0; a < size-1; a++ ){
> 		b=a;
> 		min=array[b];	//Get the current value
> 				//...and check if any of the rest numbers is lower
> 		for(int j=a+1;j<size;j++){
> 			if( array[j]< min ){
> 				b = j;
> 				min = array[b]; //...and if yes, then get it
> 			}
> 		}
> 		//Switch the values...
> 		array[b]=array[a];
> 		array[a]=min;
> 	}
> }
> 
> int main(int argc, char *argv[])
> {
> 	double matrix[10];
> 	cout << "before..." << endl;
> 	for ( int i = 0; i < 10; i++ ){
> 		matrix[i] = 10 - i;
> 		matrix[3] = 3;
> 		matrix[4] = 3;
> 		matrix[1] = 2;
> 		matrix[0] = 0;
> 		cout << matrix[i] << endl;
> 		}
> 	freq_array( matrix, ( sizeof( matrix )/sizeof( matrix[0] ) ) );
> 	
> 	exit(0);
> }
> 
> 



More information about the Linux-greek-users mailing list