Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday 10 March 2022

35.Introduction to arrays

Introduction to arrays:

 

Any variable belongs to primary type can store only a single value.

 

Example:

#include<stdio.h>

void main()

{

   int x=20;

  

   x=25;

   printf("x=%d",x);

  

}

 

Output:

x=25

 

Example explained:

"x" is initialized with 20 and then assigned with 25.

Here 20 is overwritten by 25 so the output is 25.

   

Need of arrays:

Some times we need to store multiple values of the same type in a common memory for example storing the marks of a student in 6 subjects, sales of a product throughout the year etc.

It may be cumbersome to declare and use number of variable of the same type.

An array is a derived type used to store multiple values of the same type under a common memory allocation.

 

Array:

Array is a set of data items (elements) of the same type stored in contiguous memory allocations under a common name.

Array is also called subscript data type.

The type of elements, the name of memory allocation and the number of elements to be stored are specified in declaration.

Every element in an array is identified with its index.

In C Language Index of the first element is always 0 and the last element is n-1 where n is the size of array.

Every element is accessed with its subscript and works like a normal variable. These elements are free to participate in arithmetic, relational and logical operations.

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:

Accept two numbers and print the sum, subtraction, product and division of them.

 

Program:

#include<stdio.h>

void main()

{

float a[6];

 

printf("Enter two numbers:");

scanf("%f%f",&a[0],&a[1]);

a[2]=a[0]+a[1];

a[3]=a[0]-a[1];

a[4]=a[0]*a[1];

a[5]=a[0]/a[1];

printf("Sum=%f",a[2]);

printf("\n Subtraction=%f",a[3]);

printf("\n Product=%f",a[4]);

printf("\n Division=%f",a[5]);

 

}

 

Execution:

Enter two numbers:

52.50

12.25

Sum=64.750000

Subtraction=40.250000

Product=643.125000

Division=4.285714

 

 

 

 

 

 

 

 

 

 

 

 

 


Example explained:

Address of (&) symbol is used to accept the data from the keyboard and store into the specified element.

scanf("%f%f",&a[0],&a[1]); accepts two numbers from the keyboard and stores into a[0] and a[1].

Addition, subtraction, product and division of these elements are stored into a[2], a[3], a[4] and a[5].

Finally the values of  a[2], a[3], a[4] and a[5] are printed as output.

 

Initializing while declaration:

By default all the elements of an array are stored with garbage values (unknown).

Assigning a set of elements while declaration of an array is called initializing while declaration.

Elements are assigned to the array in the same order as in the order of set.

It is optional to specifying the size of array while its initialization.

Syntax:

 

<data type>   <name>[ <size> ]={ set of elements }

 
 

 

 

 

 


Example:

#include<stdio.h>

void main()

{

int a[]={45,56,78,65,25};

int i;

 

printf("The elements are:");

for(i=0;i<5;i++)

    printf("%5d",a[i]);

 

}

 

Output:

45        56        78        65        25

 

Example explained:

Individual elements can be printed using printf("%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4]); but is cumbersome

Here the for loop i=0;i<5;i++ is used to access elements one by one. When "i" is 0 then a[0] that is 45 is accessed, when "i" is 1 then a[1] that is 56 is accessed.

The index variable "i" could access elements of array one by one as the loop progresses.


0 comments:

Post a Comment