Saturday, August 18, 2018

SUM OF ARRAY

  • Problem Description

    Jane is fond of arrays in C program. But she finds it difficult to handle pointers with arrays. Help her to write a C Program to compute sum of the array elements using pointer. 

    Input : The first line contains the no of test cases, 0<T<=1000, 

    The Second line contains the no of array elements n of test case 1, 
    The third line contains the n integers separated by a space. 
    For T test cases the second and third lines are successively entered. 

    Output: The sum of array elements of each test case , 

    If T <0 or T>1000 print " INVALID INPUT"
  • CODING ARENA::
  • #include <stdio.h>
    int main()
    {
      int a,i,b,sum=0,j;
      scanf("%d",&a);
      if(a<=1000)
      {
        for(i=0;i<a;i++)
        {
          scanf("%d",&b);
          int arr[b];
          for(j=0;j<b;j++)
          {
            scanf("%d",&arr[j]);
            sum=sum+arr[j];
          }
          printf("\n%d",sum);
          sum=0;
        }
      }
      else
        printf("INVALID INPUT");
          

    return 0;
    }
  • Test Case 1

    Input (stdin)
    2
    
    5
    
    2 4 6 8 2
    
    3
    
    10 20 30
    
    
    Expected Output
    22
    
    60
  • Test Case 2

    Input (stdin)
    1002
    
    5
    
    1 2 3 4 5
    
    
    Expected Output
    INVALID INPUT

No comments:

Post a Comment