-1

The below program is to find the first and second largest element in an array

INPUT: 5 10 11 23

  //To find max and second max element in an array

        #include < stdio.h >
        #include < stdlib.h >

  int main(int argc, char * argv[]) {
    int l, arr[100], n;

    int first = arr[0];
    int second = arr[0];
    int i;

    for (i = 0; i <= n; i++) {
      arr[i] = atoi(argv[i]);
    }
    for (i = 0; i < 10; i++) {
      if (first < arr[i]) {
        second = first;
        first = arr[i];
      } else if (second < arr[i]) {
        second = arr[i];
      }

    }

    printf("First = %d\n", first);
    printf("Second = %d\n", second);

    return 0;
  }

OUTPUT:

First = 242475432
Second = 242425128

There are garbage value but not the sorted value

Sethu pathy
  • 1
  • 1
  • 1

3 Answers3

3

The behaviour of your code is undefined. You are reading arr[0] before setting it to a value.

Also n is never initialised; more undefined behaviour. Why not set it to argc, and make sure you run i to < argc? Also, don't forget that argv[1] is the first argument on the command line: consider running i from 1.

A good line by line debugger would help identify these problems.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

To load the array from the command line, I suggest:

int main(int argc, char * argv[]) {
    int l, arr[100];

    for (int i = 1; i < argc; i++) {
      arr[i-1] = atoi(argv[i]);
    }
    int n= argc - 1;
    int first = arr[0];
    int second = arr[0];
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

This is a corrected, but still simple, code that solves your problem:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h> // For INT_MIN

int main(int argc, char ** argv) {
  int l, i;

  int first = INT_MIN;  // It will set first and second at the minimum
  int second = INT_MIN; // value possible for an int.

  for (i = 1; i < argc; i++) { // there is no need for arr
                               // just use argc for iterations.
                               // The element 0 of argv is the 
                               // binary name, thus i starts from 1

    l = atoi(argv[i]); // as soon you are still learning 
                       // atoi is a good solution, but you should
                       // consider something more robust for your 
                       // conversion. Consider that if you give as
                       // input to your code
                       //   bin.exe hello -1 -2
                       // the result is:
                       //   First = 0, Second = -1   
                       // since atoi("hello") is 0.

    // in this way you are not forced to have less than 
    // 100 elements in input.
    if (l > first) { // shifting the first
      second = first;
      first = l;
    } else if (l > second) // saving only in the second
      second = l;
  }

  printf("First = %d\nSecond = %d\n", first, second);
  return 0;
}

You may consider to use something like strtol, that sets an error number in case of wrong conversions. Take a look at this answer.

Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34