0

I am using exactly the same code as at HowStuffWorks to create a bubbling sorting. But I cannot compile code as I get the following error message:

sorting.c:29:22: error: subscripted value is not an array, pointer, or vector
            if (a > a[y+1])
                    ~^~~~
sorting.c:32:20: error: subscripted value is not an array, pointer, or vector
                a=a[y+1];
                  ~^~~~
sorting.c:33:18: error: subscripted value is not an array, pointer, or vector
                a[y+1]=t;
                ~^~~~

For some reason it doesn't like square brackets.

Here is the code itself:

#include <stdio.h>

#define MAX 10

int a;
int rand_seed=10;

int rand()
{
  rand_seed = rand_seed * 1103515245 +12345;
  return (unsigned int)(rand_seed / 65536) % 32768;
}

int main()
{
  int i,t,x,y;

  for (i=0; i < MAX; i++)
  {
    a=rand();
    printf("%d\n", a);
  }

  for (x=0; x < MAX-1; x++)
    for (y=0; y < MAX-x-1; y++)
      if (a > a[y+1])
      {
        t = a;
        a = a[y+1];
        a[y+1] = t;
      }

  printf("-------------------\n");
  for (i=0; i < MAX; i++)
  printf("%d\n", a);

  return 0;
}
3
  • Is it possible that you want to write i instead of y?
    – MARS
    Aug 13, 2016 at 15:11
  • @MARS, I've tried to replace y with i - didn't solve the issue. Aug 13, 2016 at 15:27
  • you have to declare a global array. Perhaps a[MAX]?
    – MARS
    Aug 13, 2016 at 15:42

2 Answers 2

1

The variable a is declared as a single integer. In the line if (a > a[y+1]) this code attempts to compare a to an array of the same variable name. This presents two problems. First, two vars can't have the same name in C. Second, nowhere in the code is an array declared. So, this will never compile.

I looked at the code on that link. It's unfortunate that they would post code that is so severely flawed. It also demonstrates a poor usage of global vars. While some would say that globals should never be used, they are appropriate when well justified. In this example though, they are only locally used inside of main() and rand(), respectively and should only be declared locally. (More on globals later in the course.)

It would be a very valuable exercise for you to debug this example and get it working on your own!

IF this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks Cliff! I'll try to fix it! Aug 14, 2016 at 6:15
0

Fixed the problem. If someone is interested please check the following code:

#include <stdio.h>

#define MAX 10

int a[MAX];
int rand_seed=10;

int rand()
{
  rand_seed = rand_seed * 1103515245 +12345;
  return (unsigned int)(rand_seed / 65536) % 32768;
}

int main()
{
  int i,t,x,y;

  for (i=0; i < MAX; i++)
  {
    a[i]=rand();
    printf("%d\n", a[i]);
  }

  for (x=0; x < MAX-1; x++)
    for (y=0; y < MAX-x-1; y++)
      if (a[y] > a[y+1])
      {
        t = a[y];
        a[y] = a[y+1];
        a[y+1] = t;
      }

  printf("-------------------\n");
  for (i=0; i < MAX; i++)
  printf("%d\n", a[i]);

  return 0;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .