This sequence finds the Minimim and Maximum from an array, using For Each , If and Assign activities

I am stuck on a exercise where I am using For Each and IF condition but I can’t understand the execution of the program . Below is the program/code/ptocedure :slight_smile:

Find the smallest and biggest numbers in an Array

Use For Each, Assign and If Statements to find the minimum and maximum number in an array of Int32 elements and print it.

Note : You can instantiate the Array with {7, 5, 2, 4, 3, 9} or with your own values, as long as they are integers.

Practice 1 Solution

  1. Start the project as a sequence and define a variable of type Array of Int32 . You can name it “initArray”
  2. Define 2 Int32 variables : “min” and “max”.
  3. Use an ‘Assign’ activity to populate the Array variable with a some Int32 values, let’s say {7, 5, 2, 4, 3, 9}.
  4. Use 2 ‘Assign’ activities to assign the first element in the array to both “min” and “max”.
  5. Drag a ‘For Each’ activity to go through each item in the Array. Make sure you change the ‘Type Argument’ of the ‘For Each’ in the Properties panel to Int32 , and create an Int32 variable (“Index”) to place in the ‘Output’ field
  6. Inside the body of the ‘For Each’ activity, drag 2 If activities:
  7. The condition for the first If is “array(index) < min” to compare the values in the string and identify the smallest. In the ‘Then’ block add an ‘Assign’ activity to store the value in the “min” variable.
  8. The condition for the second If is “array(index) > max” to compare the values in the string and identify the biggest. In the ‘Then’ block add an ‘Assign’ activity to store the value in the “max” variable.
  9. At the end of the workflow (outside the Body of ‘For Each’), add 2 ‘Log Message’ activities in order to display “min” and “max” variables.

Regards,
Nikesh.

1 Like

minMax.xaml (9.7 KB)

Try this…Debug it one by one for each iteration…! @Nikesh_Konda

Thanks!

1 Like

can you please resend the file cause the current one doesn’t work.

I have the same exercise to complete as part of my curriculum and speedily doing so would be a pleasure.

1 Like

Thank you 100 times.
After seeing your answer, I understood the exercise request as well as the answer.
I thought there could be a more simple approach, but the answer is just to assume that the first number is Min and Max and then update that by comparing it to the array.

1 Like