coderz.py

Keep Coding Keep Cheering!

One-dimensional Array

Matrix multiplication

A one-dimensional array is one that only needs one subscript statement to show a single array element. A one-dimensional array is an organized set of parts(also known as array elements) that can be accessed separately by defining the component’s position with a single index value.

Declaring a One Dimensional Array:
  • Before using an array variable in a program, it must be declared.
  • A data type (int, float, char, double, etc.), variable name, and subscript must be defined in the declaration.
  • The subscript symbolizes the array’s size. For instance, when the subscript is 10 Programmers can store ten elements.
  • An array index always starts with 0. For instance, if an array variable is defined as s[10], it will have a value between 0 and 9.
  • Each array component is stored in its own memory region.

Let’s look at an example of a one-dimensional array to better grasp how it works.

Sorting an array:
#include<stdio.h>    
void main ()    
{    
    int i, j,temp;     
    int ar[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
    for(i = 0; i<10; i++)    
    {    
        for(j = i+1; j<10; j++)    
        {    
            if(ar[j] > ar[i])    
            {    
                temp = ar[i];    
                ar[i] = ar[j];    
                ar[j] = temp;     
            }     
        }     
    }     
    printf("Printing Sorted Element List ...\n");    
    for(i = 0; i<10; i++)    
    {    
        printf("%d\n",ar[i]);    
    }    
}  
Output:
Printing Sorted Element List ...
101
78
44
34
23
23
12
10
9
7
Passing an array into a function:

Array elements can be passed to a function by calling the function by value or by reference.

For instance: Take a look at the code above, but this time we’ve passed the array into a sorting method. The approach used here is called bubble sort, it applies to sort the elements in a given order.

#include<stdio.h>   
void Bubble_Sort(int[]);  
void main ()    
{    
    int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
    Bubble_Sort(arr);    
}    
void Bubble_Sort(int ar[]) //array ar[] points to arr.   
{  
int i, j,temp;     
    for(i = 0; i<10; i++)    
    {    
        for(j = i+1; j<10; j++)    
        {    
            if(ar[j] < ar[i])    
            {    
                temp = ar[i];    
                ar[i] = ar[j];    
                ar[j] = temp;     
            }     
        }     
    }     
    printf("Printing Sorted Element List :\n");    
    for(i = 0; i<10; i++)    
    {    
        printf("%d\n",ar[i]);    
    }  
}  
Output:
Printing Sorted Element List :
7
9
10
12
23
23
34
44
78
101

Note: also read about the Passing arguments between functions in C & Arrays in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement