Okay so I'm a beginner programmer so any tips on any part of the code are greatly appreciated,
but the main question is why does the code in function int longestSequence(int n,int array[n]);work when placed in main, but not when called from the function?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int longestSequence(int n,int array[n]);
int main()
{
int n;
scanf("%d", &n);
int mat[n][n];
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
scanf("%d", &mat[i][j]);
}
}
int arraySize = n*n;
int array[arraySize];
int arrayIndex = 0;
for(int i=0; i<n; i++){
if(i%2 == 0){
for(int j = 0; j<n; j++){
array[arrayIndex++] = mat[i][j];
}
}else{
for(int j = n-1; j>=0; j--){
array[arrayIndex++] = mat[i][j];
}
}
}
/// Here's the same code that works when in main
// int numOfSequental = 0;
// int maxNumOfSequental = INT_MIN;
// for(int i = 0; i<n; i++){
// if(niz[i] == (niz[i+1]-1)){
// numOfSequental++;
// if(numOfSequental>maxNumOfSequental){
// maxNumOfSequental = numOfSequental;
// }
// continue;
// }
// numOfSequental = 0;
// }
//calling the function in printf
printf("Length of the sequence: %d", longestSequence(arraySize, array[arraySize]));
return 0;
}
int longestSequence(int n,int array[n])
{
int numOfSequental = 0;
int maxNumOfSequental = INT_MIN;
for(int i = 0; i<n; i++){
if(array[i] == (array[i+1]-1)){
numOfSequental++;
if(numOfSequental>maxNumOfSequental){
maxNumOfSequental = numOfSequental;
}
continue;
}
numOfSequental = 0;
}
return maxNumOfSequental+1;
}