I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c
Undefined first referenced
symbol in file
discrete_uniform /tmp/scratch.hYayTQ/ccRDoT1o.o
printFirst /tmp/scratch.hYayTQ/ccRDoT1o.o
addToList /tmp/scratch.hYayTQ/ccRDoT1o.o
printSecond /tmp/scratch.hYayTQ/ccRDoT1o.o
disparity /tmp/scratch.hYayTQ/ccRDoT1o.o
clearList /tmp/scratch.hYayTQ/ccRDoT1o.o
ld: fatal: Symbol referencing errors. No output written to ex3test
collect2: ld returned 1 exit status

how can I fix this?

#include <stdio.h>
#include <stdlib.h>

/* Step 1: define your struct here */
typedef struct _Node {
  int data1, data2;
  struct _Node* next;
  struct _Node* next2;
} Node;

typedef struct _List {
  Node* front;
  Node* front2;
  Node* rear;
  Node* rear2;
} List;

static List list;

/* Step 2: put code in the functions below to make the list work. */

void clearList ()
{
  while (list.front)
  {
    Node* tmp = list.front;
    list.front = list.front->next;
    free(tmp);
  }

  list.front = list.front2 = list.rear = list.rear2 = NULL;
}

void addData1(Node* newNode, int data1)
{
  Node* prev = NULL;
  Node* iter = list.front;

  newNode->data1 = data1;

  while (iter && iter->data1 < data1)
  {
    prev = iter;
    iter = iter->next;
  }
  
  if (list.front == NULL)
  {
    list.front = list.rear = newNode;
    newNode->next = NULL;
  }
  else
    if (prev == NULL)
    {
      newNode->next = list.front;
      list.front = newNode;
    }
    else {
      newNode->next = iter;
      prev->next = newNode;
      if (prev == list.rear)
        list.rear = newNode;
    }
}

void addData2(Node* newNode, int data2)
{
  Node* prev = NULL;
  Node* iter = list.front2;

  newNode->data2 = data2;
  while (iter && iter->data2 < data2)
  {
    prev = iter;
    iter = iter->next2;
  }
  
  if (list.front2 == NULL)
  {
    list.front2 = list.rear2 = newNode;
    newNode->next2 = NULL;
  }
  else
    if (prev == NULL)
    {
      newNode->next2 = list.front2;
      list.front2 = newNode;
    }
    else {
      newNode->next2 = iter;
      prev->next2 = newNode;
      if (prev == list.rear2)
        list.rear2 = newNode;
    }
}

void addToList (int data1, int data2)
{
  Node* newNode = (Node*)malloc(sizeof(Node));

  addData1(newNode, data1);
  addData2(newNode, data2);
}

void printFirst ()
{
  Node* iter = list.front;

  while (iter)
  {
    printf("(%d %d)\n", iter->data1, iter->data2);
    iter = iter->next;
  }
}

void printSecond ()
{
  Node* iter = list.front2;

  while (iter)
  {
    printf("(%d %d)\n", iter->data1, iter->data2);
    iter = iter->next2;
  }
}

int findPos2(int data1, int data2)
{
  int pos = 0;
  Node* iter = list.front2;

  while (iter && !(iter->data1 == data1 && iter->data2 == data2))
  {
    ++pos;
    iter = iter->next2;
  }

  return pos;
}

int disparity ()
{
  int result = 0;
  Node* iter = list.front;
  int pos = 0;

  while (iter)
  {
    result += abs(findPos2(iter->data1, iter->data2) - pos);

    iter = iter->next;
    ++pos;
  }

  return result;
}

Edited 7 Years Ago by Samyx: n/a

Attachments
#include <stdio.h>
#include <math.h>
#include <string.h>

#define boolean int
#define FALSE 0
#define TRUE 1

/* These are the functions implemented in the linked list code */
extern void clearList ();
extern void addToList (int,int);
extern void printFirst ();
extern void printSecond ();
extern int disparity ();

/* This is needed for random generation */
extern discrete_uniform (int, int);

void permute (int *A, int n)
{
  int i = 0;
  for (i=0; i<n; i++) {
    /* Pick a random element to move to i-th position. */
    int k = discrete_uniform (i+1, n-1);
    int temp = A[i];
    A[i] = A[k];
    A[k] = temp;
  }
}

void addElements (int n)
{
  int i;
  int *perm1, *perm2;

  /* First, make two random permutations. */
  perm1 = (int*) malloc (sizeof(int) * n);
  perm2 = (int*) malloc (sizeof(int) * n);
  for (i=0; i<n; i++) {
    perm1[i] = perm2[i] = i;
  }
  permute (perm1, n);
  permute (perm2, n);
    
  for (i=1; i<=n; i++) {
    addToList (perm1[i], perm2[i]);
  }
}


void test (char *testName, int numTrials, int listSize, boolean printList)
{
  double totalDisp, avgDisp;
  int n;

  /* Repeat over a number of trials and estimate average. */
  totalDisp = 0;
  for (n=0; n<numTrials; n++) {
    clearList ();
    addElements (listSize);
    totalDisp += disparity ();
  }

  printf ("-----------------------\n");
  printf ("%s\n", testName);
  if (printList) {
    printFirst ();
    printSecond ();
  }
  printf("Average disparity: %lf\n", (totalDisp/numTrials));

}

int main (int argc, char **argv) 
{
  // Simple tests.
  test ("Test 1: 1 element", 1, 1, TRUE);
  test ("Test 2: 5 elements", 1, 5, TRUE);
  test ("Test 3: 10 elements", 1, 10, TRUE);

  // Statistics.
  test ("Test 4: 10 elements", 10000, 10, FALSE);
  test ("Test 5: 100 elements", 10000, 100, FALSE);
}
#include <stdio.h>
#include <math.h>


static long r_seed = 12345678L;

/* Uniform [0,1] using the standard Lehmer generator. 
   See http://www.seas.gwu.edu/~simhaweb/java/lectures/appendix/random.html
   for more details. */

double uniform ()
{
  static long m = 2147483647;
  static long a = 48271;
  static long q = 44488;
  static long r = 3399;
  long t, lo, hi;

  hi = r_seed / q;
  lo = r_seed - q * hi;
  t = a * lo - r * hi;
  if (t > 0)
    r_seed = t;
  else
    r_seed = t + m;
  return ( (double) r_seed / (double) m );
}



/* Uniform random number generator - returns a number between a and b */ 

double uniform_range (double a, double b)
{
  if (b > a)
    return ( a + (b-a) * uniform() );
  else { 
    printf ("ERROR: in uniform.uniform_range(): a=%lf b=%lf\n",a,b); return 0;
  }
}



/* Uniform random number generator - returns an integral number between a and b */ 

long discrete_uniform (long a, long b)
{
  if (b > a) {
    double x = uniform ();
    long c = ( a + (long) floor((b-a+1)*x) );
    return c;
  }
  else if (a == b) return a;
  else { printf("ERROR in uniform.discrete_uniform(): a=%2ld b=%2ld\n",a,b);return 0;}
}



/* Generate a random int from the given discrete pmf */

int generateFromPmf (double *pmf, int length)
{
  int lastIndex, i;
  double u, sum;

  if ( ( pmf == NULL) || (length <= 1) ){
    printf ("ERROR: in uniform.generateFromPmf(): pmf null or zero-length");
    return 0;
  }
    
  lastIndex = length - 1;
  u = uniform();
  i = 0;
  sum = pmf[0];
  while ( (i < lastIndex) && (u > sum) ) {
    i++;
    sum += pmf[i];
  }
  return i;
}



/* Generate from zipf distribution of given size. */

int zipf (int size) 
{
  double *pmf, sum;
  int index, i;

  pmf = (double*) malloc (sizeof(double)*(size+1));
  sum = 0;
  pmf[0] = 0;
  for (i=1; i<=size; i++) {
    pmf[i] = 1.0 / (double) i;
    sum += pmf[i];
  }
  for (i=1; i<=size; i++) {
    pmf[i] = pmf[i] / sum;
  }
  index = generateFromPmf (pmf, size+1);
  if (index == 0)
    index = 1;
  return index;
}



/* Reverse zipf, has an increasing pmf */

int reverseZipf (int size) 
{
  double *pmf, sum;
  int index, i;

  pmf = (double*) malloc (sizeof(double)*(size+1));
  pmf[0] = 0;
  sum = 0;
  for (i=1; i<=size; i++) {
    pmf[i] = 1.0 / (double) (size-i+1);
    sum += pmf[i];
  }
  for (i=1; i<=size; i++) {
    pmf[i] = pmf[i] / sum;
  }
  index = generateFromPmf (pmf, size+1);
  if (index == 0)
    index = 1;
  return index;
}

I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c


Do you mean when you compile ex3text.c it gives you errors

Edited 7 Years Ago by gerard4143: n/a

I am using the file ex3test.c to run the following program, but the UNIX gives the errors:

[38] $ gcc -o ex3test ex3test.c


Do you mean when you compile ex3text.c it gives you errors

yes

First of all, try something like this:

gcc -Wall -Wextra -o exe3test exe3test.c uniform.c -lm

The -Wall & -Wextra switches will give you a little more info about what you have done wrong in your code, and where. As you can see, I added the uniform.c file to the compile - if you are going to depend on functions found in another file, then you need to add that when compiling. Finally, if you are going to #include <math.h>, then you need to add the -lm switch at the end of your compiler command.

BTW, this will not clean up your compiler output (it will actually look worse), or produce an executable, but at least you will know what you have to start fixing in order to get there.

Edited 7 Years Ago by kermit: n/a

thank you for the wall extra information.
I had used the -lm earlier. I had compiled the ex3.c ex3test.c uniform.c before I wrote any line of code :/

those are the errors I am getting:

$ gcc -Wall -Wextra -o ex3test ex3test.c
ex3test.c:17: warning: type defaults to `int' in declaration of `discrete_uniform'
ex3test.c: In function `addElements':
ex3test.c:37: warning: implicit declaration of function `malloc'
ex3test.c: In function `test':
ex3test.c:53: warning: unused variable `avgDisp'
ex3test.c: At top level:
ex3test.c:75: warning: unused parameter 'argc'
ex3test.c:75: warning: unused parameter 'argv'
ex3test.c: In function `main':
ex3test.c:85: warning: control reaches end of non-void function
Undefined first referenced
symbol in file
discrete_uniform /tmp/scratch.hYayTQ/ccwzHMjg.o
printFirst /tmp/scratch.hYayTQ/ccwzHMjg.o
addToList /tmp/scratch.hYayTQ/ccwzHMjg.o
printSecond /tmp/scratch.hYayTQ/ccwzHMjg.o
disparity /tmp/scratch.hYayTQ/ccwzHMjg.o
clearList /tmp/scratch.hYayTQ/ccwzHMjg.o
ld: fatal: Symbol referencing errors. No output written to ex3test
collect2: ld returned 1 exit status

I fixed some of the problems I had before and my program is compiling. However, when I run the program this is the error I am getting:
...
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
ERROR in uniform.discrete_uniform(): a=100 b=99
...

This question has already been answered. Start a new discussion instead.