1-D Array in C language

“Some people want it to happen, some wish it to happen; others make it happen.”

Hey you beautiful people!!!!
Today we will discuss 1-D array in detail.
I hope you all must be excited!!!
Soo let’s start quickly 🙂

Accessing 1D-Array:-
1) The elements of an array can be accessed by specifying array name followed by subscript or index inside square brackets (i.e []).
2) Array subscript or index starts at 0. If the size of an array is 10 then the first element is at index 0, while the last element is at index 9. The first valid subscript (i.e 0) is known as the lower bound, while last valid subscript is known as the upper bound.

Representation of 1-D array

•Initializing Arrays:-
•You can initialize an array in C either one by one or using a single statement as follows –
•int balance[5] = {100, 200, 300, 400, 500}; The number of values between braces { } cannot be larger than the number of elements that we declare for the array between Square brackets [ ].
• If we initialize int balance[4] = 50;
• Then fifth element will be assigned a value 50 as indexing starts from 0.

Eg1:-Simple Program to Accept and Display One Dimensional Array.

#include <stdio.h>

int main()
{
int values[5];

printf(“Enter 5 integers:”);

// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf(“%d”, &values[i]);
}

printf(“Displaying integers:”);

// printing elements of an array
for(int i = 0; i < 5; ++i)
{
printf(“%d\n”, values[i]);
}
return 0;
}

Output:-

Enter 5 integers:1

2

3

4

5

Displaying integers:1

2

3

4

5

Eg2:-To find Maximum from the array elements.

For example Consider the array elements as {10,20,30,40}
Step 1:- First accept the array elements
Step 2:- Initialize the first element in array to be maximum ie :- max=a[0]
Initially max=10
Step 3:- Then traverse the array from second element ie:-
Compare max with second element
If(a[i]>max) if this condition is true then
Maximum is current a[i].
In this case 20>10 condition is true so max will have 20 .
Stored now. Max=20
Step 4:- This will continue till we reach the end of the array.
Then 30>20 condition is true max=30
Step 5:- Lastly element 40 will be compared with the current max.
40>30 This condition is also true so max will be updated now to 40.
Final max will be 40 .

Program:-
#include <stdio.h>
void main()
{
int i, n;
int a[10],max;
printf(“Enter the number of elements:”);
scanf(“%d”, &n);
for (i = 0; i < n; i++)
{
printf(“Enter element of array:”);
scanf(“%d”, &a[i]);
}
max=a[0];
for (i = 0; i < n; i++)
{
if (a[i] >max)
max=a[i];
}
printf (“Max of element in array is: %d”, max);
}

Output:-
Enter the number of elements:4

Enter element of array:10

Enter element of array:20

Enter element of array:30

Enter element of array:40

Max of element in array is :40

Eg3:-Write a program to accept array and to display it in reverse order.

#include<stdio.h>
int main()
{
int n, i;
printf(“Enter the size of an array : “);
scanf(“%d”,&n);
int arr[n];
//To take the input in array
for(i = 0; i < n; i++)
{
printf(“Please give value for index %d : “,i);
scanf(“%d”,&arr[i]);
}
printf(“Array printing in reverse order is:\n”);
//for loop to print array in reverse order
for(i=n-1;i>=0;i–)
{
printf(“%d\n”,arr[i]);
}
return 0;
}

Output:-
Enter the size of an array : 5
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 5
Array printing in reverse order is:
5
4
3
2
1

•To search an element in the given array linear search:-
Step 1:- Accept no of elements in the array.
Step 2:- Accept the values of all elements in the array.
Step 3:- Accept the number to be searched.
Step 4:- Compare the element to be searched with the array elements from first index.
Step 5:- If the index of the found element is less than n and the search element value is similar to any element from the array then the element is found otherwise the element is not found.

Eg4:- Write a ‘C’ program to accept n integers in an array and search for a specific number.

#include <stdio.h>
void main()
{
int a[10],i, n, search;
printf(“Enter size of the array : “);
scanf(“%d”, &n);
printf(“Enter elements in array : “);
for(i=0; i<n; i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter the element to search : “);
scanf(“%d”, &search);
for(i=0; i<n; i++)
{
if(a[i]==search)
{
printf(“element found at %d position”,i);
break;
}
}
}

Output:-
Enter size of the array : 5
Enter elements in array : 1 2 3 4 5
Enter the element to search : 3
element found at 2 position


Eg5:- Write a C program to count the occurrence of a number in array.

#include <stdio.h>
main()
{
int a[10],n, i;
int num, count;
printf(“Enter total number of elements: “);
scanf(“%d”,&n);
printf(“Enter array elements:-\n”);
for(i=0;i< n;i++)
{
printf(“Enter element:”);
scanf(“%d”, &a[i]);
}
printf(“Enter number to find Occurrence: “);
scanf(“%d”,&num);
count=0;
for(i=0;i< n;i++)
{
if(a[i]==num)
count++;
}
printf(“Occurrence of %d is: %d\n”,num, count);
}

Output:-
Enter total number of elements: 5
Enter array elements:-
Enter element:1
Enter element:2
Enter element:3
Enter element:2
Enter element:2
Enter number to find Occurrence: 2
Occurrence of 2 is: 3

Soo these are some examples on 1-D array.
May this article be worth reading 🙂

Soo see you all in next article….. Till then goodbye.
Have a wonderful day ahead 🙂

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: