Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
the following code is written in a aspx- site.

...
...
long sum = 0;
for ( int i = 0; i < 888; i++)
{
// GetIntFromSQL returns an int from an SQL-statement-resultset with one
// row, one column
sum += GetIntFromSQL("EXEC dbo.ObscureProcedure @i = "+ i.ToString());
};
Response.Write(sum.ToString());
...
...


Proceduren i Microsoft SQL Server ser ut enligt:
CREATE PROCEDURE dbo.ObscureProcedure
@i int
AS
BEGIN
SELECT
CASE
WHEN @i < 260 THEN
COUNT(*) * @i
ELSE
COUNT(*) * 260
END AS ObscureCount
FROM
VacationGadget
WHERE
@i < 714
END

if you drive it in i Microsoft SQL Server Management Studio the following procedure ...

EXEC dbo.ObscureProcedure @i = 528

... the you get :
ObscureCount
------------
3138460

(1 row(s) affected)

which sum sholud the C#-code at the top write out?

i need help solving this? i don't know how to count it ? I'm new at this, please help

What I have tried:

i have tried to find info about how i should calculate this problem but without luck. i want to understand the formula to solve this. what should i multiple with what and add and so on.
Posted
Updated 26-Sep-17 8:42am
Comments
Richard MacCutchan 26-Sep-17 10:57am    
Ask the person who wrote the code.

Run it, and find out: it's going to depend on your data, which we do not have any access to.
 
Share this answer
 
Time for some mathematical magic! :)

Breaking the stored procedure down:
  • If @i is greater than or equal to 714, it returns nothing;
  • If @i is greater than or equal to 260, it returns 260 × C, where C is the number of records in the VacationGadget table;
  • If @i is less than 260, it returns @i × C;

With your sample run, @i is 528, and the result is 3,138,460. Therefore, since @i is greater than 260, we can deduce that C = 3,138,460 ÷ 260 = 12,071.

Now to the loop.
  • We can discount the first iteration, since 0 × anything = 0.
  • We can probably discount anything from 714 to 888 - assuming your GetIntFromSQL method does the right thing when the stored procedure doesn't return any results.
  • For 1 to 259, the result from SQL will be i × C.
    Therefore, the total sum for that part of the loop will be:
    (the sum of integers from 1 to 259) × C.
  • For 260 to 713, the result from SQL will be 260 × C.
    Therefore, the total sum for that part of the loop will be (714 - 260) × 260 × C;

Plumbing some numbers in:
  • (the sum of integers from 1 to 259)
    = 259 × 260 ÷ 2 (Why?[^])
    = 33,670;
  • (714 - 260) × 260
    = 454 × 260
    = 118,040;

The final result will be:
33,670 × C + 118,040 × C
= 151,710 × C
= 151,710 × 12,071
= 1,831,291,410
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 26-Sep-17 23:15pm    
5, neat

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900