Can Some-one help?

This code doesn't Run in MS Visual C++ 6, I get the following compile errors::

1) error C2065: 'display' : undeclared identifier
2) error C2373: 'display' : redefinition; different type modifiers
3) error C2065: 'printf' : undeclared identifier

It was written in C++ so I don't quite understand why it does not Execute

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


#define N    12   // number of elements to permute.  

void QuickPerm(void)
{
   unsigned int a[N], p[N+1];
   register unsigned int i, j, tmp; // Upper Index i; Lower Index j

   for(i = 0; i < N; i++)   // initialize arrays; a[N] can be any type
   {
      a[i] = i + 1;   // a[i] value is not revealed and can be arbitrary
      p[i] = i;
   }
   p[N] = N; // p[N] > 0 controls iteration and the index boundary for i
   display(a, 0, 0);   // remove comment to display array a[]
   i = 1;   // setup first swap points to be 1 and 0 respectively (i & j)
   while(i < N)
   {
      p[i]--;             // decrease index "weight" for i by one
      j = i % 2 * p[i];   // IF i is odd then j = p[i] otherwise j = 0
      tmp = a[j];         // swap(a[j], a[i])
      a[j] = a[i];
      a[i] = tmp;
      display(a, j, i); // remove comment to display target array a[]
      i = 1;              // reset index i to 1 (assumed)
      while (!p[i])       // while (p[i] == 0)
      {
         p[i] = i;        // reset p[i] zero value
         i++;             // set new index value for i (increase by one)
      } // while(!p[i])
   } // while(i < N)
} // QuickPerm()

void display(unsigned int *a, unsigned int j, unsigned int i)            
{
   for(unsigned int x = 0; x < N; x++)
      printf("%d ",a[x]);
   printf("   swapped(%d, %d)\n", j, i);
   return;  // press any key to continue...
} // display() 
In C++, the compiler has to known about the function display before you try to use it.

In C++, printf lives in the cstdio header. You'll have to #include that header.

In C++, we would write void QuickPerm(void) as void QuickPerm().

Looks to me like this wasn't written in C++, but in C.

If this is the whole code, it will never execute - there's no main. Presumably you're calling these functions from some other code.
Last edited on
i need to include a main function which i done but i get these errors::

error C2065: 'display' : undeclared identifier display(a, 0, 0);

error C2373: 'display' : redefinition; different type modifiers
1
2
3
4
5
6
7
8
9
10
int main(){

void display(unsigned int *a, unsigned int j, unsigned int i)            
{
   for(unsigned int x = 0; x < N; x++)
      printf("%d ",a[x]);
   printf("   swapped(%d, %d)\n", j, i);
   return;  // press any key to continue...
}return 0;
} // display() 


error C2601: 'display' : local function definitions are illegal

how do i tell the compiler about display?
how do i tell the compiler about display?


Either be declaring it before you use it:

void display(unsigned int *a, unsigned int j, unsigned int i);

or by moving the entire definition up to before you use it.

This code:
1
2
3
4
5
6
7
8
9
10
int main(){

void display(unsigned int *a, unsigned int j, unsigned int i)            
{
   for(unsigned int x = 0; x < N; x++)
      printf("%d ",a[x]);
   printf("   swapped(%d, %d)\n", j, i);
   return;  // press any key to continue...
}return 0;
} // display()  

is a mess and indicates that you aren't actually familiar with how to write C or C++ code. Function definitions must not contain other function definitions. You've got a function, main, containing an entire definition of another function, display.

The structure of your code should be something like:
1
2
3
4
5
6
7
8
9
10
11
// Necessary header file includes

// Definition of display function

// Definition of Quickperm function

int main()
{
  Quickperm();
  return 0;
}
Last edited on
Topic archived. No new replies allowed.