[SOLVED] Same code works in main, but not when calling from a function in C

Issue

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;
}

Solution

"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?"

As called it should not work in either place.

printf("Length of the sequence: %d", longestSequence(arraySize, array[arraySize]));
//                                                                   ^^^^^^^^^^^
return 0;

Note first that the index passed: arraySize is one beyond the legal index for array. In C, indexing is zero based, and so it goes from 0arraySize - 1
More importantly though the 2nd argument of longestSequence should be a pointer to the array, not an indexed element of the array.

printf("Length of the sequence: %d", longestSequence(arraySize, array));
return 0;

Also, in general, to compare subsequent numbers in an array with size n, the range of comparisons should be limited to:

a[i] == a[i+1] //for i == 0 through i == n-1

Change:

 for(int i = 0; i<n; i++){
    //          ^^^
    if(array[i] == (array[i+1]-1)){//array out of bounds when i == n
    //                    ^^^

To

 for(int i = 0; i<n-1; i++){
    //          ^^^^^  
    if(array[i] == (array[i+1]-1)){//i will never reach n

EDIT:
One last thing addresses comment about replacing calls to scanf() with using the 2nd argument of main. First to do that the code must include the prototype of main: int main(int argc, char *argv[]);. With this prototype, the program as called from the command line can now include command line arguments, eg: if running from CMD prompt in Windows:

C:\dev> myProg.exe 3 1 2 3 4 5 6 7 8 9

Inside your program then arguments of argc and argv[]` are populated as follows:

argc == 11 //total number of arguments
argv[0] == "myProg.exe" //program name is alway in argv[0]
argv[1] == "3"
argv[2] == "1"
...
argv[10] == "9"

Which should translate to creating a 3×3 array populated with the 9 subsequent values.

So the first statements in your code could now be: (in psuedo code)

int n = atoi(argv[1]);//check value of n before using
int array[n][n];
int index = 2;
for(int i = 0; i<n ; i++)
    for(int j = 0; j<n ; j++)
        array[i][j] = atoi(argv[index]);
        index++;

Answered By – ryyker

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.