Showing posts with label 1-D array. Show all posts
Showing posts with label 1-D array. Show all posts

Thursday, 7 March 2019

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.