How To Stored Value in The Memorey With Figures and Examples 1D and 2D Array
How To Stored Value in The Memorey With Figures and Examples 1D and 2D Array
How To Stored Value in The Memorey With Figures and Examples 1D and 2D Array
1D AND 2D ARRAY
C++ program-- input 2D array and display the transpose matrix
#include <iostream>
using namespace std;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
// Storing element of matrix entered by user in array a[][].
cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cin >> a[i][j];
}
// Displaying the matrix a[][]
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}
// Finding transpose of matrix a[][] and storing it in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}
// Displaying the transpose,i.e, Displaying array trans[][].
cout << endl << "Transpose of Matrix: " << endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
return 0;
}
int main()
{
int arr[20],even[20],odd[20],i,j=0,k=0,no;
cout<<"How Size of Array: ";
cin>>no;
cout<<"Enter any "<<no<<" elements in Array: ";
for(i=0; i<no;i++)
{
cin>>arr[i];
}
for(i=0; i<no;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
}
cout<<"\nEven Elements: ";
for(i=0; i<j ;i++)
{
cout<<even[i]<<" ";
}
return 0;
}
C Program to Sort Elements Using Selection Sort
Algorithm
#include <stdio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
/* To sort in descending order, change > to <. */
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Here, there are 5 elements to the sorted. So, there are 4 steps.
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1]) /* To sort in descending order, change >
to < in this line. */
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
1. Step 1: The second element of an array is compared with the elements that appears
before it (only first element in this case). If the second element is smaller than first
element, second element is inserted in the position of first element. After first step, first
two elements of an array will be sorted.
2. Step 2: The third element of an array is compared with the elements that appears before
it (first and second element). If third element is smaller than first element, it is inserted in
the position of first element. If third element is larger than first element but, smaller than
second element, it is inserted in the position of second element. If third element is larger
than both the elements, it is kept in the position as it is. After second step, first three
elements of an array will be sorted.
3. Step 3: Similary, the fourth element of an array is compared with the elements that
appears before it (first, second and third element) and the same procedure is applied
and that element is inserted in the proper position. After third step, first four elements of
an array will be sorted.
Search Operation
In an unsorted array, the search operation can be performed by linear traversal from the
first element to the last element.
// Function to implement search operation
int findElement(int arr[], int n,
int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Driver Code
int main()
{
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof(arr) / sizeof(arr[0]);
// Using a last element as search element
int key = 40;
int position = findElement(arr, n, key);
if (position == - 1)
printf("Element not found");
else
printf("Element Found at Position: %d", position + 1 );
return 0;
}
Output:
Element Found at Position: 5
Insert Operation
In an unsorted array, the insert operation is faster as compared to sorted array because
we don’t have to care about the position at which the element is to be placed.
// Inserts a key in arr[] of given capacity.
// n is current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n,
int key,
int capacity)
{
// Cannot insert more elements if n is
// already more than or equal to capcity
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
// Driver Code
int main()
{
int arr[20] = {12, 16, 20, 40, 50, 70};
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
printf("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
printf("\n After Insertion: ");
for (i = 0; i < n; i++)
printf("%d ",arr[i]);
return 0;
}
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Delete Operation
In delete operation, the element to be deleted is searched using the linear search and
then delete operation is performed followed by shifting the elements.
// C program to implement delete operation in a
// unsorted array
#include<stdio.h>
// To search a key to be deleted
int findElement(int arr[], int n,
int key);
// Function to delete an element
int deleteElement(int arr[], int n,
int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);
if (pos == - 1)
{
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return - 1;
}
// Driver code
int main()
{
int i;
int arr[] = {10, 50, 30, 40, 20};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
printf("Array before deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
n = deleteElement(arr, n, key);
printf("\nArray after deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Linear Search
Problem: Given an array arr[] of n elements, write a function to search a given element
x in arr[].
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50,
110, 100, 130, 170}
x = 110;
Output : 6
Element x is present at index 6
// return -1
#include <iostream>
int i;
if (arr[i] == x)
return i;
return -1;
int main(void)
int x = 10;
return 0;
Output:
Element is present at index 3