jameskv Posted September 30, 2021 Share Posted September 30, 2021 Hi, I am looking to sort this array function in PHP. Question Given an array A = [10,20,30,40,50,60,70,80,90,100], write a function that receives two integers as parameters. The function returns the sum of elements in the array found between those two integers. For example, if we use 20 and 50 as parameters, the function should return 140. Can anybody help me to sort this out? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/ Share on other sites More sharing options...
Barand Posted September 30, 2021 Share Posted September 30, 2021 What have you tried so far, and which bit is giving you a problem? You may find useful... array_search() Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/#findComment-1590550 Share on other sites More sharing options...
Phi11W Posted September 30, 2021 Share Posted September 30, 2021 2 hours ago, jameskv said: Hi, I am looking to sort this array function in PHP. I assume you are not using "sort" in it's technical sense here. There is no sorting required in any part of this, which looks like a Homework Assignment, to me (so you don't get the answer straight away! 😀). 2 hours ago, jameskv said: Given an array A = [10,20,30,40,50,60,70,80,90,100], write a function that receives two integers as parameters. The function returns the sum of elements in the array found between those two integers. As an aside, I would say that this function should take three parameters, not two. 1. The array itself, 2. the lower limit of values you want to look for, 3. The upper limit of values you want to look for. Anyway ... Within the function, you'll need a local variable in which to store the calculated total. Remember to start this off at zero. Then, loop through the elements of the array and compare each element to the lower and upper limits (parameters). If the element value is greater than or equal to the lower limit and less than or equal to the upper limit, then add the element value to the total. After the loop, return the total. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/#findComment-1590551 Share on other sites More sharing options...
Barand Posted September 30, 2021 Share Posted September 30, 2021 Having declared that no sorting is required in any part, your solution depends on the array being sorted. eg echo func([20, 10, 15, 30, 40], 20, 30); // 50 instead of 75 Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/#findComment-1590552 Share on other sites More sharing options...
ginerjm Posted September 30, 2021 Share Posted September 30, 2021 Since the array in the example is sorted it seems to me the answer is 70, not 140. The question was 'between' 2 values, not including the 2 themselves. Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/#findComment-1590558 Share on other sites More sharing options...
Barand Posted September 30, 2021 Share Posted September 30, 2021 In my experience, between is usually inclusive EG mysql> select id, prod_name -> from products -> where id between 2 and 4; +----+-----------+ | id | prod_name | +----+-----------+ | 2 | Bbb | | 3 | Ccc | | 4 | Ddd | +----+-----------+ and the example given by the OP certainly implies that is the case here too. Quote Link to comment https://forums.phpfreaks.com/topic/313841-function-to-find-sum-of-an-array/#findComment-1590561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.