Thursday, August 21, 2025

ARRAY ADT

#include <iostream>
#include<algorithm>
 using namespace std;
 class ArrayADT{
int *arr;
int capacity;
int size;
public:
ArrayADT(int cap){
    capacity=cap;
arr=new int[capacity];

cout<<" you created an array with capacity "<<cap<<endl;
size=0;
}
 void insert(int value) {
        if (size == capacity) {
            cout << "Array is full! Cannot insert.\n";
            return;
        }
        arr[size++] = value;
       
    }
     void Insert(int value,int index){
        if(index<0||index>size){
            cout<<" invalid index\n ";
        }
        arr[index]=value;
     }
     void Delete(int index){
     }
    void print(){
        for(int i=0;i<size;i++){
            cout<<arr[i]<<endl;
        }
        cout<<" size of array is "<<size<<endl;
    }
    void SortAscending(){
sort(arr,arr+size);
}
void SortDescending(){
sort(arr,arr+size,greater<int>());
}

int binarySearch(int n){
    int low=0;
    int high=size-1;
    int mid;
    while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==n){
    return mid;
}
if(arr[mid]<n){
    low=mid+1;
}
else
high=mid-1;

    }
    return -1;
}
 };
 int main() {
ArrayADT A(5);
A.insert(3);
A.insert(7);
A.insert(4);
A.print();
A.Insert(5,0);
A.print();
 return 0;
 }

No comments:

Post a Comment

list in c#

  using System ; using System . Collections . Generic ; class Program {     static void Main () {         // Write your C# code here  ...