Sei sulla pagina 1di 22

HOW TO STORED VALUE IN THE MEMOREY

WITH FIGURES AND EXAMPLES

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;
}

C++ program -- Find Even Elements in Array and display


output
#include<iostream>
using namespace std;

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.

Program To Sort Elements using Bubble Sort


Algorithm

/*C Program To Sort data in ascending order using bubble sort.*/


#include <stdio.h>
int main()
{
int data[100],i,n,step,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(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;
}

How insertion sort Algorithm works?


Explanation

Suppose, you want to sort elements in ascending as in above figure. Then,

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.

If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get


sorted list of array.
C Program Sort Elements Using Insertion Sort
Algorithm

/*Sorting Elements of an array in ascending order using insertion sort


algorithm*/
#include<stdio.h>
int main()
{
int data[100],n,temp,i,j;
printf("Enter number of terms(should be less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
for(i=1;i<n;i++)
{
temp = data[i];
j=i-1;
while(temp<data[j] && j>=0)
/*To sort elements in descending order, change temp<data[j] to
temp>data[j] in above line.*/
{
data[j+1] = data[j];
--j;
}
data[j+1]=temp;
}
printf("In ascending order: ");
for(i=0; i<n; i++)
printf("%d\t",data[i]);
return 0;
}

Search, insert and delete in an unsorted array

Search Operation
In an unsorted array, the search operation can be performed by linear traversal from the
first element to the last element.

// C program to implement linear 


// search in unsorted array
#include<stdio.h>

  
// 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.

// C program to implement insert 


// operation in an unsorted array.
#include<stdio.h>

  
// 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

Input : arr[] = {10, 20, 80, 30, 60, 50,


110, 100, 130, 170}
x = 175;
Output : -1
Element x is not present in arr[].
A simple approach is to do linear search, i.e
 Start from the leftmost element of arr[] and one by one compare x with each element of
arr[]
 If x matches with an element, return the index.
 If x doesn’t match with any of elements, return -1.
// C++ code to linearly search x in arr[]. If x

// is present then return its location, otherwise

// return -1

  

#include <iostream>

using namespace std;

  

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]);


    int result = search(arr, n, x);

   (result == -1)? cout<<"Element is not present in array" 

                 : cout<<"Element is present at index " <<result;

   return 0;

Output:
Element is present at index 3