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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…