Program Compile Errors

Hi

I was wondering if you could help me find a solution to my compile errors in my program. My program is supposed to generate random numbers specified by the user, and then sort them using the bubble sort. I created the code today based on another piece of work, but it seems to be getting too many compile errors. My code is:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
using namespace std;

void generate(intArray nums, int size, int low, int high);
void bubSort(intArray nums, int size);
void displayNums(intArray nums, int size);
void display(int n);
bool notInOrder(int a, int b);

int main()
{
  typedef intArray[];
  const int MAX_SIZE = 5000; // arbitrary
  int numbers[MAX_SIZE];
  int howMany;

  cout << "How many numbers to sort? ";
  cin << howMany;

  if(howMany > MAX_SIZE)
    {
      howMany = MAX_SIZE;
    }

  generate(numbers, howMany, 0, 999);
  cout << "Numbers generated..." << endl;
  displayNums(numbers, howMany);
  cout << "After sorting..." << endl;
  displayNums(numbers, howMany);
}

void generate(intArray nums, int size, int low, int high)
{
  int n, i = 0;
  do
    {
      n = rand();
      if(n >= low && n <= high)
	{
	  nums[i] = n;
	  i++;
	}
    }while (i <= size);
}

void bubSort(intArray nums, int size)
{
  for(int k = size-2; k >= 0; k--)
    {
      for(int i = 0; i <= k; i++)
	{
	  if(notInOrder(nums[i], nums[i+1]))
	    {
	      swap(nums[i], nums[i+1]);
	    }
	}
    }
}

void displayNums(intArray nums, int size)
{
  for(int s = 1; s <= size; s++)
    {
      cout.width(4);
      cout << nums[s];
      if(s != 0 && s%20 == 0)
	{
	  cout << endl;
	}
    }
}

bool notInOrder(int a, int b)
{
  return(a > b);
}

void swap(int& a, int&b)
{
  int temp;
  temp = a;
  a = b;
  b = temp;
}


And the compile errors I'm getting are:
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
tut3.2.cc:5: `intArray' was not declared in this scope
tut3.2.cc:5: parse error before `,'
tut3.2.cc:6: `intArray' was not declared in this scope
tut3.2.cc:6: parse error before `,'
tut3.2.cc:7: `intArray' was not declared in this scope
tut3.2.cc:7: parse error before `,'
tut3.2.cc: In function `int main()':
tut3.2.cc:13: ANSI C++ forbids declaration `intArray' with no type
tut3.2.cc:19: no match for `_IO_istream_withassign & << int &'
tut3.2.cc: At top level:
tut3.2.cc:33: `intArray' was not declared in this scope
tut3.2.cc:33: parse error before `,'
tut3.2.cc: In function `void generate(...)':
tut3.2.cc:39: `low' undeclared (first use this function)
tut3.2.cc:39: (Each undeclared identifier is reported only once
tut3.2.cc:39: for each function it appears in.)
tut3.2.cc:39: `high' undeclared (first use this function)
tut3.2.cc:41: `nums' undeclared (first use this function)
tut3.2.cc:44: `size' undeclared (first use this function)
tut3.2.cc: At top level:
tut3.2.cc:47: parse error before `,'
tut3.2.cc: In function `void bubSort(...)':
tut3.2.cc:55: implicit declaration of function `int swap(...)'
tut3.2.cc: At top level:
tut3.2.cc:61: parse error before `,'


I would be grateful if you could help me correct these errors.
Last edited on
Start with fixing the first compile error, which is saying that the identifier "intArray" on line 5 has not yet been declared. I can see that later on in the file you are attempting to declare it inside main(), but even that declaration is wrong.

So fix your typedef and move it before line 5. Then see how many other compile errors go away.
Well most of them have, however, there are still some errors:
1
2
3
4
5
tut3.2.cc:5: ANSI C++ forbids declaration `intArray' with no type
tut3.2.cc: In function `int main()':
tut3.2.cc:19: no match for `_IO_istream_withassign & << int &'
tut3.2.cc: In function `void bubSort(int *, int)':
tut3.2.cc:55: implicit declaration of function `int swap(...)' 


Any ideas as to why I am getting this?
Ok forget the above post, My errors have gone down to this:
1
2
3
tut3.2.cc:5: ANSI C++ forbids declaration `intArray' with no type
tut3.2.cc: In function `void bubSort(int *, int)':
tut3.2.cc:55: implicit declaration of function `int swap(...)' 


Any ideas on how I may fix these two errors?
Have you read the errors or just posted them?

I take it the line numbers have all shifted.

typedef intArray[]; is a syntax error because:
1. a typedef sets up a synonym for another type, and you haven't specified the synonym.
2. intArray[] is an imcomplete type.

You can't use << on cin.

You've not declared function swap, used in bubSort.
Ok, I've sorted out the cin error (which I figured out myself in the end (simple error to make when needing something to eat!)). My only problem is the
 
typedef intArray[]


should it really be something like this then?
 
typedef int intArr[]


or can't it be like this because int is a reserved word?

Thank you if you can help.
Ok.

All the errors have been sorted. Now the program doesn't seem to be sorting them into the correct order. I am quite sure that I have done my bubble sort correctly, but It is not sorting them out.

Any ideas where the bubble sort is wrong?
If you don't have a debugger, you could write out the values that swap and NotInOrder are seeing using cout.

Also, it may be easier to debug if you start with the same set of number rather than a new random set each time.
Sorry, but I still have no idea why my bubble sort is not sorting the random numbers. All it seems to be doing is displaying the original random numbers generated, then displaying them again as being sorted, but they are not. Here is a sample of my program in execution:
1
2
3
4
5
6
63 jaguar% ./a.out
How many numbers to sort? 10
Numbers generated...
   3  58  60  96  72  55  64  57  31  94
After sorting...
   3  58  60  96  72  55  64  57  31  94


What is the problem? I've not had problems before with bubble sorts, only this one. But need help from you guys on how to make them sort in order of smallet to largest.
closed account (z05DSL3A)
Unless you have modded lines 26 to 30 in your original post:
1
2
3
4
5
  generate(numbers, howMany, 0, 999);
  cout << "Numbers generated..." << endl;
  displayNums(numbers, howMany);
  cout << "After sorting..." << endl;
  displayNums(numbers, howMany);


You never ask for the numbers to be sorted.
Unless I have missed somthing :)
Sorry, I forgot to ask for the numbers to be sorted!

Any ways I've found a new problem now in that it seem to get rid of a couple of numbers when sorting, and then generates ones to replace the ones which get lost like this:

1
2
3
4
Numbers generated...
   4  59  61  97  73  56  65  58  32  95
After sorting...
  32  56  58  59  61  65  73  84  97  95


As you can see 4 should be at the start, but it disappears!, any idea what is going wrong here?

There are a few small issues still remaining.

in your first post:

There is no call to bubSort (ie bubSort(numbers, howMany);)

in bubSort, for(int k = size-2; k >= 0; k--) can be changed to for(int k = size-1; k >= 0; k--) as it was sometimes missing an element to sort

the issue with removing a number is caused by
1
2
3
  for(int k = size-2; k >= 0; k--)
    {
      for(int i = 0; i <= k; i++)

I don't quite know why... but it can be fixed by changing those lines to:
1
2
3
for(int k = 0; k < size; k++)
    {
        for(int i = size-1; i > k; i--)

(was sometimes missing an element also because of int k = size-2; instead of int k = size-1;
Maybe if you step through it (debug) you can find out what was causing that element to be removed, or a more experienced user may be able to tell us.
Good luck!

enduser
There are a few small issues still remaining.

in your first post:

There is no call to bubSort (ie bubSort(numbers, howMany);)

in bubSort, for(int k = size-2; k >= 0; k--) can be changed to for(int k = size-1; k >= 0; k--) as it was sometimes missing an element to sort

the issue with removing a number is caused by
1
2
3
  for(int k = size-2; k >= 0; k--)
    {
      for(int i = 0; i <= k; i++)

I don't quite know why... but it can be fixed by changing those lines to:
1
2
3
for(int k = 0; k < size; k++)
    {
        for(int i = size-1; i > k; i--)

(was sometimes missing an element also because of int k = size-2; instead of int k = size-1;
Maybe if you step through it (debug) you can find out what was causing that element to be removed, or a more experienced user may be able to tell us.
Good luck!

enduser
Topic archived. No new replies allowed.