Skip to main content

Posts

Reversing an Array

 In this post, we will learn "How to reverse an Array". Let us start by taking an array as input from the user and then we will reverse it. Steps to follow: Move element at the first index to last, and element at last index to first Move element at the second index to second last, and element at second last index to the second and so on Let's have a look at the code, how to do this work: #include<iostream> using namespace std ; int main() { int arr[100], tot, i, j, temp; cout << "Enter the Size for Array: " ; cin >>tot; cout << "Enter " <<tot<< " Array Elements: " ; for (i=0; i<tot; i++) cin >>arr[i]; cout << " \n The Original Array is: \n " ; for (i=0; i<tot; i++) cout <<arr[i]<< " " ; j = tot-1; for (i=0; i<j; i++, j--) { temp = arr[i]; //swaped first element with last element ...
Recent posts

Searching in an Array: Linear Search

 In this post, we will learn "How to search for an element in an array". To search for an element in an array first you have to traverse through all the elements in the given array. This method is known as Linear search. The full code is given below: C++ code to implement the linear search operation Here x is the element you want to search in the given array and we will output the position of that element if it is present otherwise -1. #include <iostream> using namespace std; //Function for linear search int search(int arr[], int n, int x) { int i; for (i = 0; i < n; i++) if (arr[i] == x) return i; return -1; } int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int x = 10; int n = sizeof(arr) / sizeof(arr[0]); //way to find out how many elements are present in array int result = search(arr, n, x);   // Function call (result == -1) ? cout << "-1"; : cout << "Element is present at index " << result; ...

Taking array as an input.

 In this post, we will learn about How to take an array as input in C++. Firstly, to take an array as input, you should know about "for loop". For taking an array as input we have to first take input the size of the array and then, we should declare the array of that given size. Example: int n;       //Here we first declared the size of the input array cin>>n;       int Array[n];     //Here we declared an array of the input size Now to take the elements of the array as input we have to use for loop: Syntax of for loop:   for(initialization; condition,operation) Now to take elements as input, do as follows: for(int i=0;i<n;i++){ cin>>Array[i]; } In this way, we can take input an array. Let's take a look at whole code at once: int n; cin>>n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; }; I hope, you all understood this concept.

Arrays

  In this post, we will learn about what is an array? and declaration of arrays. What is an Array? An array can be defined as a collection of data, but the data type of all the elements should be the same i.e., all the elements should be of integer type or all be of character type, etc but not the mixture of data types like two elements are integer and other are characters. Declaration of an Array: The syntax of the array declaration in C++ is as follows: dataType_name_[size]={elements separated by commas}; for example: 1. int A[5]={1,2,3,4,5}; This means that I have declared an array of data type int and of size 5 having elements 1,2,3,4,5. 2.char B[3]={'A','B','C'}; This means that I have declared an array of data type character of size 3 having A, B, and C as elements. In character array, you have to keep the elements in single quotes.    As in the image above, you can see, I have declared an array of size 6 having 10,20,30,40,50,60 as elements. The numbers w...