Hi, I am

Ngô Tôn

I am a programmer.

Home / Programming / C/C++ / Xóa tất cả các phần tử nhỏ hơn X

Xóa tất cả các phần tử nhỏ hơn X

Yêu cầu:

– Nhập vào giá trị X. Viết hàm xoá tất cả các phần tử có giá trị nhỏ hơn X.

Thuật toán:

– Kiểm tra điều kiện: A[i] < x và xóa phần tử A[i].

Code:

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

void print_arr(int A[], int n);
void delete_below_x(int A[], int* n, int x);

void main()
{
int A[] = {1, 4, -9, 0, 7, 11, 8, 4};
int n = sizeof(A)/sizeof(int);
print_arr(A, n);
delete_below_x(A, &n, 7);
print_arr(A, n);
getch();
}

void delete_below_x(int A[], int* n, int x)
{
int i, j;
for(i = *n - 1; i >= 0; i--)
{
if(A[i] < x)
{
for(j = i; j < *n - 1; j++)
{
A[j] = A[j+1];
}
(*n)--;
}
}

}

void print_arr(int A[], int n)
{
int i;
printf("\n=========================");
for(i = 0; i < n; i++)
{
printf("\nA[%d] = %d", i, A[i]);
}
printf("\n=========================");
}

Kết quả:

About ngoton

Ngô Tôn is a programmer with passion for tailored software solutions. Comes with 7+ years of IT experience, to execute beautiful front-end experiences with secure and robust back-end solutions.

Check Also

Xây dựng hàm bạn để tính diện tích hình chữ nhật

Yêu cầu: Xây dựng hàm bạn để tính diện tích hình chữ nhật Code: Giải …

Leave a Reply

avatar
  Subscribe  
Notify of