I am having problem getting the final result here, it keeps on looping.

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
int main()

{


        int i,  t, k, max, min, sumof;
        printf("Enter number of integer elements in array: ");
       scanf("%d",&t);


       int a[t];

      printf("Enter list of array: \n");
      for ( i = 0; i < t ; i++)
      {
      scanf("%d",&a[i]);
      fflush(stdin);
         }


         // search for secound first and second last number in array 
         // t-1 = second last and t-2 = secound first#
         k=1;
          for (i = 0; i < t; i++)

          {
             if (k==(t-(t-2)))
            {
                printf("\n The second first number is %d",a[i]);
            }
            k++;
          }


         k=1;
          for (i = 0; i < t; i++)

          { 

            if (k==(t-1))
            {
                printf("\nthe second last number is %d",a[i]);
            }
             k++;
          }

      //for maximum
      max=a[0];
      for (i = 0; i < t; i++)
      {

             if (a[i]> max )
                         max = a[i];
      }

          printf("\n  maximum is %d \n", max);


      //for minimum
      min=a[0];
      for (i = 0; i < t; i++)
      {

             if (a[i]< min )
                         min = a[i];
      }

      printf(" \n minimum is %d\n ", min);


      sumof = 0;
    for (i = 0; i < t; i++)
    {

            sumof = sumof + a[i];
        }   
            printf (" \n sum of array  is %d\n ", sumof);



      return 0;
}

// t-1 = second last and t-2 = secound first

What?? The second last is t-2, the second first is 1. Why do you need loops to find the second last and second first numbers?

Sometimes ... the obvious ... is alarmingly simple ... when you 'see' it :)

C arrays start at index 0, so next in, from front, has index 1,

and ... the last index is size-1 (if size > 0 ),

so next to last index is size-2 (if size > 1)

Edited 2 Years Ago by David W

This question has already been answered. Start a new discussion instead.