Docsity

Arrays ( Data Structures And Algorithms ), Exams of Data Structures and Algorithms

Partial preview of the text

Download Arrays ( Data Structures And Algorithms ) and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structure

Arrays

2

Introduction

Linear data structure. Is used to store similar types of data. An array is a

finite collection of similar elements stored in adjacent memory

locations.

Decleration of the Arrays: (In C)

the array name, the element type and the array size.

Examples:

/ / one-dimensional

arrays

int a[20],

b[3],c[7];

float f[5], c[2];

char m[4], n[20];

Initialization of an array is the process of assigning initial values.

Examples:

float, b[3]={2.0, 5.5, 3.14};

char name[4]=

{โ€˜Eโ€™,โ€™mโ€™,โ€™rโ€™,โ€™eโ€™}; int c[10]={0};

4

Two- dimensional arrays

5

Two- dimensional arrays

7

#include<iostream.h>

int main ()

int i,n; int * p;

cout << "How many numbers would you like to type?

"; cin >> i;

p= new int[i]; / / it takes memory at run-time from

Heap if(p == NULL)

cout << "Error: memory could not be allocated";

else

for(n=0; n<i; n++)

cout << "Enter number:

"; cin >> p[n];

int*k=p; / / to hold the base address

of dynamic array

cout << "You have entered: \n";

for(n=0; n<i; n++) cout << *k<< ", "; k++;

cout<<"\n"; delete[] p; / / it release the memory to send it back to

Heap

return 0;

Sample code for

Dynamic

Array declaration

7

9

Travers, Insert, Delete on Arrays

a) Traversing in Linear Array

1

0

Travers, Insert, Delete on Arrays

c) Deleting from Linear

Array

1.C++ program to store and calculate the

sum of 5 numbers entered by the user

using arrays.

2. Write a program how insert, delete and

traverse the array.

Example for Array