Search code examples
algorithmgenetic-algorithmmit-scratch

Converting Scratch to Algorithm


First time I am learning algorithms and trying to figure out with stratch. I am following tutorials on Stratch wiki. How can I convert this to algorithm?( with flow chart or normal steps). Especially the loop.( I uploaded as picture) Please click here to see picture

I Started:

Step:1 Start
Step2: İnt: delete all of numbers, iterator, amount,sum
Step3: How many numbers you want?
Step4:initialize sum=0,amount=0,iterator=1
Step5: Enter the elements values
Step6: found the sum by using loop in array and update sum value in which loop must be continue till (no of elements-1 ) times
Step7:avg=sum/no of elements
Step8: Print the values average

I don't think It's true. I mean I feel there are errors? Thank you for time.


Solution

  • Scratch

    Here is the algorithm in variant 2 (see Java algorithm below) in Scratch. The output should be identical. enter image description here

    Java

    Here is the algorithm in Java where I did comment the steps which should give you a step-by-step guide on how to do it in Scratch as well.

    I have also implemented two variants of the algorithm to show you some considerations that a programmer often has to think of when implementing an algorithm which mainly is time (= time required for the algorithm to complete) and space (= memory used on your computer).

    Please note: the following algorithms do not handle errors. E.g. if a user would enter a instead of a number the program would crash. It is easy to adjust the program to handle this but for simplicity I did not do that.

    Variant 1: Storing all elements in array numbers

    This variant stores all numbers in an array numbers and calculates the sum at the end using those numbers which is slower than variant 2 as the algorithm goes over all the numbers twice. The upside is that you will preserve all the numbers the user entered and you could use that later on if you need to but you will need storage to store those values.

    public static void yourAlgorithm() {
            // needed in Java to get input from user
            var sc = new Scanner(System.in);
            // print to screen (equivalent to "say"/ "ask")
            System.out.print("How many numbers do you want? ");
            // get amount of numbers as answer from user
            var amount = sc.nextInt();
            // create array to store all elements
            var numbers = new int[amount];
            // set iterator to 1
            int iterator = 1;
            // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
            // equivalent to "repeat amount" except that retries are possible if no number was entered
            while (iterator <= amount) {
                // ask for a number
                System.out.printf("%d. number: ", iterator);
                // insert the number at position iterator - 1 in the array
                numbers[iterator - 1] = sc.nextInt();
                // increase iterator by one
                iterator++;
            }
            // calulate the sum after all the numbers have been entered by the user
            int sum = 0;
            // go over all numbers again! (this is why it is slower) and calculate the sum
            for (int i = 0; i < amount; i++) {
                sum += numbers[i];
            }
            // print average to screen
            System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);
        }
    

    Variant 2: Calculating sum when entering new number

    This algorithm does not store the numbers the user enters but immediately uses the input to calculate the sum, hence it is faster as only one loop is required and it needs less memory as the numbers do not need to be stored. This would be the best solution (fastest, least space/ memory needed) in case you do not need all the numbers the user entered later on.

    // needed in Java to get input from user
            var sc = new Scanner(System.in);
            // print to screen (equivalent to "say"/ "ask")
            System.out.print("How many numbers do you want? ");
            // get amount of numbers as answer from user
            var amount = sc.nextInt();
            // set iterator to 1
            int iterator = 1;
            int sum = 0;
            // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
            // equivalent to "repeat amount" except that retries are possible if no number was entered (e.g. character was entered instead)
            while (iterator <= amount) {
                // ask for a number
                System.out.printf("%d. number: ", iterator);
                // get number from user
                var newNumber = sc.nextInt();
                // add the new number to the sum
                sum += newNumber;
                // increase iterator by one
                iterator++;
            }
            // print average to screen
            System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);
    

    Variant 3: Combining both approaches

    You could also combine both approaches, i. e. calculating the sum within the first loop and additionally storing the values in a numbers array so you could use that later on if you need to.

    Expected output

    enter image description here