lundi 26 octobre 2015

Random() in the stdlib.h library

I'm using code blocks in Ubuntu 15.04.

The random() function does not work properly and I don't know why.

This is my code:

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

const int LENG = 9;

void creating_arr (int matrix[][LENG])
{
    int i,j;
    for (i = 0; i < LENG; i++)
        for (j = 0; j < LENG; j++)
            matrix[i][j] = random(40);
}

void printing_arr (int matrix[][LENG])
{
    int i,j;
    printf("The array is: \n");
    for (i = 0; i < LENG; i++)
    {
        printf("\n");
        for (j = 0; j < LENG; j++)
            printf("%3d ", matrix[i][j]);
    }
}

int sum_arr (int matrix[][LENG])
{
    int i, j, sum = 0;

    for (i = 0; i < LENG; i++)
        for (j = 0; j < LENG; j++)
            sum += matrix[i][j];
    printf ("\n\rSum of array is:");
    return sum;
}

int max_arr (int matrix[][LENG])
{
    int i, j, max = matrix[0][0];

    for (i = 0; i < LENG; i++)
        for (j = 0; j < LENG; j++)
            if (max < matrix[i][j])
                max = matrix[i][j];
    printf ("\n\rMax of array is:");
    return max;

}

void main()
{
    randomize();
    int arr[LENG][LENG];
    creating_arr (arr);
    printing_arr (arr);
    printf(" %d ",sum_arr (arr));
    printf ("%d", max_arr (arr));
}

The debugger returns:

prog.c: In function 'creating_arr':
prog.c:12:19: warning: implicit declaration of function 'random' [-Wimplicit-function-declaration]
    matrix[i][j] = random(k);
                   ^
prog.c: At top level:
prog.c:51:6: warning: return type of 'main' is not 'int' [-Wmain]
 void main()
      ^
prog.c: In function 'main':
prog.c:53:2: warning: implicit declaration of function 'randomize' [-Wimplicit-function-declaration]
  randomize();
  ^
/home/LI8BoL/cc4BQqdc.o: In function `main':
prog.c:(.text.startup+0x10): undefined reference to `randomize'
collect2: error: ld returned 1 exit status

Can someone help me? I really need to get this done for a school project.




Aucun commentaire:

Enregistrer un commentaire