ITIC:Software and programming introduction - Exercises

From Juneday education
Jump to: navigation, search

Questions and exercises for software and programming introduction

The CPU

Describe what a CPU can do and what its role in a computer is.

Also, try to describe what the relation between the CPU and programming is.

The TOY computer

Try to explain step-by-step what values will go into the accumulator and the memory called sum in the small program example below if the user enters the following numbers 10, 20, 30, 0:

Main get
 ifzero End
 add sum
 store sum
 goto Main
End load sum
 print
 stop
sum 0

Try to also explain what will be printed to the screen as a result at the end of the program.

Hint: Make two columns on a piece of paper; Accumulator and sum. When the program starts the accumulator is empty and sum has the value 0 (zero). Walk through the instructions and fill in the values under each column as you execute the instructions on paper. Sometimes a column doesn't change between instructions (see below).

Remember:

  • get reads a value from the user into the accumulator (overwriting whatever was there before)
  • ifzero End jumps to the label End if the accumulator has the value 0 stored, and continues to the next instruction otherwise
  • add sum reads sum and adds that value to the value currently of the accumulator (sum is not changed, but the accumulator value increases)
  • store sum overwrites sum with the present value of the accumulator (the accumulator isn't changed)
  • goto Main jumps up to the label Main
  • load sum overwrites the value of the accumulator with the value in sum
  • print prints out whatever is in the accumulator to the screen
  • stop ends the execution (the program terminates)

Small Bash program

Paste the following into your terminal window (running Bash):

sum=0; read num; while ((num != 0)); do sum=$((sum + num)); read num; done; echo "Sum is: $sum";

Press Enter if you still are on the command line, so that it is executed.

Enter on one line each the numbers 1, 2, 3, 4, 5, 0 (to get the sum of those numbers). What was it?

Now, use arrow-up to get the previous command line again and press Enter. Enter all numbers between 1 and 10 on one line each. What is the sum?

Here's an interesting facts about adding the sum of numbers from 1 to 10. You can do this pretty quickly without using a computer by adding pairs of numbers that have a sum of 10:

 0 + 10 = 10
 1 +  9 = 10
 2 +  8 = 10
 3 +  7 = 10
 4 +  6 = 10

Sum = 50 + 5 = 55

(the number 5 was in the middle)

Try to add the numbers between 1 and 100 using the same technique. 50 is in the middle like the five. What result did you get?

You can use this command line to get the answer from Bash:

sum=0;for i in $(seq 1 100);do sum=$((sum + i));done;echo $sum

The part that reads for i in $(seq 1 100) starts a loop in which i gets all values from 1 to 100 in each iteration. In each loop, sum is given the value of the current value of sum plus i.

We will teach you more about Bash and Bash programming in a later module, but thought it would be fun to give you a preview of what you can do with Bash already. It's OK if you don't feel it was as fun as we thought.

Toy computer challenge

Here's a challenge exercise for the ambitious students. Go to Kernighan's online simulator of the Toy Computer and try to write the program described on the previous page (here).

We'll repeat the description of what your program should do here, so that you don't have to leave this page:

Write a Toy Computer program, that reads a number from the user. It should then calculate the sum of all numbers up to and including this number. Or, perhaps easier, calculate all numbers from this number down to zero.

If the user enters 5, the program should print the sum of 5 + 4 + 3 + 2 + 1 + 0 = 15. If the user enters 0, it should print 0. If the user enters 1, it should print 1 (1 + 0). If the user enters 2 it should print 3 (2 + 1 + 0). If the user enters 10 it should print 55 (10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0).

Use the program to calculate the sum of all numbers from 0 to 100 (or, to be more inline with the task description: from 100 down to 0). Did you get the same result as the Bash command line above?

Hints:

  • Declare two variables (memory locations) at the end of the program
    • i 0
    • sum 0
  • start by getting the number from the user
  • store this number in location i
  • start a labeled instruction called Loop and load the value from i to the accumulator
  • check if the accumulator is 0, if so jump to label End
  • otherwise (accumulator wasn't 0) add value from sum to the accumulator
  • store the new value in the accumulator to location sum
  • load i to the accumulator
  • subtract 1 from the accumulator
  • store the value of the accumulator to location i
  • jump back to Loop
  • declare a labeled instruction called End with the instruction to load the value of sum into the accumulator
  • print the value of the accumulator
  • stop the execution

Remember the syntax rules for the TOY language:

  • the program comes before the declaration and initialization of the locations (variables) i and sum
  • instructions start with a space. Remember that labels don't start with a space.
  • The variable (location) declarations at the end don't have a leading space either.

Expand using link to the right to see a suggested solution.

 get
 store i
Loop load i
 ifzero End
 add sum
 store sum
 load i
 sub 1
 store i
 goto Loop
End load sum
 print
 stop
i 0
sum 0

What happens if you enter a negative number? Try to answer before you try!


Expand using link to the right to see an explanation and a suggested solution for how to handle this problem.

The program will end up in an infinite loop, because if you start with a negative number and decrease that number in each loop iteration, it will never reach zero, which is the condition for stopping the loop.

Here's a version we wrote that handles negative input. If the user enters a negative number, then the value -1 is printed and the program stops. If no negative value is entered, the program continues as normal.

 get
 store i
 load 0
 sub i
 sub 1
 ifpos Error
Loop load i
 ifzero End
 add sum
 store sum
 load i
 sub 1
 store i
 goto Loop
Error
 load -1
 stop
End load sum
 print
 stop
i 0
sum 0

Our solution works like this:

Before the program starts the loop, we load zero into the accumulator and subtract the value stored in i (the value read from the user), and then we subtract 1 (one). If the result is positive (we use the ifpos instruction), then the value from the user must have been negative, so we jump past the loop to the label error which prints -1 and stops. Otherwise (the result wasn't positive), we enter the loop as usual.

The subtraction of one from the result is to be able to still handle 0. The ifpos test checks if the value of the accumulator is >= 0. Let's see how this works:

User enters 0. We store 0 in i. Then we load 0 to the accumulator. Then we subtract i: 0 - 0 = 0. Then we subtract 1: 0 - 1 = -1. Now, the ifpos test fails, as we want, since 0 is a legal value.

User enters -1. We store -1 in i. Then we load 0 to the accumulator. Then we subtract i: 0 - (-1) = 1. Now, the ifpos succeeds, so we jump to Error.

User enters -10. We store -10 in i. Then we load 0 to the accumulator. Then we subtract i: 0 - (-10) = 10. Now, the ifpos succeeds, so we jump to Error.

User enters 10. We store 10 in i. Then we load 0 to the accumulator. Then we subtract i: 0 - 10 = -10. Now, the ifpos test fails, as we want, since 0 is a legal value.

Links - Intro IT Software and programming intro exercises

Where to go next

The next page is Working_in_the_shell_-_Introduction_to_Bash.

« PreviousBook TOCNext »

Bonus questions (included from another page)

Note: The below is an inclusion of the page Chapter:Programming_introduction_-_Exercises

Exercises for the Programming introduction chapter

Introduction to Programming

In what format are program source code stored?

Expand using link to the right to see a hint.

Text file format.

Java and C are examples of compiled languages. What does the compiler do with the source code?

Expand using link to the right to see a hint.

It checks it for syntactic and other kinds of errors, and transforms it to a new format which can be run. In the case of C, the source code is compiled to a binary file which can be run by the target operating system. In the case of Java, the source code is compiled to so called byte code, which can be run by a Java Virtural Machine anywhere.

What is the minimal information you must provide the compiler with, in order for it to compile your program?

Expand using link to the right to see a hint.

Where the source code to compile is. This is typically given as an argument to the compiler on the command line. There are graphical programs for developing, compiling and even running programs too, and in such cases, the compiler knows which file(s) to compile because of the context when you tell the software to compile. We recommend against using such IDEs (integrated development environments) when learning to program. We think it is wise to wait with those until you know more about the basic process of editing, compiling and running a program.

Can you name a few programming languages?

Expand using link to the right to see a hint.

Perhaps you thought of:

  • C
  • C++
  • Java
  • C#
  • Ada
  • Haskell
  • Lisp
  • Pascal
  • Python
  • JavaScript or EcmaScript
  • PHP
  • Perl

Of course, the list could go on forever and ever.


End inclusion of Chapter:Programming_introduction_-_Exercises