Sie sind auf Seite 1von 7

P a g e |1

Arrays
CONCEPT
An array is a number of data items of the same type arranged contiguously in memory. The array is the most commonly used data storage structure. Its built into most programming languages. It is like a multiple-occurrence data structure, except that the index is explicitly specified. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula. The basic syntax of declaring an array is: Data-type variable-name [size of array]; Where size of the array is the total no values you want to declare. There are three most commonly used types of arrays: One-dimensional array Two-dimensional array Multi-dimensional array int a [i] int a [i] [j] int a [i] [j] [k]*n+

A One-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index. The mathematical concept of a matrix can be represented as a twodimensional grid, two-dimensional arrays are also sometimes called matrices. Accessing its elements involves two subscripts which represent rows and column index. In C programming a multi-dimensional array can have three or four or even ten or more dimensions. More dimensions in an array mean more data it can hold and of course more difficulties to manage and understand these arrays.

P a g e |2

BASIC OPERATIONS
There are three basic operations are: Insertion Searching Deletion

Insertion:
The data is inserted using this operation in the array. Its basic syntax is Variable-name [index value] = value to be stored The index value is the position where the value is to be stored.

Searching:
In this operation the required value is matched with the values stored in the array it will show the required output otherwise message will be displayed value not found.

Deletion:
To delete an item is should be found first. After finding the value we delete it. This operation is sometime not considered as necessary because it can be over write as well.

APPLICATIONS
1: Find the sum of elements of an array.
#include<iostream>; #include <stdlib.h> using namespace std; int main() { int s; char c='y'; while(c=='y') { cout<<"Enter no of values you want to sum: "; cin>>s;

P a g e |3

int array[s]; cout<<"Enter "<<s<<" values:"; for(int i=0;i<s;i++) { cin>>array[i]; } int j=0; int sum=0; for(int i=0;i<s;i++) { sum=sum+array[i]; } cout<<"Sum is:"<<sum<<endl; cout<<"\nDo you want to find another sum?(y/n)"; cin>>c; system("CLS"); } }

Output:

2: Find the largest of the elements of an array.


#include<iostream>; #include <stdlib.h> using namespace std; int main() { int s; char c='y'; while(c=='y')

P a g e |4

{ cout<<"Enter no of values you want to sum: "; cin>>s; int array[s]; int max; cout<<"Enter "<<s<<" values:"; for(int i=0;i<s;i++) { cin>>array[i]; } max=arr[0]; for(int j=0;j<s;j++){ if(max<arr[j]) { max=arr[j; } } cout<<"Maximun no is: "<<max; cout<<"\nDo you wan to find another maximun no.?(y/n)"; cin>>c; system("CLS"); } }

Output:

P a g e |5

STACK
CONCEPT
It is a linear data structure. A stack is a type of data structure, a means of storing information in a computer. When a new object is entered in a stack, it is placed on top of all the previously entered objects. In other words, the stack data structure is just like a stack of cards, papers, credit card mailings, or any other real world objects you can think of. When removing an object from a stack, the one on top gets removed first. This method is referred to as LIFO (Last In, First Out).

BASIC OPERATIONS
There are three basic operations are: Insertion (Push) Deletion (Pop)

Insertion (Push):
It is used to insert data in the stack. The data is always entered on the top end and a pointer always points towards the last entered value when a new value is entered it moves upwards. If it reaches to the upper limit of stack no more data can be inserted and Stack overflow message is displayed.

Deletion (Pop):
It is used to remove data from stack. The value on the top most is removed first then the value present at the top after it. The value inserted in the stack at first will be removed at last. If no more data is remaining in the stack or if stack will become empty then Stack Underflow message is displayed.

P a g e |6

ALGORITHM
Push Operation: 1. For insertion of any new item, confirm that stack is not
having status Overflow.

2. If it is confirmed that stack is not over flow then increment


top variable (pointer) i.e. top++.

3. Insert the new item on the position pointed by top. Pop Operation: 1. Confirm that the stack statue is not Underflow. 2. Remove the item pointed by the top. 3. Decrement the top variable i.e. top--.

ADT IMPLEMENTATION
The basic code of Stack ADT is following: class Stack{ private: int top; int arr[10]; public: Stack(); void push(); void pop(); }; Stack::Stack() { top=-1; }

P a g e |7

void Stack:: push() { int n; if(top==9) { cout << "stack overflow\n Cannot enter new data"; return; } cout<<"Enter Value: "; cin>>n; arr[++top]=n; } void Stack::pop() { if(top==-1) { cout << "Stack is underflow (Empty)"; return ; } else cout<<"Data Popped: "<<arr[top--]; }

Das könnte Ihnen auch gefallen