Single Dimensional Array

Single Dimensional Array


Name: - Sanjyot Sanjay Mankar

Urn no: - 2020-B-03022003

Class: - BCA CTMA

Course Name : - C Programming

 

· what's an array?

Ø An array may be a collection of 1 or more values of an equivalent type. Each value is named a component of the array. the weather of the array shares an equivalent variable name but each element has its own unique index (also referred to as a subscript). An array is often of any type, for instance, int, float, char, etc. an array is must be of type int only.

Ø To store roll no. of 100 students, we've to declare an array of size 100 i.e, roll_no[100]. Here size of the array is 100, so it's capable of storing 100 values. "In C, index or subscript starts from 0, so roll_no[0] is that the primary element, roll_no[1] is that the second element, and so on.". Note that the last element of the array is going to be at roll_no[99] not at roll_no[100] because the index starts at 0.

Ø Arrays are often single or multidimensional. the amount of subscripts or indexes determines the size of the array. An array of 1 dimension is understood as a one-dimensional array or 1-D array, while an array of two dimensions is understood as a two-dimensional array or 2-D array.

· One dimensional Array: -

Ø Array elements are stored one after one.

Ø Syntax: datatype array_name[size];

Ø datatype: It denotes the sort of the weather within the array.

Ø array_name: Name of the array. It must be a legitimate identifier.

Ø size: Number of elements an array can hold. here are some example of array declarations:

 1)Int num[100];

 2)float tem[20];

 3)char ch[50];

num is an array of type int, which may only store 100 elements of type int.

temp is an array of type float, which may only store 20 elements of type float.

ch is an array of type char, which may only store 50 elements of type char.

 

Note: When an array is said it contains garbage values.

Accessing The element of an array: -

The elements of an array are often accessed by specifying array name followed by subscript or index inside square brackets (i.e []). Array subscript or index starts at 0. If the dimensions of an array are 10 then the primary element is at index 0, while the last element is at index 9. the primary valid subscript (i.e 0) is understood because the boundary, while the last valid subscript is understood because of the boundary.

 

int my_arr[5];

 

Then elements of this array are;

 

·        First element – my_arr[0]

·        Second element – my_arr[1]

·        Third element – my_arr[2]

·        Fourth element – my_arr[3]

·        Fifth element – my_arr[4]

 Processing 1-Dimensional Array: -

Example no 1: - Simple C program to access the array.

#include

int main()

{

 int arr[5], i;

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

 {

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

 scanf("%d", &arr[i]);

 }

 printf("\nPrinting elements of the array: \n\n");

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

 {

 printf("%d ", arr[i]);

 }

 return 0;

}

 

Output: -

Enter a[0]: 11

Enter a[1]: 22

Enter a[3]: 4

Enter a[2]: 34

Enter a[4]: 34

Printing elements of the array:

11 22 34 4 34

 

Explain the above program: -

In this above program, we've declared an array of 5 integers

and variable i of type int. Then a for loop is

used to enter five elements into an array.

In scanf() we've used & operator (also referred to as the address of

operator) on element arr[i] of an array, a bit like we had through with variables of type int, float, char, etc.

"Printing elements of the array" to the console. The second for loop prints all the

elements of an array one by one.

 

Example no 2: -

#include

int main()

{

 int arr[5], i, s =0;

 for(i = 0; i

  {      

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

scanf("%d", &arr[i]);

    }

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

    {

        s += arr[i];

    }

    printf("\nSum of elements = %d ", s);

    return 0;

}

Output: -

Enter a[0]: 22

Enter a[1]: 33

Enter a[2]: 56

Enter a[3]: 73

Enter a[4]: 23

Sum of elements = 207

 

In this above program; we see that the first for loop asks

the user to enter five elements into

the array. The second for loop reads all the elements of an

array one by one and accumulate the sum of all the elements in the

variable s.

Note: -that it is necessary to initialize the variable s to 0, otherwise, we will

get the wrong answer because of the garbage value of s.

 

Initializing An Array

When an array is declared inside a function the elements of the array has a garbage value. If an array is global or static, then its elements are automatically initialized to 0. We can explicitly initialize elements of an array at the time of declaration using the following syntax:

·        Syntax: datatype array_name[size] = { val1, val2, val3, ..... valN };

·        the data type is the type of elements of an array.

·        array_name is the variable name, which must be any valid identifier.

·        size is the size of the array.

·        val1, val2 ... are the constants known as initializers. Each value is separated by a comma(,) and then there is a semi-colon (;) after the closing curly brace (}).

 

Example: -

float temp[5] = {12.3, 4.1, 3.8, 9.5, 4.5}; // an array of 5 floats

 

int arr[9] = {11, 22, 33, 44, 55, 66, 77, 88, 99}; // an array of 9 ints


Comments

Popular posts from this blog

Elementary Data Structures