JorgeeFG
JorgeeFG

Reputation: 5961

MS VS 2010 srand() not compiling?

I have a piece of code ( I'm student ) that "should" work in theory, but Microsoft's Visual Studio 2010 seems to have a problem with srand, because it's not being highlighted like other reserved names.

If I remove srand from Auto_Complete_Matrix then code will compile with no problem.

Function Auto_Complete_Matrix

Error 2 error C2143: error de sintaxis : missing ';' ahead of 'type' c:\users\jorgee!\desktop\uade\program. 1\proyectos\tp3-matrices\ejercicio 2\main.c 46 1 Ejercicio 2

I've included stdlib and time libraries.

Thanks a lot for the help.

/* 2.   Realizar una función que determine si una matriz cuadrada de dimensión N 
es simétrica con respecto a su diagonal principal. */


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

#define FIL 5
#define COL 5

int True_False( char *message );
int Auto_Complete_Matrix( int matrix[ FIL ][ COL ] );
int Manual_Complete_Matrix( int matrix[ FIL ][ COL ] );
void Print_Matrix( int matrix[ FIL ][ COL ] );

int main () {
    int matrix[ FIL ][ COL ];
    if( True_False( "Desea autocompletar la Matriz con números al azar? <Si / No> \n\n" ) ) {
        Auto_Complete_Matrix( matrix );
    }
    else {
        Manual_Complete_Matrix( matrix );
    }
    Print_Matrix( matrix );
    system( "pause" );
}

int True_False( char *message ) {
    char Answer[3];
    fputs( message, stdout );
    fgets( Answer, 3, stdin );
    if ( Answer[0] == 1 ) {
        return 1;
    }
    if ( strncmp( Answer, "Si", 2 ) == 0 || strncmp( Answer, "si", 2 ) ) {
        return 1;
    }
    return 0;
}

int Auto_Complete_Matrix ( int matrix[ FIL ][ COL ] ) {

    srand(time(0));
    int i, j;

    for ( i = 0; i < FIL; i ++ ) {
        for ( j = 0; j < COL; j ++ ) {
            matrix[i][j] = rand() % (100 - 0 + 1) + 0;
        }
    }
    return 0;
}

int Manual_Complete_Matrix( int matrix[ FIL ][ COL ] ) {

    int i, j;

    for ( i = 0; i < FIL; i++ ) {
        for ( j = 0; j < COL; j++ ) {
            while( fscanf( stdin, "%d", matrix[i][j] ) != 1 ) {
                fflush( stdin );
                continue;
            }
        }
    }
}

void Print_Matrix( int matrix[ FIL ][ COL ] ) {
    int i, j;
    for( i = 0; i < FIL; i++) {
        for( j = 0; j < COL; j++ ) {
            printf( "%5d", matrix[i][j] );
        }
        puts("\n");
    }
}

Upvotes: 0

Views: 288

Answers (1)

reuben
reuben

Reputation: 3370

In a file compiled as C (instead of C++), you must declare all variables at the top of the enclosing scope (i.e. in this case the surrounding curly braces). Here, you're calling a function (srand) before the 'int i, j' declaration statement.

Upvotes: 1

Related Questions