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.
Let’s look at an example of a one-dimensional array to better grasp how it works.
#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]);
}
}
Printing Sorted Element List ...
101
78
44
34
23
23
12
10
9
7
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]);
}
}
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…