Brute Force Program Error on for loop


// Can someone run this program on C++ compiler: I'm getting an error on this
// for loop on line 101 and I can't figure out why?

//Error Message: Brute_Force.cpp:101:18: error: cannot convert 'int (*)[c]'
//to 'int (*)[5]' for argument '1' to 'int search(int (*)[5], int, int)'
//search(mat, r, 25);

//for(loop=1;loop<=100000;loop++)

//{

//search(mat, r, 25);

//}




// Program Title: time complexity bruteforce


#include <stdio.h>

#include <limits.h>

#include <time.h>

#include <sys/time.h>

//BruteForce

int search(int mat[5][5], int n, int k)

{

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

if(mat[i][j]==k)

{

// printf("localMinindex=(%d,%d)",i,j);

break;

}

}

}

}

int main() {

// your code goes here

int i,j,r,c;

printf("Enter number of rows\n");

scanf("%d",&r);

printf("Enter number of column\n");

scanf("%d",&c);

int mat[r][c];

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

scanf("%d",&mat[i][j]);

}

}

int locmin=INT_MAX;

struct timeval timer_usec;

long long int timestamp_usec; // timestamp in microsecond

if (!gettimeofday(&timer_usec, NULL)) {

timestamp_usec = ((long long int) timer_usec.tv_sec) * 1000000ll +

(long long int) timer_usec.tv_usec;

}

else {

timestamp_usec = -1;

}

printf("%lld microseconds since epoch\n", timestamp_usec);

int loop;



if (!gettimeofday(&timer_usec, NULL)) {

timestamp_usec = ((long long int) timer_usec.tv_sec) * 1000000ll +

(long long int) timer_usec.tv_usec;

}

else {

timestamp_usec = -1;

}

printf("%lld microseconds since epoch\n", timestamp_usec);

return 0;

}

//Binary search :-code

#include <stdio.h>

#include <limits.h>

#include <time.h>

#include <sys/time.h>

//Binary Search

int bsearch(int mat[5][5], int n, int k)

{

int i = 0, j = n-1; //set indexes for top right element

while ( i < n && j >= 0 )

{

if ( mat[i][j] == k )

{

// printf("n Found at %d, %d", i, j);

return 1;

}

if ( mat[i][j] > k )

j--;

else // if mat[i][j] < k

i++;

}

printf("n Element not found");

return 0; // if ( i==n || j== -1 )

}
Last edited on
int i,j,r,c;

printf("Enter number of rows\n");

scanf("%d",&r);

printf("Enter number of column\n");

scanf("%d",&c);

int mat[r][c];

I don't think you can do this in c++. The array dimensions are not constant at compile time.
Topic archived. No new replies allowed.