Write a program which reads integers from the user and place

Write a program which reads integers from the user and places them in an array until te user enters the string \"S\". This program should then print the sum of the first half of the array minus the sum of the second half of the array. If the array has an odd number of entries, ignore the middle entry.

Solution

Since no specific programming language was mentioned, giving the solution in python

Code:

array = [];
inp = None;

while True:
   inp = raw_input();
   if( inp == \"S\" ):
       break;
   else:
       try:
           array.append( int(inp) );
       except:
           continue;

firstHalfSum = 0;
secondHalfSum = 0;

for i in range( len(array)/2 ):
   firstHalfSum = firstHalfSum + array[i];

for i in range( len(array)/2 ):
   secondHalfSum = secondHalfSum + array[ len(array)-1- i];

print firstHalfSum-secondHalfSum;

Write a program which reads integers from the user and places them in an array until te user enters the string \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site