static keyword not working in C (Visual Studio)

Question - N numbers are entered by the user (N is also given by the user). Store the numbers in an array. Write a C function which deletes all the second largest elements and rearranges the array. The program should appropriately print the new array and the value of N. For e.g if N=7 and the elements are 7 11 13 11 8 7 4 the output should be N=5 and the array is 7 13 8 7 4

My Code -

#include<stdio.h>

int secondLargest(int arr[], int n);
int indexOfSecondLargest(int arr[], int n, int max2);
void deleteElement(int arr[], int n, int index);
void deleteSecondLargest(int arr[], int n);
void printArray(int arr[], int n);

int main() 
{
    static int n;
    printf("Enter value of n \n");
    scanf("%d", &n);

    int arr[100];

    printf("Enter n numbers \n");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }    

    deleteSecondLargest(arr, n);

    printf("New value of n = %d and the array is \n", n);
    
    printArray(arr, n);
    
    return 0;
}

int secondLargest(int arr[], int n)
{
    int max1 = arr[0], max2 = arr[0];
    for (int i = 0; i < n; i++)
    {
        if (arr[i] > max1)
        {
            max2 = max1;
            max1 = arr[i];            
        }
        if (arr[i] > max2 && arr[i] < max1)
        {
            max2 = arr[i];
        }       
    }
    
    return max2;
}

int indexOfSecondLargest(int arr[], int n, int max2)
{
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == max2)
        {
            return i;
        }        
    }
    
    return -1;
}

void deleteElement(int arr[], int n, int index)
{
    for (int i = index; i < n - 1; i++)
    {
        arr[i] = arr[i+1];
    }

    n = n - 1;
}

void deleteSecondLargest(int arr[], int n)
{
    int max2 = secondLargest(arr, n);

    int index = indexOfSecondLargest(arr, n, max2);
    while (index != -1)
    {
        deleteElement(arr, n, index);
        index = indexOfSecondLargest(arr, n, max2);
    }
}

void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }    
}

My Output -

Enter value of n 
7
Enter n numbers 
7 11 13 11 8 7 4
New value of n = 7 and the array is 
7 13 8 7 4 4 4

Even though I am using static keyword before int n, the value of n is not getting updated to 5 in main() function. I even tried putting int n as a global variable but still it doesn't work. Can someone help me ??

Upvotes: 0

Views: 58

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222724

Adding static to int n; says “This n persists through all of program execution.” it does not say “This n is the only n in the whole program.” Where a parameter n is declared, as in void deleteElement(int arr[], int n, int index), that creates an n different from the n in main.

However, even if no parameter is declared with the name n, an n declared inside main is not visible in any other function. To use the same n throughout the program, you need to declare int n; or static int n; outside of any function, anywhere before the first function that uses n, and you need to not declare any other n.

That would likely solve this problem for you. However, it is bad practice to use external identifiers for objects. Instead, you should use int n; in main and modify deleteElement so that it provides main with the updated value. It could do this by returning an int instead of void, or you could modify deleteElement to take a pointer to an int instead of taking an int, by making the parameter int *n. Then you would have to use it inside the function as *n instead of n.

Upvotes: 2

Related Questions