Sie sind auf Seite 1von 6

CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P.

GUILALAS

St. Paul University Surigao


COLLEGE OF ENGINEERING
Surigao City, Philippines

Data Structures and


Algorithm Analysis
Laboratory Exercises

PREPARED BY:
ENGR. ANGELUS VINCENT P. GUILALAS

Laboratory Exercises Page 1


CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P. GUILALAS

Student Report
CpE 102 DATA STRUCTURES AND ALGORITHM ANALYSIS (Lab)
2ND Semester, SY 2012-2013

Name: Joseph Bryan J. Sarvida Course and Year: BSCpE-III

Laboratory Exams

Major Exams Perfect Score Score Remarks

Midterm Exam

Final Exam

Laboratory Exercises

Instructor’s
Exercise # Title Date Performed Rating
Signature

ARRAY
1 Dec. 5, 2017
Insertion at the Beginning of an Array

Laboratory Exercises Page 2


CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P. GUILALAS

Exercise No. 5
Deletion Operation
OBJECTIVES

1. Provide practical implementation of insertion of array.


2. Implement insertion of array at the beginning using C++
3. Illustrate the insertion operation in array.

EQUIPMENT

Quantity
1 PC with Microsoft Visual Studio C++ per student

DISCUSSION

Array

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures
make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.

Element − each item stored in an array is called an element.


Index − each location of an element in an array has a numerical index, which is used to identify the element.

Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

As per the above illustration, following are the important points to be considered.

Index starts with 0.


Array length is 8 which means it can store 8 elements.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.

Laboratory Exercises Page 3


CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P. GUILALAS

Basic Operations

Following are the basic operations supported by an array.

Traverse − Prints all the array elements one by one.


Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.

Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be
added at the beginning, end, or any given index of array.
Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm
to delete an element available at the Kth position of LA.

Implementation in C++
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#define MAX 5
using namespace std;

int main()
{
int array[5];
int i;
int pos;

cout << "Enter Data in Array: \n";


for(i=0;i<5;i++)
{
cin >> array[i];
cout<< " Array["<<i<<"] = "<<array[i]<<endl;
}
cout << "Enter the position to delete: ";

Laboratory Exercises Page 4


CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P. GUILALAS

cin >> pos;


for (int i=pos; i<5; i++)
{
int temp = array[i];
array[i]=array[i+1];
array[i+1]=temp;

}
cout<<"\nNew data in Array: \n";
for(i=0;i<5-1;i++)
{
cout<< "Array["<<i<<"] = "<<array[i]<<endl;
}

return 0;
}

Screen Shoots

Laboratory Exercises Page 5


CPE 110L DATA STRUCTURES AND ALGORITHM ANALYSIS ENGR. ANGELUS VINCENT P. GUILALAS

GENERALIZATION

Since it was the same at the beginning of array I change some of code to run a deletion in array. I use C++.

Laboratory Exercises Page 6

Das könnte Ihnen auch gefallen