File Management

Miscellaneous

Newbie ProgrammerArrays Some cool examples

Newbie Programmer

full book coming soon

by Shubham Ramdeo

That's how we play with arrays, practically !

Welcome to the Newbie Programmer Series. In the last two parts, we have discussed the concepts behind the arrays and how to use them. In this part we will discuss some examples to learn more about arrays. So if you are new here, please check out all the previous parts (click here) so that you can easily understand what is happening below.

Average with array formed during run time

Calculating average of some numbers is one of the easiest examples. Why I put it here in a different way is because here I have used variable array !!! WHAT ? But we have already studied in the last part that variable arrays cannot be made. Then how is it made possible ? Actually it is a trick. It is very true that once the array is created of a certain size, it cannot be change. But before creating the array, we can change the size. So in this example here, user need to set the size of the array. Then the array is created which remains fix. In the following code, program asks for total number of values. Say user enters 5. Then the program asks 5 values to be entered and then prints their average.

Study it a little by yourself. The above example is for integers, you can set it for float too. The main point to learn is how we have tricked it with array looking like variable size. If there is any trouble, please comment below.

Let's Enjoy the output :

Enter the Number of values.
4
Now enter 4 values one by one.
10
20
30
40
The average of this data is 25
Press Enter to exit...

Rotating Arrays

There is no need to create any program for this. We will just look at the concept. Suppose we have a table 'A' and we want to rotate it to 'B' :

(A) (B)
1 2 3 clockwise 7 4 1
4 5 6 rotated to 8 5 2
7 8 9 9 6 3

Here each element is moved to a different position. In a simple way, we can do it by creating a new array and then reassigning the positions. Let's say "A[row][column]" is the original array and "B[row][column]" will become the rotated one.

After this, the array "B" is the rotated array "A". This rotation is also called as Transpose. Two invert a table, or rotating a table anticlockwise, instead of doing some thing again, we should make a function for a "single rotation" (as above) and then, for example, for anticlockwise rotation, rotate it clockwise three times ! Well it is a very confusing concept. But later we will learn using pointers which can make everything easy.

Printing Multiplication Tables

While learning loops, (click here) we have already created a multiplication table program. Here let's learn a little new thing. In that example, we have printed tables like "2 x 1 = 2... 2 x 2 = 4...". Using loops directly without storing anything in a form of table.

Here, we will use a two-dimensional array and will print the table as it is.

The point to learn here is that how the table gets printed. So keep your eyes on the table printing code :

Now you would have already understood how the table was formed. Let's talk about the printing function. The concept is that the INNER loop starts printing with ROW=0, Column=0 to Column=9. When the end of a ROW is reached (column=9), the inner loops ends. And a "new line" character is printed due to the body of the second loop. The Row increases from ROW=0 to ROW=1. Same pattern is followed. All column of a row is printed then comes a new line for the second row. When all the rows are also printed, the loops ends.

I think this is enough. We have discussed some practical ways of using arrays and here we can complete our introduction of arrays. From the next part we will start discussing "structures". My favorite as we can do programming like treating real physical objects. So please stay connected :)

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.