Đếm Số Phần Tử X

Sign in Sign in Welcome!Log into your account your username your password Forgot your password? Password recovery Recover your password your email Search Saturday, February 14, 2026
  • Sign in / Join
Sign in Welcome! Log into your account your username your password Forgot your password? Get help Password recovery Recover your password your email A password will be e-mailed to you. sinhvientot.net sinhvientot.net sinhvientot.net sinhvientot.net Home Lập trình C/C++ Đếm số phần tử x Facebook Twitter Pinterest WhatsApp

Bài toán: Đếm số phần tử x xuất hiện trong mảng

Input:  mảng a có n phần tử

Xử lý:  gán biến đếm=0

Duyệt mảng, tìm các giá trị bằng x, mỗi lần tìm được x thì đếm+1

Output: đếm số phần tử bằng x

Hàm đếm số phần tử x

int DemPTu(int a[], int n, int x) { int i,dem=0; for( i=0;i<n;i++) if(a[i]==x) dem++; return dem; }

Chương trình

#include<stdio.h> #include<conio.h> #include <stdlib.h> #define MAX 100 void nhapmang(int a[], int &n); void xuatmang(int a[], int n); int DemPTu(int a[], int n, int x); void main() { int a[MAX],n,x; nhapmang(a,n); printf("\nNoi dung cua mang"); xuatmang(a,n); printf("\nNhap x: "); scanf("%d",&x); int kq=DemPTu(a,n,x); printf("\nSo lan phan tu x co trong mang: %d",kq); printf("\nChuc cac ban hoc tot"); getch(); } void nhapmang(int a[], int &n) { do { printf("\nSo phan tu trong mang "); scanf("%d",&n); }while(n<=0 && n>100); for(int i=0 ; i<n ; i++ ) { printf("\nSo phan tu a[%d] la: ",i); scanf("%d",&a[i]); } } void xuatmang(int a[], int n) { for(int i=0 ; i<n ; i++ ) printf("%4d",a[i]); } int DemPTu(int a[], int n, int x) { int i,dem=0; for( i=0;i<n;i++) if(a[i]==x) dem++; return dem; }

RELATED ARTICLESMORE FROM AUTHOR

C/C++

Sự khác nhau giữa Inline function và Macro trong C

C/C++

Trong ngôn ngữ C/C++ có bao nhiêu vùng nhớ (Memory layout)

C/C++

Cấu trúc dữ liệu danh sách nhân viên

C/C++

Tổng quan File trong C

C/C++

Cấu trúc kiểu dữ liệu sinh viên

C/C++

Cấu trúc mô tả một điểm trên tọa độ xOy

LEAVE A REPLY Cancel reply

Log in to leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Danh sách các bài học

Các kiểu dữ liệu cơ bản trong ngôn ngữ C/C++

Mr Good - April 16, 2016 0

Hướng dẫn Tạo Project Visual C++ trong Visual Studio 2012

April 16, 2016

Biến-Hằng-Câu lệnh và biểu thức trong C/C++

April 16, 2016

Cấu trúc IF-ELSE

April 16, 2016

Cấu trúc switch – case

April 16, 2016

Vòng lặp For

April 16, 2016

Cấu trúc While, Do-while

April 16, 2016

Cách sử dụng hàm trong lập trình

April 16, 2016

Mảng một chiều

April 16, 2016 Load more

Bài viết mới nhất

Download

Download Cisco Packet Tracer

Windows 10

Hướng dẫn cài đặt webserver trên localhost để chạy wordpress

HPE

Hướng dẫn cấu hình IP ILO máy chủ HP DL380 Gen10

CentOS

CentOS 8 – Giới thiệu về hệ điều hành Linux (P1)

Load more © Copyright 2016, All Rights Reserved. Donations are always appreciated! MEW: 0x296f1a39d5Ca3cb83C76724eA38af3B90B90109D MORE STORIES

Blockbuster vs Netflix lessons. Và … còn lại gì cho ta?

Mr Good - December 17, 2019 0

Quản trị viên hệ thống mạng là gì ? Làm sao để đạt được...

Duong Hien Vinh - July 12, 2016 4

Từ khóa » Hàm đếm Số Phần Tử Trong Mảng C