Code:
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
const int max_a=100;
void input(int a[], int n){ /* Hàm nhập mảng 1 chiều */
for(int i=0;i<n;i++){
printf("a[%d] = ", i);
scanf(" %d", &a[i]);
}
}
void output(int a[], int n){ /* Hàm xuất mảng 1 chiều */
for(int i=0;i<n;i++)
printf("\n\ta[%d] = %d", i, a[i]);
}
void swap(int *a, int *b){ /* Hàm đổi vị trí a[j] , a[j+1]của mảng 1 chiều */
int temp=*a; *a=*b; *b=temp; /* Đoạn này đã đc @MHoang hỗ trợ */
}
void bubblesort(int a[], int n){ /* Sắp xết tăng dần theo bubble */
int i,j;
bool test=false;
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
test=true;
}
if(test==false) return;
}
}
bool odd_check(int x){ /* check số lẻ */
if(x%2==0) return false;
return true;
}
int odd_sum_avg_at_even_pos(int a[], int n){ /*tính tổng số lẻ ở vị trí chẵn, đoạn này mình biết cách làm bằng while nhưng thích for hơn, ko hiểu sao có cảm tình với for */
int sum=0;
for(int i=0;i<n;){
if(odd_check(a[i])==true) sum+=a[i]; i+=2;
}
return sum;
}
int max_find(int a[], int n){ /* tìm giá trị lớn nhất trong mảng 1 chiều */
int max=a[0];
for(int i=1;i<n;i++){
if(a[i]>max) max=a[i];
}
return max;
}
int min_find(int a[], int n){ /* tìm giá trị nhỏ nhất trong mảng 1 chiều */
int min=a[0];
for(int i=1;i<n;i++)
if(a[i]<min) min=a[i];
return min;
}
void min_pos_find(int a[], int n){ /* tìm vị trí của các số nhỏ nhất */
int min=min_find(a,n);
printf("\n\n\tMang a[n] co min = %d tai vi tri :", min);
for(int i=0;i<n;i++)
if(a[i]==min) printf("\ta[%d]", i);
}
bool square_num_check(int x){ /* Kiểm tra số chính phuơng */
int temp = sqrt(x);
return temp * temp == x; /* Đoạn này đã đc @Khoaph hỗ trợ */
}
int square_num_count(int a[], int n){ /* Đếm số lượng số chính phương */
int count=0;
for(int i=0;i<n;i++){
if(square_num_check(a[i])==true) count++;
}
return count;
}
bool prime_num_check(int x){ /* Kiểm tra số nguyên tố */
if(x<2) return false;
int count=0;
for(int i=2;i<=sqrt(x);i++) /* Đoạn này đã đc @Khoaph hỗ trợ */
if(x%i==0) count++;
if(count==1) return true;
else return false;
}
void output_prime_num(int a[], int n){ /* Xuất số nguyên tố trong mảng */
printf("\nCac so nguyen to co trong mang la: ");
int count=0;
for(int i=0;i<n;i++){
if(prime_num_check(a[i])==true){
printf("%d ", a[i]); count++;
}
}
if(count==0) printf("Khong co so nguyen to");
}
void delete_negative(int a[], int &n){ /* Xóa các số âm khỏi mảng */
for(int i=0;i<n;i++){
if(a[i]<0){
for(int j=i;j<n-1;j++){
a[j]=a[j+1];
}
n--; i--;
}
}
}
void replace_negative(int a[], int n){ /* Thay thế số âm = 0 tại mảng */
for(int i=0;i<n;i++)
if(a[i]<0) a[i]=0;
}
int main(){
int a[max_a], n, x;
printf("Hay nhap so phan tu mang n = ");
scanf(" %d", &n);
input(a,n);
do{
system("cls");
printf("Hay chon cong viec: ");
printf("\n1. Tinh trung binh cong cac so le o vi tri chan");
printf("\n2. Tim so lon nhat trong mang vua nhap");
printf("\n3. Tim vi tri cac so nho nhat trong mang");
printf("\n4. Dem cac so chinh phuong trong mang");
printf("\n5. Hien thi cac so nguyen to co trong mang len man hinh");
printf("\n6. Thay the cac gia tri am trong mang = 0");
printf("\n7. Xoa cac phan tu am trong mang");
printf("\n8. Sap xep mang theo thu tu tang dan\n");
scanf(" %d", &x);
}while(x<1 || x>8);
switch(x){
case 1: system("cls"); printf("Trung binh cong cac so le o vi tri chan = %d", odd_sum_avg_at_even_pos(a,n));
break;
case 2: system("cls"); printf("So lon nhat trong mang vua nhap = %d", max_find(a,n)); break;
case 3: system("cls"); min_pos_find(a,n); break;
case 4: system("cls"); printf("Dem cac so chinh phuong trong mang = %d", square_num_count(a,n)); break;
case 5: system("cls"); output_prime_num(a,n); break;
case 6: system("cls"); replace_negative(a,n); output(a,n); break;
case 7: system("cls"); delete_negative(a,n); output(a,n); break;
default: bubblesort(a,n); output(a,n);
}
return 0;
}