lundi 1 mars 2021

Why does my Do-While loop keep breaking when I want it to return back to the Menu

I want my do-while loop to keep repeating when I enter a number for a case. For Instance, If I wanted to Insert an element in an array I would enter 1, and then I would enter the element and index of where I want it to be inserted. It then should display the menu again, but for some reason, it just breaks out of the do-while loop?

#include <iostream>
using namespace std;

struct Array
{
    int *A;
    int size;
    int length;
};

void display(struct Array *arr)
{
    int i;
    printf("\nElements: ");
    for(i = 0; i < arr->length; i++)
        printf("%d ", arr->A[i]);
};

struct Array *mergeArrays(struct Array *array1, struct Array *array2)
{
    int i = 0;
    int j = 0;
    int k = 0;

    struct Array *array3;

    int length = array1->length + array2->length;
    array3 = new Array[length];

    while(i < array1->length && j < array2->length)
    {
        if(array1->A[i] < array2->A[j])
        {   
            array3->A[k++] = array1->A[i++];
        }
        else
        {
            array3->A[k++] = array2->A[j++];
        }
    }

    for(; i < array1->length; i++)
        array3->A[k++] = array1->A[i];
    for(; j < array2->length; j++)
        array3->A[k++] = array2->A[j];

    array3->length = length;

    return array3;
};

struct Array *Union(struct Array *array1, struct Array *array2)
{
    int i = 0;
    int j = 0;
    int k = 0;

    struct Array *array3;

    int length = array1->length + array2->length;
    array3 = new Array[length];

    while(i < array1->length && j < array2->length)
    {
        if(array1->A[i] < array2->A[j]) {   
            array3->A[k++] = array1->A[i++];
        }
        else if(array2->A[j] < array1->A[i]){
            array3->A[k++] = array2->A[j++];
        } else {
            array3->A[k++] = array1->A[i++];
            j++;
        }
    }

    for(; i < array1->length; i++)
        array3->A[k++] = array1->A[i];

    array3->length = k;
    array3->size = 10;

    return array3;
};

struct Array *Intersection(struct Array *array1, struct Array *array2)
{
    int i = 0;
    int j = 0;
    int k = 0;

    struct Array *array3;

    int length = array1->length + array2->length;
    array3 = new Array[length];

    while(i < array1->length && j < array2->length)
    {
        if(array1->A[i] < array2->A[j]) {
            i++;
        } else if(array2->A[j] < array1->A[i]) {
            j++;
        } else {
            array3->A[k++] = array1->A[i++];
            j++;
        }
    }

    array3->length = k;
    array3->size = 10;

    return array3;
};

struct Array *Difference(struct Array *array1, struct Array *array2)
{
    int i = 0;
    int j = 0;
    int k = 0;

    struct Array *array3;

    int length = array1->length + array2->length;
    array3 = new Array[length];

    while(i < array1->length && j < array2->length)
    {
        if(array1->A[i] < array2->A[j]) {   
            array3->A[k++] = array1->A[i++];
        }
        else if(array2->A[j] < array1->A[i]){
            j++;
        } else {
            i++;
            j++;
        }
    }

    for(; i < array1->length; i++)
        array3->A[k++] = array1->A[i];

    array3->length = k;
    array3->size = 10;

    return array3;
};

int Delete(struct Array *array1, int index)
{
    int x = 0;
    int i = 0;

    if(index >= 0 && index < array1->length)
    {
        x = array1->A[index];
        for(i = index; i < array1->length-1; i++)
            array1->A[i] = array1->A[i+1];
        array1->length--;
    }
    return x;
};

void Append(struct Array *array1, int value)
{  
    if(array1->length < array1->size)
        array1->A[array1->length++] = value;
};

void Insert(struct Array *array1, int index, int element)
{   
    int i;
    if(index >= 0 && index <= array1->length)
    {
        for(i = array1->length; i > index; i--)
            array1->A[i] = array1->A[i-1];

        array1->A[index] = element;
        array1->length++;
        
    }
}

bool Search(struct Array *array1, int element)
{
    for(size_t i{0}; i < array1->length; i++) {
        if(array1->A[i] == element)
            return true;
    }
    return false;
};

void Sum(struct Array array1)
{
    int arraySum = 0;

    for(int i{0}; i < array1.length; i++)
    {
        arraySum+=array1.A[i];
    }

    cout << arraySum << endl;
}


int main()
{
    int index;
    int x;
    int ch;

    struct Array array1;
    printf("Enter Size Of Array: ");
    scanf("%d", &array1.size);
    array1.A=(int *)malloc(array1.size*sizeof(int));

    do
    {
    printf("Display Menu\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");

    printf("Enter Your Choice: ");
    scanf("%d",&ch);

    switch(ch)
    {
        case 1: printf("Enter Element First And Then Index: ");
            scanf("%d%d", &x, &index);
            Insert(&array1,index, x);
            break;

        case 2: printf("Enter Index To Be Deleted: ");
            scanf("%d",&index);
            x = Delete(&array1, index);
            printf("Deleted Element is %d|n", x);
            break;

        case 3: printf("Enter Element To Be Searched: ");
            scanf("%d", x);
            x = Search(&array1, x);
            break;

        case 4: printf("Array Sum: ");
            Sum(array1);
            break;
        
        case 5: display(&array1);
    }
    } while(ch < 6);
      return 0;
}

Aucun commentaire:

Enregistrer un commentaire