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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago