Tìm phần tử cuối cùng trong mảng lớn hơn -1

Yêu cầu:
– Cho mảng số thực
– Tìm phần tử cuối cùng trong mảng thỏa mãn lớn hơn -1. Nếu không tìm thấy, trả về 0.

Giải thuật:

Code

/************************************************************
* Author: VNCODING
* History
* 2016/12/24        first create    VNCODING
*************************************************************/

#include <stdio.h>
#include <conio.h>

#define MAX_SIZE_ARR 100

float get_last_element_greater_negative1(float arr[], int sz);

int main()
{
    float arr[MAX_SIZE_ARR];
    int n, i;
    float res;
    do
    {
        printf("n = ");
        scanf("%d", &n);
    }while(n <= 0 || n > 100);
    
    // Input array
    for(i = 0; i < n; i++)
    {
        printf("\narr[%d] = ", i);
        scanf("%f", &arr[i]);
    }
    res = get_last_element_greater_negative1(arr, n);
    if (res == 0)
    {
        printf("\nNot found!!!");
    }
    else
    {
        printf("\nThe last element is greater than -1 that is: %f", res);
    }
    getch();
    return 0;
}

float get_last_element_greater_negative1(float arr[], int sz)
{
    int i;
    float res = 0;
    for (int i = 0; i < sz; i++) 
    { 
        if (arr[i] > -1 && arr[0] < 0)
        {
            res = arr[i];
        }
    }
    return res;
}

Kết quả:

Tìm phần tử cuối cùng có giá trị lớn hơn -1
Tìm phần tử cuối cùng có giá trị lớn hơn -1

Be the first to comment

Leave a Reply