Implementing Array as an Abstract

Implementing Array as an Abstract Data Type in java Language.

Understanding the Snippet Below:

  1. Defining a Structure:
    First, we define a structure. In c, you can use a class and its methods to achieve this. A structure is utilized to create customized data types.

  2. Define the Structure Elements:
    Inside the structure, we define integer variables total_size and used_size, along with an integer pointer that points to the address of the first element.

  3. Ready for Custom Data Type:
    With our customized data type established, we can define several functions, which will include:

    • Creating an array of this data type.
    • Printing the contents of this array.
    • Setting values in this array.

Create a void function createArray by passing the address of a struct data type a, and integers tSize and uSize. We can very easily assign this tSize and uSize given from the main, to the total_size and used_size of the struct myArray a by either of the methods defined below.

   (*a).total_size = tSize; 
               or 
    a->total_size = tSize;

Code Snippet 1: Syntax for assigning structure elements to structure pointers.

Similarly, assign the integer pointer ptr, the address of the reserved memory location using malloc. Do use the header file <stdlib.h> for using malloc.

a->ptr = (int *)malloc(tSize * sizeof(int));

Code Snippet 2: Using malloc

  1. Creating a Show Function:
    We will create a show function to display all the elements of the struct myArray. We will pass the address of the struct myArray a as an argument. To print all the elements, we will traverse through the entire struct and print each element until the iterator reaches the last element. We will use a->used_size to define the loop size, accessing each element with (a->ptr)[i].

  2. Creating a SetVal Function:
    Next, we will create a setVal function to set values in the struct myArray a. We will pass the address of the struct as an argument. Inside this function, we will use scanf to assign values to each element via (a->ptr)[i].

#include<stdio.h>
#include<stdlib.h>
 
struct myArray
{
    int total_size;
    int used_size;
    int *ptr;
};
 
void createArray(struct myArray * a, int tSize, int uSize){
    // (*a).total_size = tSize;
    // (*a).used_size = uSize;
    // (*a).ptr = (int *)malloc(tSize * sizeof(int));
 
    a->total_size = tSize;
    a->used_size = uSize;
    a->ptr = (int *)malloc(tSize * sizeof(int));
}
 
void show(struct myArray *a){
    for (int i = 0; i < a->used_size; i++)
    {
        printf("%d\n", (a->ptr)[i]);
    }
}
 
void setVal(struct myArray *a){
    int n;
    for (int i = 0; i < a->used_size; i++)
    {
        printf("Enter element %d", i);
        scanf("%d", &n);
        (a->ptr)[i] = n;
    }
    
}
 
int main(){
    struct myArray marks;
    createArray(&marks, 10, 2);
    printf("We are running setVal now\n");
    setVal(&marks);
 
    printf("We are running show now\n");
    show(&marks);
 
    return 0;
}

Code Snippet 3: A program to implement the ADT array

These were the basic methods we defined for this struct. We will check if they work by executing the program. First, we'll call the createArray and setVal functions to create an array of size 2 and assign some values to it. After that, we'll call the show function to verify that everything functions correctly.

Output of the above program:

Enter element 0 : 12
Enter element 1 : 13
We are running show now
12
13

try yourself 😉

#include<stdio.h>
#include<stdlib.h>

struct myArray
{
    int total_size;
    int used_size;
    int *ptr;
};

void createArray(struct myArray * a, int tSize, int uSize){
    // (*a).total_size = tSize;
    // (*a).used_size = uSize;
    // (*a).ptr = (int *)malloc(tSize * sizeof(int));

    a->total_size = tSize;
    a->used_size = uSize;
    a->ptr = (int *)malloc(tSize * sizeof(int));
}

void show(struct myArray *a){
    for (int i = 0; i < a->used_size; i++)
    {
        printf("%d\n", (a->ptr)[i]);
    }
}

void setVal(struct myArray *a){
    int n;
    for (int i = 0; i < a->used_size; i++)
    {
        printf("Enter element %d", i);
        scanf("%d", &n);
        (a->ptr)[i] = n;
    }
    
}

int main(){
    struct myArray marks;
    createArray(&marks, 10, 2);
    printf("We are running setVal now\n");
    setVal(&marks);

    printf("We are running show now\n");
    show(&marks);

    return 0;
}