×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

That's how we play with arrays, practically !

 

arrays some examplesWelcome 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.

#include <stdio.h>
main()
{
     int n, k, sum, avg;
     printf("Enter the number of values. \n");
     scanf("%d", &n);
     int val[n]; //now array is created.
     printf("Now enter %d values one by one",n);
     
     //asking n values
     for(k=0; k

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.

 (A)                   (B)

    0   1   2             0   1   2                  
   -----------           -----------            
0 | 1 | 2 | 3 |       0 | 7 | 4 | 1 |   
   -----------           -----------           
1 | 4 | 5 | 6 |       1 | 8 | 5 | 2 |
   -----------           -----------           
2 | 7 | 8 | 9 |       2 | 9 | 9 | 3 |    
   -----------           -----------

Comparing from the above table, you can equate like :

B[0][0] = A[2][0];
B[0][1] = A[1][0];
B[0][2] = A[0][0];
B[1][0] = A[2][1];
B[1][1] = A[1][1];
B[1][2] = A[0][1];
B[2][0] = A[2][2];
B[2][1] = A[1][2];
B[2][2] = A[0][2];

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 :

#include <stdio.h>
main()
{
     int i,k,tab[10][10];
     for(i=0; i<10, i++)
     {
          for(k=0; k<10; k++)
          {
                tab[i][k] = i * k;
          }
     }
     
     for(i=0; i<10, i++)
     {
          for(k=0; k<10; k++)
          {
                printf("%d ",tab[i][k]);
          }
          printf("\n");
     }

     printf("Press Enter to exit...\n");
     getchar();
     return 0;
}

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.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo