Challenges
Last updated
Last updated
Create an array called powersOfTwo
that stores 16 integers. Use a for loop to initialize the values of the array with the powers of 2
as shown in the example below.
HINT - Powers of Two
To calculate the 5th power of two, you can multiple the 4th power of two with
2
.
Print out the resulting list. An example is shown here:
Can you explain the last negative number if you increase the array to 32
numbers?
Create an array of 100 random numbers. Now ask the user for a number and print all the indexes of where the number in the array can be found. Feel free to output the resulting array too.
Generate 1000 random numbers between 0
and 20
. Do not store the numbers themselves that were generated. Instead create a frequency table to store the number of times each value was generated.
For example:
Index
Counter
0
7
1
11
...
...
Meaning value 0
was generated 7 times out of 1000 and value 1
was generated 11 times out of a 1000.
Print the full frequency table after generating the 1000 numbers.
What did you expect as outcome ? Try increasing the number of values you generate.
Generate an array of 10 random numbers (with a value below 100 for example). Sort the randomly generated numbers of integers in ascending order. Search the Internet for "selection sort". You will find enough information and examples. Do no copy / paste code, try to implement it for yourself.
Print the list of numbers before and after the sorting.
Example:
Often when reordering elements in an array (for example when sorting) two values need to be swapped places.
Take the following piece of code and write some code below the first for
loop to swap the first and the last elements of the values
array. In other words the arrays should contain the following values after swapping the elements: 3, 2, 1
.
Create a matrix of random integer values. Print it to the terminal together with the sum of each row and each columns in the format shown below.