Saturday, 9 March 2019

Structures in C (Part 1)

As you are aware that arrays store elements of similar type. Contrary to this, structures are objects that contain more than one item.
Definition:
  • A structure contains a number of data types grouped together.
  • These data types may or may not be of the same type.
In this article, you will learn following fundamental aspects of structures:
(a) declaration of a structure
(b) accessing of structure elements
(c) Initializing values to structure element(s)
(d) how structure elements are stored in memory

(a) declaration of a structure
The general form of a structure declaration statement is given below:
struct structure_name{
       datatype structure_element 1 ;
       datatype structure_element 2 ;
       datatype structure_element 3 ;
        ......
        ......

   } structure_variable(s);

Points to remember about above declaration:
  • The first line contains the struct keyword, then the optional structure_name:struct structure_name {
  • The structure_name can be used to create a copy of the structure. An opening brace follows the structure_name (or the struct keyword, if the structure_name is not used). This brace signals to the compiler that the next lines are member definitions. Each member definition consists of a variable type and a name. The members can be any valid variable type, including arrays, structures, and unions.
  • Following the last member name is a closing brace and the optional structure_variable, as follows:} structure_ variable(s) ;
  • The closing brace in the structure type declaration must be followed by a semicolon.
  • A structure type declaration does not reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure.

When using the structure_ variable and the structure_name, you can choose any of the following:
  • If a structure_ variable is not specified and a structure _name is specified, the structure is being defined but not declared.
  • If a structure_ variable is specified and a structure _name is not specified, the structure is being declared but not defined.
  • If a structure_ variable and a structure _name are provided, the structure is being both defined and declared.
  • If neither a structure_ variable nor a structure _name is provided, a compile-time error will result. 

If you want to initialize the structure, you must have a structure_ variable because
it signals the compiler that this is a declaration. The structure_ variable is also necessary if you want to refer to the structure.
After the structure_variable are optional initializers:
{initializer_values};
Example:
struct student
{
  int rollno;
  int marks;
} stu1;

(b).Accessing Structure Elements
In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator.
Note that before the dot there must always be a structure variable and after the dot there must always be a structure element.
Syntax:
   structure_variable.structure_element;

If the structure_element is also a structure ,the structure_element name is followed by a period and its structure_element name:
structure_variable.memberstructure. structure_element

 Example:
   stu1.rollno;

In this example, I will show you how to declare a structure, assign values and access the elements of structure.
#include<stdio.h>
struct student
{
  int rollno;
  int marks;
} stu1,stu2;
int main(){

  return 0;
}


(c).Initializing values to structure element(s)
Unlike standard variables, the syntax for initialising structure variables is different.The structure elements are accessed using the dot notation.
One can either initialize a structure by initializing the individual elements as shown below:
      stu1. rollno = 6532;
      stu1. marks = 89;

 or by simply listing the element's value inside curly braces, with each value separated by a comma.
     stu1 = {6532,89};

A structure variable can be assigned  to another providing that the structures have the same number of members and that the members are of the same data type.
  Example : stu2=stu1;

(d).How Structure Elements are Stored

Whatever be the elements of a structure, they are always stored in contiguous memory locations.

Example:
#include <stdio.h>

struct student{
    char *name;
    int rollno;
    int marks;
}stu1;
int main()
{
     /*Assigning the values of each struct element here*/
     stu1.name = "Steve";
     stu1.rollno = 1234;
     stu1.marks = 30;

     /* Displaying the values of struct element */
     printf("Student Name is: %s", stu1.name);
     printf("\nStudent roll no is: %d", stu1.rollno);
     printf("\nStudent marks is: %d", stu1.marks);
     return 0;

}

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Friday, 8 March 2019

How to stop On-Screen keyboard from popping up in Windows

It is very irritating when on-screen keyboard keeps popping-up. There are several ways you can get rid of this problem.  In this article, I will explain simple method to resolve above issue in Windows 7.
 Solution:
  (1).Click on the Start icon bottom left of computer screen.
  (2).Go to Control Panel.
   
 (3).Then click ‘Ease of Access’.


(4).Now click on ‘Use the computer without a mouse or keyboard’.
(5).You will find ‘Use On-Screen keyboard’ checked.
(6). Uncheck Use On-Screen Keyboard option.

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Thursday, 7 March 2019

Multi-Dimensional Array in C (Part 3)

In C, arrays can have more than one subscript. Arrays having more than one subscript are known as multi-dimensional arrays.
Multi-dimensional array is an array of array or more precisely collection of array.
So Two-dimensional array is the collection of one-dimensional array, Three-dimensional array is collection of two-dimensional array and so on.

In this tutorial, you will study two-dimensional arrays.
Two-dimensional array is a collection of one-dimensional array. They are logically represented a matrix. Any matrix problem can be converted easily to a two-dimensional array.

Declaring 2-D array
  • Like a variable, we must declare an array before using it.
  • Declaring an array tells the compiler the kind of values an array will hold, number of rows and columns,
  • The general syntax for declaring an array is as follows:

                       data_type   array_name[rows][columns];
   Examples:
int arr[3][4];               // array marks holds 10 integer values

Initializing 2-D Array
Initializing a two-dimensional array is similar to a one-dimensional array  with one difference. Difference is that you put the initial values for each row between braces, {}, and then enclose all the rows between braces.
Syntax
   array_name[row][columns]={
   {val1,val2…},{ val1,val2…},{ val1,val2…}..
};

Example:
int arr[3][4] = {
{ 11, 12, 13, 14 }, // Values for first row
{ 21, 22, 23, 24 }, // Values for second row
{ 31, 32, 33, 34 } // Values for third row
};
Each set of values that initializes the elements in a row is between braces, and the whole lot goes between another pair of braces. The values for a row are separated by commas, and each set of row values is separated from the next set by a comma.
If you specify fewer initializing values than there are elements in a row, the values will be assigned to row elements in sequence, starting with the first. The remaining elements in a row that are left when the initial values have all been assigned will be initialized to 0. You can initialize the whole array to 0 by supplying just one value:
int arr[3][4] = {0};

It is important to remember that while initializing a 2-D array it is necessary to mention the second (column) dimension, whereas the first dimension (row) is optional. Thus the declarations,
int arr[2][3] = { 11, 12, 13, 14, 15, 16 } ;
int arr[ ][3] = { 11, 12, 13, 14, 15, 16 } ;
are perfectly acceptable,
whereas,
int arr[2][ ] = { 11, 12, 13, 14, 15, 16 } ;
int arr[ ][ ] = { 11, 12, 13, 14, 15, 16 } ;
are invalid.

Entering data into 2-D array
Two nested loops are needed for entering data into a 2-D array.
Syntax:
int i,j;
for(i=0;i<rows;i++){
  for(j=0;j<columns:j++){
    scanf(“%d”,&arr[i][j]);
  }
}

Accessing elements of 2-D array
You need a nested loop to process all the elements in a multidimensional array. The level of nesting will be the number of array dimensions. Here’s how you could sum the elements in the previous numbers array:
Syntax:
int i,j;
for(i=0;i<rows;i++){
  for(j=0;j<columns:j++){
    printf(“%d\t”,arr[i][j]);
  }
 printf(“\n”);
}
printf("The sum of the values in the numbers array is %d.", sum);
Each loop iterates over one array dimension. For each value of i, the loop controlled by j will execute completely.

//Complete c program to read and write data into 2-D array

#include<stdio.h>
int main()
{
    int arr[3][3];
    int i,j;

    //Entering number into 2D array
    for(i=0; i<3; i++){
            for(j=0; j<3; j++){
        printf("Enter number:");
        scanf("%d",&arr[i][j]);
      }  
    }
    printf("\n");
    //Dislaying elements from 2D array
    printf("Elements of 2D array\n");
    for(i=0; i<3; i++){
            for(j=0; j<3; j++){
        printf("%d\t",arr[i][j]);
      } 
              printf("\n");
    }
    printf("\n");
   
    //Dislaying address of elements of 2D array to show that they are stored in continuous memory locations.
    printf("\nFollowing addresses shows that elements of multi dimensional array are also stored in continuous memory locations.\n");
    for(i=0; i<3; i++){
            for(j=0; j<3; j++){
        printf("%d\t",&arr[i][j]);
      } 
    }
    printf("\n");
    return 0;
}
Output:


Memory representation of a 2-Dimensional Array
Like one-dimensional array, in a two-dimensional array, the array elements are stored in one continuous memory locations.

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Single-Dimensional Array in C (Part 2)

In the previous tutorial, you learnt following things about arrays:
  1. What are arrays?
  2. Why do we need arrays?
  3. Types of arrays?
  4. Declaration and initialization of 1-D arrays

In this tutorial, you will learn entering values into already declared 1-D array and accessing elements of 1-D array.
Entering input data into an array
Once array is declared, we use for loop for entering data into array. Since array index starts with 0, for loop will start with 0;
Example:
int marks[10];
for(i=0; i<10; i++){
        printf("Enter marks of student %d\n",i+1);
        scanf("%d",&marks[i]);
    }
Reading data from an array
Since we have entered marks into marks array, we will use for loop to display the content of this array:
Example:
for(i=0;i<10;i++){
        printf("Marks of student %d are %d.\n",i+1,marks[i]);
    }
This for loop steps through the elements in the array and outputs each value. You use the loop control variable i to produce the sequence number for the value of the number of the element and to access the corresponding array element.
//Following program asks user for marks, then display them
#include<stdio.h>
int main(){
    int marks[10];
    int i;

    //Entering marks into marks array
    for(i=0; i<10; i++){
        printf("Enter marks of student %d\n",i+1);
        scanf("%d",&marks[i]);
    }
   
    //Dislaying marks from marks array
    for(i=0;i<10;i++){
        printf("Marks of student %d are %d.\n",i+1,marks[i]);
    }
    return 0;
}

Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 5 March 2019

Single-Dimensional Array in C (Part 1)

What is an array?
  • An array is the collection of elements of single/similar type.
  • These similar elements could be of all ints, all floats, all chars etc.
  • The elements of an array are referred by a single name.
  • All elements of an array must be of same type.

Why do we need arrays?
Suppose we want to store marks of 40 students of a class. There are two options:-
  1. Declare 40 variables to hold marks of 40 students. Or
  2. Declare an array of size 40 to hold marks of 40 students. 

Option 2 is better compared to first one. Because it would be much easier to handle single variable than managing 40 variables.

Types of arrays
  1. Single Dimensional (1 D) array
  2. Multi-Dimensional (n D) array where n=2,3,4…..

Single Dimensional (1 D) Array
Definition
·         Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another.
·         Memory representation of 1-D array:
Declaring 1 D array:
  • Like a variable, we must declare an array before using it.
  • Declaring an array tells the compiler the kind of values an array will hold and its size.
  • The general syntax for declaring an array is as follows
               data_type   array_name[size_of_array];


   Examples:
  • int marks[10];               // array marks holds 10 integer values
  • float salary[5];             //array salary holds 5 real numbers
  • char name[4];           //character array name can hold 4 characters


Initialiazing 1-D array
·         To initialize the elements of an array, you just specify the list of initial values between braces and separate them by commas in the declaration.
Syntax
·         array_name[size]={value1, value 2, value 3,…..};
Example:
  • marks[10]={67,87,89,90,98,65,79,86,45,33};
  • salary[5]={,887.0,689.0,970.0,968.0,695.0};
  •  name[]={‘D’,’E’,’E’,’P’};

Declaration and initiazation
·         Declaration and initialization can be grouped together.
Syntax
·         Data_type array_name[size]={value1, value 2, value 3,…..};
Examples:
·         int marks[10]={67,87,89,90,98,65,79,86,45,33};
·         float salary[5]={,887.0,689.0,970.0,968.0,695.0};
·         char name[]={‘D’,’E’,’E’,’P’};

Accessing array elements:
The elements of an array can be accessed by specifying array name followed by subscript or index inside square brackets (i.e []).
For example, accessing elements of above marks array,
first element=marks[0];
second element=marks[1];
element at ith index=marks[i];
Notes:
  1. Array subscript or index starts at 0.
  2. If the size of an array is n then the first element is at index 0, while the last element is at index n-1.
  3. The first valid subscript (i.e 0) is known as the lower bound, while last valid subscript is known as the upper bound.
  4. The C language doesn’t check bounds of the array. It is the responsibility of the programmer to check array bounds whenever required.
  5. Elements of an array are stored in continuous memory locations.
  6. An array is also known as a subscripted variable.
  7. Before using an array its type and dimension must be declared.
  8. If the array is initialized where it is declared, mentioning the dimension of the array is optional



Please comment if you find anything incorrect, or you want to improve the topic discussed above.