Categories: C

One-dimensional Array

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago