3

I was reading through material on howstuffworks for cs50 and am having trouble implementing one of the tutorial programs. Specifically, when I copy and paste the following code from page 11 (Arrays) and run the program on the IDE, I get error messages. The code is:

#include <stdio.h>

#define MAX 10

int a;
int rand_seed=10;

/* from K&R
   - returns random number between 0 and 32767.*/
int rand()
{
    rand_seed = rand_seed * 1103515245 +12345;
    return (unsigned int)(rand_seed / 65536) % 32768;
}

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

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

    /* bubble sort the array */
    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;
            }
    /* print sorted array */
    printf("--------------------\n");
    for (i=0; i < MAX; i++)
    printf("%d\n",a);

    return 0;
}

As I was reading through the program, it seemed like something was funky with the handling of 'a' in the sort portion of the program. Sure enough, the error "subscripted value is not an array, pointer, or vector if (a > a[y+1])" came back.

Has anyone else tried implementing the tutorial programs from howstuffworks? If so, have you had problems with their programs?

2
  • I came here with the same issue. An earlier example from that same page says "The following code initializes the values in the array sequentially and then prints them out:" #include <stdio.h> int main() { int a[5]; int i; for (i=0; i<5; i++) a = i; for (i=0; i<5; i++) printf("a[%d] = %d\n", i, a); } But my understanding (which is admittedly minimal) is that this would simply set a equal to 0 through 5, not doing anything to the array, then cause other mischief. In testing that, I copied the code into the IDE, and got compiler errors.
    – Dr.Queso
    Commented Mar 2, 2016 at 0:34
  • I just went through the same thing, and came up with the same solution as below, but frustrating to see reference material so poorly edited. I submitted a Corrections request via HowStuffWorks’ contact form. Commented Jul 15, 2016 at 23:37

1 Answer 1

4

You're correct, that example appears to have been sloppily edited, the indices of array a have been omitted. Here's how it should look like with the correct indices:

#include <stdio.h>

#define MAX 10

int a[MAX];
int rand_seed=10;

/* from K&R
   - returns random number between 0 and 32767.*/
int rand()
{
    rand_seed = rand_seed * 1103515245 +12345;
    return (unsigned int)(rand_seed / 65536) % 32768;
}

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

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

    /* bubble sort the array */
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;
        }
/* print sorted array */
printf("--------------------\n");
for (i=0; i < MAX; i++)
printf("%d\n",a[i]);

    return 0;
}
2
  • Thank you! Those corrections worked. But it's discouraging that reference material for the course is not accurate! I had thought I understood how arrays worked and then got completely confused when reading through that material.
    – Mary Beth
    Commented Feb 19, 2016 at 12:34
  • Thanks ronga. These amendments were very helpful. I figured they had omitted the indices on the arrays, but didn't yet know enough about arrays to have worked out how to fix it. Your solution saved me a lot of time.
    – mike peek
    Commented Nov 6, 2016 at 4:44

You must log in to answer this question.

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