lundi 9 septembre 2019

How to fix crashes if statement in c

T have N input for array inside main int(). After assign the input you need to check is the input of the array static or not. My idea is to make function to get IsMonoStatic(T) return boolean and 3 another function. I'll show them below. If the function return true then I must print something in terminal somehow it just working for IsMonoStatic(T), when I try input that return true for IsMonoNotSmaller(T) after assigning the input the terminal crashing literally not printing the message and the terminal is just freezing, I need to use ctrl + c in order to get out of my executing program.

First condition is working at terminal when IsMonoStatic(T) return true

4
1
1
1
1
Array monotonik statik

But when i try another input, the program is crash.

Here is my code program.

#include <stdio.h>
#include "array.h"
#include "boolean.h"

boolean IsMonoNotSmaller (TabInt T)
{
    // kamus lokal
    IdxType i; boolean cek;
    // algoritma
    i = GetFirstIdx(T);
    cek = true; // masih dalam kondisi cek
    while ((i<GetLastIdx(T))&&(cek==true)){
        if (T.TI[i]<=T.TI[i+1]){
            cek = true; // benar maka cek lagi
            i++;
        } else{
            cek = false; // berhenti loop
        }
    }
    return cek;
}

boolean IsMonoNotGreater (TabInt T)
{
    // kamus lokal
    IdxType i; boolean cek;
    // algoritma
    i = GetFirstIdx(T);
    cek = true;
    while ((i<GetLastIdx(T))&&(cek==true)){
        if (T.TI[i]>=T.TI[i+1]){
            cek = true; // benar maka cek lagi
            i++;
        } else{
            cek = false; // berhenti loop
        }
    }
    return cek;
}

boolean IsMonoStatic (TabInt T)
{
    // kamus lokal
    IdxType i;
    boolean cek;
    // algoritma
    i = GetFirstIdx(T);
    cek = true; // tetap ngecek
    while ((i<GetLastIdx(T))&&(cek=true)){
        if (T.TI[i]==T.TI[i+1]){
            cek = true;// belum ditemukan & ttp lanjut pncarian
            i++;
        } else{ // != berhenti cek
            cek = false;
        }
    }
    return cek;
}

int main(){
    /* kamus lokal */
    TabInt T;

    /* algoritma */
    // read input
    BacaIsi(&T);

    if (IsMonoStatic(T)==true){
        printf("Array monotonik statik\n");
    } else {
        if (IsMonoNotSmaller(T))
            printf("Array monotonik tidak mengecil\n");
        if (IsMonoNotGreater(T))
            printf("Array monotonik tidak membesar\n");
        printf("Array tidak monotonik\n");
    }

    return 0;
}


I expect the output of

4
4
4
3
2
Array monotonik tidak membesar

But the output is not working here

4
4
4
3
2
 // the program crash here, I need to use ctrl+c to quit this program

Aucun commentaire:

Enregistrer un commentaire