[Solved] I have to write a program which accepts n number of integers and display the degree of the array [closed]


This is kind of naughty:

int main(int argc, char**argv)
{
    argv++;
    int n = argc - 1;
    int arr[n];
    for(int i = 0; i < n; i++)
    {
        arr[i] = atoi(argv[i + 1]);
    }
    printf("%d\n", degreeOfArray(arr, n));
}
  • Argc is 4.
  • You increment argv (which is naughty).
  • n is 3.
  • arr has values [0] through [2]
  • You loop from 0..2 (which is fine)
  • You set arr[i] just fine, but the call atoi(argv[i+1]) is a problem.

I is going to reach 2. so you’re hitting argv[3]. Which would be fine if you hadn’t incremented argv.

So either get rid of the i+1 there or don’t increment argv.

2

solved I have to write a program which accepts n number of integers and display the degree of the array [closed]