Hi, I am

Ngô Tôn

I am a programmer.

Home / Programming / C/C++ / Xóa phần tử tại vị trí lẻ trong mảng

Xóa phần tử tại vị trí lẻ trong mảng

Yêu cầu:

– Xóa phần tử tại vị trí lẻ trong mảng.

Thuật toán:

– Giả sử xóa phần tử tại vị trí thứ i. Chúng ta thực hiện phép dồn các phần tử của mảng: A[i] = A[i+1].

Code:

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

void delArr(float A[], int* n);
void printArr(float A[], int n);

void main()
{
float A[] = {-9, 2.3, 0, 5, 4.5, -2, 3, 8, 10};
int n = sizeof(A)/sizeof(float);
printArr(A, n);
delArr(A, &n);
printArr(A, n);
getch();
}

void delArr(float A[], int* n)
{
int i, j;
for(i = *n - 1; i >= 0; i--)
{
if(i%2 != 0)
{
for(j = i; j < *n-1; j++)
{
A[j] = A[j+1];
}
(*n)--;
}
}
}

void printArr(float A[], int n)
{
int i;
printf("\n---------------------------");
for(i = 0; i < n; i++)
{
printf("\nA[%d] = %.3f", 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