int arr [5] = {1, 3, 5, 7, 9};
cout << arr[3]; // arr[3] i.e. index 3 of the array will print the value 7
Manipulation of Elements of One Dimensional Array
We will use the next way to alter a specific member included in an array:
Example 1 of Manipulating Elements of One Dimensional Array
C++
#include <iostream>
using namespace std;
int main()
{
int arr [7] = {10, 20, 30, 40, 55, 60, 70};
cout << "5th value of Array Before updation: \n" << arr[4];
arr [4] = 50;
cout << "\n 5th value of Array After updation: \n" << arr[4];
}
Output
5th value of Array Before Updation:
55
5th value of Array After Updation:
50
By using the subsequent technique, we can utilize 1D arrays to calculate the average and sum of the items in the array:
Example 2 of Manipulating Elements in One Dimensional Array
C++
#include <iostream>
using namespace std;
int main()
{
int sum = 0, avg = 0;
int num [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;
for (int i = 0; i < 10; i++ )
{
sum = sum + num [i] ;
}
cout << "Sum of all elements is: " << sum ;
avg = sum / 10 ;
cout << "\n Average is: " << avg ;
}
Output
Sum of all elements is: 55
Average is: 5
Declaration of Strings in One Dimensional Array
One-dimensional arrays can incorporate alphabetic values in addition to numeric data. The only thing that strings are is a collection of characters; alternatively, we might say that strings are an array of characters. Using the char data type, strings may also be stored in arrays. Initializing a string array requires the following syntax:
Syntax of Declaration of String Using One Dimensional Array:
char string_name [string_size] = {comma_seperated_character_list} ;
Example of Declaration of String Using One Dimensional Array:
char str [20] = {"Hello World"};
cout << str;
Output
Hello World
The length of a string may be determined, two strings can be compared, a string can be copied, or reversed, certain words or alphabets can be removed from the strings, the number of words or letters can be counted, etc. using strings in a 1D array.
Applications of One Dimensional Array
The Applications of One Dimensional Array are listed below.
Other data structures like stacks, queues, heaps, graphs, etc. are implemented using one-dimensional arrays.
We can execute operations on 1D arrays using them, including identifying the location of any element within the array, determining the largest and smallest element inside the array, adding and deleting elements, merging two arrays, and more.
Additionally, sorting algorithms like Insertion Sort, Bubble Sort, Selection Sort, Merge Sort, etc. are implemented using 1D arrays.
Conclusion
One-dimensional arrays are a fundamental and versatile data structure in programming, allowing for the storage and manipulation of elements of the same data type in a contiguous memory space. In this conclusion, we’ll summarize key points and address frequently asked questions related to one-dimensional arrays.
Frequently Asked Questions (FAQs) related to One-Dimensional Array
Here are some Frequently Asked Questions related to “One Dimensional Array”.
1. What is the main characteristic of a one-dimensional array?
A one-dimensional array is characterized by its linear structure, where elements are stored in a single row or column. Elements in a one-dimensional array are accessed using a single index.
2. How are one-dimensional arrays different from other types of arrays, like multi-dimensional arrays?
One-dimensional arrays store elements in a linear sequence, while multi-dimensional arrays store elements in a grid-like structure with rows and columns. Multi-dimensional arrays are used when data requires more complex organization.
3. What are common use cases for one-dimensional arrays?
One-dimensional arrays are frequently used for storing lists, implementing data structures like stacks and queues, performing mathematical operations on sets of numbers, and representing sequences, among other applications.
4. Are one-dimensional arrays limited to a single data type?
No, one-dimensional arrays can store elements of various data types, but all elements within a single array must be of the same data type.
5. What is the time complexity of accessing an element in a one dimensional array?
Ans. The time complexity of accessing an element in a one dimensional array is O(1).
For better understanding of Arrays you can check out these Articles.
Types of Arrays
Base Address of a Two-Dimensional Array
Post navigation
Previous
Previous post:
What is Double-Ended Queue in Data Structure?
Next
Next post:
Array in C
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Search for:
Search …
Pages
ALGORITHMS
ARRAY
BACKTRACKING
C PROGRAMMING LANGUAGE
C++ PROGRAMMING LANGUAGE
CAPGEMINI
CIRCULAR LINKED LIST
COMPANY PLACEMENT PROCEDURE
COMPETITIVE CODING
COMPUTATIONAL GEOMETRY
CSE SUBJECTS
DATA STRUCTURE
DOUBLY LINKED LIST
DYNAMIC PROGRAMMING
GAME THEORY
GRAPHS
GREEDY ALGORITHM
HASHING
HEAP
INTERVIEW PREPARATION
INTERVIEW TIPS
JAVA PROGRAMMING LANGUAGE
JAVASCRIPT PROGRAMMING LANGUAGE
Languages
LINKED LIST
LINKED LIST USING C
MATHEMATICS
OPERATING SYSTEM
POINTERS
PYTHON PROGRAMMING LANGUAGE
QUEUE
RECURSION
SEARCHING
SEGMENT TREE
SORTING
STACK
STRING