How to Convert Array C Code to C++ Code

How to Convert Array C Code to C++ Code with Examples:

In this article, we will see how to convert C language code to C++ code with Examples. In our previous article, we have seen how we can perform different operations on an array and create a Menu-Driven Program using C language. In C language, we have used the ‘struct’ keyword but in C++ we will use the ‘class’ keyword:

class List {
private:
 int *B;
 int size;
 int length;
public:
 List() {
  size = 10;
  length = 0;
  B = new int[size];
 }
 List(int size) {
  this.size = size;
  length = 0;
  B = new int[size];
 }
 ~List() {
  delete[]B;
 }
};

In C++, we are using class with access modifiers i.e. public, private. Any object of this class can access only public members, not private members.

In class, we declared constructor, parameterized constructor, and destructor. The constructor will call at initialization of object and it will initialize all the given fields with default values as we did in the above code. The destructor will delete the dynamically allocated memory of a variable or array.

We have to remove the usage of this symbol- ‘->’ because all the variables are member variables in a class, so we no longer need this symbol. Also, we will use the ‘new’ operator instead of the ‘malloc’ function. And we will declare all the operation’s function prototypes inside the public modifier in the above class. Then what modification do we have to do in those functions? We have to remove the ‘struct’ keyword. Let’s see these all changes in the code.

Full code of all operations in C++ language:
#include <iostream>
#include <conio.h>
using namespace std;

class List {
private:
 int *B;
 int size;
 int length;
public:
 List() {
  size = 10;
  length = 0;
  B = new int[size];
 }
 List(int sz) {
  size = sz;
  length = 0;
  B = new int[size];
 }
 ~List() {
  delete[]B;
 }

 void Display();
 void Append(int x);
 void Insert(int index, int x);
 int Delete(int index);
 void swap(int* x, int* y);
 int LinearSearch(int key);
 int ImprovedLinearSearch(int key);
 int BinarySearch(int key);
 int RBinSearch(int a[], int l, int h, int key);
 int Get(int index);
 void Set(int index, int x);
 int Max();
 int Min();
 int Sum();
 int RSum(List list, int n);
 float Avg();
 void Reverse();
 void Reverse2();
 void InsertSort(int x);
 int isSorted();
 void arrangeNegPos();
 List* Merge(List *list1, List *list2);
 List* Union(List *list1, List *list2);
 List* Intersection(List *list1, List *list2);
 List* Difference(List *list1, List *list2);
};

void List::Display() {
 int i;
 printf("\nElements are\n");
 for (i = 0;i<length;i++)
  printf("%d ", B[i]);
}

void List::Append(int x) {
    if (length < size)
       B[length++] = x;
}

void List::Insert(int index, int x) {
    if (index >= 0 && index <= length) {
       for (int i = length; i > index; i--) {
           B[i] = B[i - 1];
       }
       B[index] = x;
       length++;
    }
}

int List::Delete(int index) {
    int x = 0;
    if (index >= 0 && index < length) {
        x = B[index];
        for (int i = index; i < length - 1; i++)
            B[i] = B[i + 1];
            length--;
            return x;
        }
    }

void List::swap(int* x, int* y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int List::LinearSearch(int key) {
    for (int i = 0; i < length; i++) {
       if (key == B[i])
          return i;
    }
    return -1;
}

int List::ImprovedLinearSearch(int key) {
    int i;
    for (i = 0; i < length; i++) {
       if (key == B[i]) {
          swap(&B[i], &B[0]);
          return i;
       }
    }
    return -1;
}

int List::BinarySearch(int key) {
    int l, mid, h;
    l = 0;
    h = length - 1;
    while (l <= h)
    {
       mid = (l + h) / 2;
       if (key == B[mid])
          return mid;
       else if (key < B[mid])
          h = mid - 1;
       else
          l = mid + 1;
     }
     return -1;
}

int RBinSearch (int a[], int l, int h, int key){
    int mid;
    if (l <= h)
    {
        mid = (l + h) / 2;
        if (key == a[mid])
         return mid;
        else if (key < a[mid])
         return RBinSearch (a, l, mid - 1, key);
        else
         return RBinSearch (a, mid + 1, h, key);
    }
    return -1;
}

int List::Get(int index){
 if(index >= 0 && index < length)
 return B[index];
 
 return -1;
}

void List::Set(int index,int x){
 if(index >= 0 && index < length)
  B[index] = x;
}

int List::Max(){
 int max = B[0];
 	for(int i = 1;i < length; i++){
 		if(B[i] > max)
 		max = B[i];
 	}
 	return max;
}

int List::Min(){
 int min = B[0];
 	for(int i = 1;i < length; i++){
 		if(B[i] < min)
 		min = B[i];
 	}
 	return min;
}

int List::Sum(){
    int sum = 0;
    for(int i = 0;i < length; i++)
    sum+=B[i];
    return sum;
}

int List::RSum(List list, int n){
    if (n < 0)
        return 0;
    else
        return RSum(list, n-1) + B[n];
}

float List::Avg(){
 return (float)Sum()/length;
}

void List::Reverse(){
    int *C;
    int i,j;
 
 C = new int[length];
    for(i=length-1,j=0;i>=0;i--,j++)
        C[j]=B[i];
    for(i=0;i<length;i++)
        B[i]=C[i];
}

void List::Reverse2(){
    int i,j;
    for(i=0,j=length-1;i<j;i++,j--){
        swap(&B[i],&B[j]);
    }
}

void List::InsertSort(int x){
 int i = length-1;
 
 if(length == size)
  return;
 while(i >= 0 && B[i] > x)	{
  B[i+1] = B[i];
  i--;
 }
 B[i+1] = x;
 length++;
}

int List::isSorted(){
    int i;
    for(i=0;i<length-1;i++){
        if(B[i]>B[i+1])
            return 0;
    }
    return 1;
}

void List::arrangeNegPos(){
 int i, j;
 i = 0;
 j = length - 1;
 while(i < j){
  while (B[i] < 0) i++;
  while (B[j] >= 0) j--;
  if (i < j) swap (&B[i], &B[j]);
 }
}

List* List::Merge(List *list1, List *list2){
 int i,j,k;
 	i = j = k = 0;
 
 	List *list3 = new List();
 
 	while(i < list1->length && j < list2->length){
 		if(list1->B[i] < list2->B[j])
 			list3->B[k++] = list1->B[i++];
 		else
 			list3->B[k++] = list2->B[j++];
 	}
  for(; i < list1->length; i++)
  list3->B[k++] = list1->B[i];
  for(; j < list2->length; j++)
  	list3->B[k++] = list2->B[j];
  list3->length = list1->length + list2->length;
  list3->size = 10;
 
 return list3;
}

List* List::Union(List *list1, List *list2) {

 int i, j, k;
 i = j = k = 0;

 List *list3 = new List();

 while (i < list1->length && j < list2->length) {
  if (list1->B[i]<list2->B[j])
   list3->B[k++] = list1->B[i++];
  else if (list2->B[j]<list1->B[i])
   list3->B[k++] = list2->B[j++];
  else {
   list3->B[k++] = list1->B[i++];
   j++;
  }
 }

 for (;i<list1->length;i++)
  list3->B[k++] = list1->B[i];
 for (;j<list2->length;j++)
  list3->B[k++] = list2->B[j];

 length = k;
 size = 10;

 return list3;
}

List* List::Intersection(List *list1, List *list2) {
 int i, j, k;
 i = j = k = 0;

 List *list3 = new List();

 while (i < list1->length && j < list2->length) {
  if (list1->B[i] < list2->B[j])
   i++;
  else if (list2->B[j] < list1->B[i])
   j++;
  else if (list1->B[i] == list2->B[j]) {
   list3->B[k++] = list1->B[i++];
   j++;
  }
 }

 length = k;
 size = 10;

 return list3;
}

List* List::Difference(List *list1, List *list2) {
 int i, j, k;
 i = j = k = 0;

 List *list3 = new List();

 while (i<list1->length && j<list2->length) {
  if (list1->B[i]<list2->B[j])
   list3->B[k++] = list1->B[i++];
  else if (list2->B[j]<list1->B[i])
   j++;
  else {
   i++;
   j++;
  }
 }

 for (;i<list1->length;i++)
  list3->B[k++] = list1->B[i];

 length = k;
 size = 10;

 return list3;
}

int main() {
 struct List list_1;
 int ch;
 int x, index;
 
 do{
  printf("\nMenu\n");
  printf("1. Insert\n");
  printf("2. Delete\n");
  printf("3. Search\n");
  printf("4. Sum\n");
  printf("5. Display\n");
  printf("6.Exit\n\n");
  
  printf("Enter your choice: ");
  scanf("%d", &ch);
  
  switch(ch){
   case 1: printf("Enter an element and index: ");
     scanf("%d%d", &x, &index);
     list_1.Insert(index, x);
     break;
   case 2: printf("Enter index: ");
     scanf("%d", &index);
     list_1.Delete(index);
     printf("Deleted element is %d\n", x);
     break;
   case 3: printf("Enter element to search: ");
     scanf("%d", &x);
     index = list_1.LinearSearch(x);
     printf("Element index is %d", index);
     break;
   case 4: printf("Sum is %d\n", list_1.Sum());
     break;
   case 5: list_1.Display();
  }
 }while (ch < 6);
 
 return 0;
}

In the next article, I am going to discuss Finding Single Missing Element in a Sorted Array in C with Examples. Here, in this article, I try to explain how to convert C language code to C++ code with Examples and I hope you enjoy this convert C language code to C++ code with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *