-5
#include<stdio.h>
int main(){
  int a[50],size,i,j=0,big,secondbig;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i]){
           big=a[i];
           j = i;
      }
  }

  secondbig=a[size-j-1];
  for(i=1;i<size;i++){
      if(secondbig <a[i] && j != i)
          secondbig =a[i];
  }

  printf("Second biggest: %d", secondbig);
  return 0;
}

i wrote above program using array. but i want to write without using array.

conditions are given a sequence of integers as input, terminated by a -1. i.e the input integers may be +ve, -ve or 0. A -1 in the input signals the end of the input. and -1 is not considered as part of the input.

Nidhi Murthy
  • 17
  • 2
  • 2
  • 4
  • 6
    Do we have a "close as homework with no effort" flag? – Martin Beckett Aug 25 '14 at 17:16
  • 2
    since you seem to use an array as storage for your input, what do you mean by "without using array"? how do you want the program to function? – NathanFrasier Aug 25 '14 at 17:16
  • @Martin Beckett A good place to discuss"close as homework with no effort flag" is http://meta.stackoverflow.com – chux - Reinstate Monica Aug 25 '14 at 17:49
  • @MartinBeckett There's already a post with that subject: [meta.stackexchage: "How do I ask and answer homework questions](http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions) – Barranka Aug 25 '14 at 17:54
  • 1
    @MartinBeckett: "Too broad" is a pretty good proxy for "your question sucks." And I'm sure you can make up a dumb rationale for why it actually fits if you really wanted. – tmyklebu Aug 25 '14 at 18:14
  • @tmyklebu - I flagged it "too broad" but really "too narrow" would be more accurate ! – Martin Beckett Aug 25 '14 at 21:13

1 Answers1

6
foreach value v
  if (v > biggest)
    second = biggest
    biggest = v
  else if (v > second)
    second = v

Note: you do have to decide what to do if there are repeated elements. In other words, suppose the numbers are 5,5,3,1. Then the largest is obviously 5, but is the second largest 5, or 3? That's for you to decide and fix the algorithm accordingly.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135