Avatar

just to show.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Count Sort in c++


#include < iostream.h >
#include < stdio.h >
#include < conio.h >

    void print(int a[]) {
  for (int i = 0; i < 10; i++) {
    cout < < a[i] < < "-";
  }
  cout < < endl;
}

int max(int a[]) {
  int mac = 0;
  for (int i = 0; i < 10; i++) {
    if (a[i] > mac) {
      mac = a[i];
    }
  }
  cout < < "ths is mac " < < mac < < endl;
  return mac;
}

void countTimes(int a[], int b[]) {
  int maxi = max(a);

  for (int i = 0; i < = maxi; i++) {
    b[i] = 0;

  }
  for (i = 0; i < 10; i++) {
    b[a[i]] = b[a[i]] + 1;
  }
}

void countRanks(int b[]) {
  for (int i = 1; i < = 7; i++) {
    b[i] += b[i - 1];
  }
}
void countSort(int a[], int b[], int c[]) {
  int l = 0;
  for (int i = 9; i >= 0; i--) {
    cout < < a[i] < < "-" < < b[a[i]] < < "\n";
    c[b[a[i]] - 1] = a[i];
    b[a[i]] = b[a[i]] - 1;
  }
  print(c);
}

void main() {
  clrscr();

  int a[] = {
      1, 2, 4, 6, 7, 7, 0, 6, 3, 5};
  print(a);
  int * b;
  int mac = max(a);
  int c[] = {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  b = new int[mac];

  countTimes(a, b);
  countRanks(b);
  countSort(a, b, c);
  print(c);
}

Refactorings

No refactoring yet !

Avatar

KangOl

November 3, 2007, November 03, 2007 22:27, permalink

No rating. Login to rate!

I don't understand what you're trying to do...

776160fbb8bda99799a240df27a2b548

Fluxus

November 7, 2007, November 07, 2007 10:37, permalink

No rating. Login to rate!

You can use template to implicitly pass to the function the size of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include < iostream.h >
#include < stdio.h >
#include < conio.h >

template <class T, int Size>
void print(T (&a)[Size]) {
  for (int i = 0; i < Size; i++) {
    cout < < a[i] < < "-";
  }
  cout < < endl;
}

template <class T, int Size>
int max(T (&a)[Size]) {
  int mac = 0;
  for (int i = 0; i < Size; i++) {
    if (a[i] > mac) {
      mac = a[i];
    }
  }
  cout < < "ths is mac " < < mac < < endl;
  return mac;
}

template <class T, int Size>
void countRanks(T (&b)[Size]) {
  for (int i = 1; i < = Size; i++) {
    b[i] += b[i - 1];
  }
}

void main() {
  clrscr();

  int a[] = {
      1, 2, 4, 6, 7, 7, 0, 6, 3, 5};
  print(a);
  int * b;
  int mac = max(a);
  int c[] = {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  b = new int[mac];

  countRanks(b);
  print(c);
}

Your refactoring





Format Copy from initial code

or Cancel