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

V13 v13 at priest.com
Sun Jul 2 21:36:59 EEST 2006


On Thursday 01 June 2006 23:26, 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)

Kati san ayto?

-----
#include <iostream>
#include <map>

using namespace std;

// We will need those:
typedef map<double,int>	t_cnt;
typedef t_cnt::iterator i_cnt;

t_cnt freq_array2(double *mtx, int elements)
{
 int n;
 t_cnt ret;
 i_cnt i;

 // For each element
 for (n=0; n<elements; n++)
 {
  // If there is an existing entry
  i=ret.find(mtx[n]);
  if (i!=ret.end())
   i->second++; // Increase the counter
//   ret[mtx[n]]++; // Increase the counter
  else
   ret[mtx[n]]=1; // Else, set the counter to 1
 }

 return(ret);
}

int main()
{
 t_cnt cnt;
 i_cnt i;
 double matrix[10];

 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;
 }

 cnt=freq_array2(matrix, 10);

 for (i=cnt.begin(); i!=cnt.end(); i++)
  cout << i->first << ": " << i->second << endl;
}

-----

Output:

0
2
8
3
3
5
4
3
2
1
0: 1
1: 1
2: 2
3: 3
4: 1
5: 1
8: 1
-----

<<V13>>




More information about the Linux-greek-users mailing list