question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked karenpayneoregon commented

How to stop a for loop at a specific cycle?

Hello,



I am trying to stop a loop at a specific cycle.

 using System.CodeDom.Compiler;
 using System.Collections.Generic;
 using System.Collections;
 using System.ComponentModel;
 using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Runtime.Serialization;
 using System.Text.RegularExpressions;
 using System.Text;
 using System;
    
 class Solution {
    
     // Complete the maxSubsetSum function below.
     static int maxSubsetSum(int[] arr) {
            
          int size = arr.Length;
         int max_so_far = int.MinValue,
          max_ending_here = 0; 
      
         for (int i = 0; i < size; i++) 
         {
                 
             if (i % 2 == 0)
             {
                
             max_ending_here = max_ending_here + arr[i];
               
      
             if (max_so_far < max_ending_here)
                 max_so_far = max_ending_here;
                    
             // cant tell why the loop not stopping at the largest sum
                 
                  Console.WriteLine("\t3rd loop cycle!:  " +max_ending_here );
             }
                
         }
            
            
         return max_so_far;
    
     }
    
     static void Main(string[] args) {
         TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
    
         int n = Convert.ToInt32(Console.ReadLine());
    
         int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
         ;
         int res = maxSubsetSum(arr);
    
         textWriter.WriteLine(res);
    
         textWriter.Flush();
         textWriter.Close();
     }
 }

The output return :

Input (stdin)

 5

 3 7 4 6 5

Your Output (stdout)

 12

Expected Output

 13

Debug output

  3rd loop cycle!:  3

  3rd loop cycle!:  7

  3rd loop cycle!:  12


 5

 3 5 -7 8 10

Your Output (stdout)

 6

Expected Output

 15

Debug output

  3rd loop cycle!:  3

  3rd loop cycle!:  -4

  3rd loop cycle!:  6


 5

 2 1 5 8 4

Only this output return the right answer.

Your Output (stdout)

 11

Expected Output

 11

Debug output

  3rd loop cycle!:  2

  3rd loop cycle!:  7

  3rd loop cycle!:  11

Thank you


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Use a break when the condition is hit e.g.

 var breakOn = 9;
    
 for (int index = 0; index < 22; index++)
 {
        
     if (index == breakOn)
     {
         Console.WriteLine($"Breaking on {breakOn}");
         break;
     }
     Console.WriteLine($"{index}");
 }

Another example

 int[] breakItems = {11, 15, 19};
    
 for (int index = 0; index < 22; index++)
 {
        
     if (breakItems.Contains(index))
     {
         Console.WriteLine($"Breaking on {index}");
         break;
     }
     Console.WriteLine($"{index}");
 }

Or

 for (int index = 0; index < 22; index++)
 {
    
     switch (index)
     {
         case 11:
             Console.WriteLine("\t11");
             break;
             case 15:
                 Console.WriteLine("\t15");
                 break;
             case 19:
                 Console.WriteLine("\t19");
                 break;
             default:
                 Console.WriteLine(index);
                 break;
     }
    
 }


 bool breakOut = false;
 for (int index = 0; index < 22; index++)
 {
    
     switch (index)
     {
         case 11:
             Console.WriteLine("\t11");
             breakOut = true;
             break;
         case 15:
             Console.WriteLine("\t15");
             breakOut = true;
             break;
         case 19:
             Console.WriteLine("\t19");
             breakOut = true;
             break;
         default:
             Console.WriteLine(index);
             break;
     }
    
     if (breakOut)
     {
         break;
     }
    
 }

If this was in a method and wanted to return a value we use return. Also look at continue

Or multiple cases

 private static bool BreakOutWithoutExpression(in int index)
 {
     var result = false;
        
     switch (index)
     {
         case 11:
         case 15:
         case 19:
             result = true;
             break;
     }
    
     return result;
 }

See also
https://kodify.net/csharp/loop/exit-loop/





· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried to break after that part but i cant figure out how make it stop at 13, 15 and 11.

if (max_so_far < max_ending_here)
max_so_far = max_ending_here;

       ty
0 Votes 0 ·

I've edited my reply. Keep in mind my samples are options that you would need to adapt to your logic.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon commented

If using the current release or C#8 then there are switch expressions

 private static bool BreakOut(int index) => index switch {11 => true, 15 => true, 19 => true, _ => false};

.

 for (int index = 0; index < 22; index++)
 {
     if (!BreakOut(index)) continue;
     Console.WriteLine(index);
     break;
 }


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Those are interesting solutions if i can only find a way to apply them.

By defaut that part :

max_ending_here = max_ending_here + arr[i];

          if (max_so_far < max_ending_here)
              max_so_far = max_ending_here;

should return the max value.

3 7 4 6 5

Once it sum 3, 4, 5
it keeps summing : 7, 6 = 13
4, 5 = 9

it return 9 the last answer when it should stop automatically at 13.

strange

0 Votes 0 ·

Put a breakpoint on

if (max_so_far < max_ending_here)

Edit the breakpoint and add a condition (here it's == but can be any valid assertion), when the condition is hit then the debugger breaks and if the condition is never met the debugger will not break.

81910-f1.png


0 Votes 0 ·
f1.png (27.8 KiB)