Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
#include<stdio.h>// de su dung printf va scanf #include<conio.h>// ket thuc ham bang getch(); #define MAX 100 // gia tri lon la 100 //___________________________________________________________________________________________// /* Su dung 2 thu vien chinh. */ //______________________________________________________________________// /* Khai bao danh sach struct */ //________________________________________________________________________// /* Khai bao chuong trinh con */ //===============================// // ham con nhap 1 mang //===== so luong phan tu mang =========// void nhapsoluongphantu( int &n ) { do{ printf("Moi ban nhap so luong phan tu cua mang, chu y khong am: "); scanf("%d", &n); } while (n<=0 || n>100); // nhap vao gia tri sai voi dieu kien se phai nhap lai } /*----------------------------------------- // ham nhap gia tri cho tung phan tu trong mang */ void nhapmang(int a[],int n) { for(int i=0;i<n;i++)/// khoi tao vong lap for voi i= 0 , vi mang bat dau tu thu 0 , i<n vi phan tu cuoi cung la bang n-1 { printf("a[%d]=",i); scanf("%d",&a[i]); } } /*----------------------------------------- // ham xuat cac phan tu cua mang ra man hinh */ void xuatmang(int a[],int n) { printf("\n Mang gom cac phan tu :\n"); for(int i=0;i<n;i++) /// khoi tao vong lap for voi i= 0 , vi mang bat dau tu thu 0 , i<n vi phan tu cuoi cung la bang n-1 { printf("%5d",a[i]); } } /*----------------------------------------- // ham tim vi tri phan tu duong dau tien */ int timduongdau(int a[],int n) { int flag = -1 ; for(int i=0;i<n;i++)/// khoi tao vong lap for voi i= 0 , vi mang bat dau tu thu 0 , i<n vi phan tu cuoi cung la bang n-1 { if(a[i]>0)// de kiem tra phan tu duong trong mang { flag =1; return 0; } } // ket thuc vong for ,vna khong thay doi flag, nghia la tim khong thay if(flag = -1) { return -1 ; } } /*----------------------------------------- // ham tim vi tri phan tu duong cuoi trong mang */ int PhanTuDuongCuoiCungTrongMang(int a[],int n) { int flag = -1; // flag xac dinh ton tai so duong trong mang hay ko?, hien tai la -1, ko biet. for( int i=n-1; i>=0;i-- ) // chay nguoc tu cuoi mang ve 0, cuoi mang la n-1, ve 0 nen >=0 { if( a[i]>0 ) // day la dieu kien phan tu duong { flag = 1; return i; } } //// ket thuc vong for, van ko thay doi flag, nghia la tim ko thay if( flag == -1 ) { return -1; } } //=============================================// int GiaTriLonNhatTrongMang( int a[], int n ) { int Max = a[0]; // gan thang a[0] la max, so sanh nhung vi tri con lai xem co phan tu nao lon hon Max ko? for( int i = 1; i < n ; i++ ) // chay tu vi tri 1, cho den n-1. ko xet vi tri 0 { if( a[i] > Max ) // neu no cao hon thang Max, thi no se danh vi tri Max { Max= a[i]; // chu mung dong chi a[i] da danh ngoi vi quan quan } } ////// ket thuc vong fot, da xac dinh dc Max return Max; } //=======Xuat vi tri Max, co the co nhieu vi tri Max======================// void ViTriMaxTrongMang( int a[], int n ) { int Max = GiaTriLonNhatTrongMang( a,n ); printf("\nNhung vi tri ma gia tri lon nhat trong mang: "); for( int i = 0; i < n; i++ ) // lan nay chay i tu 0 toi n-1, vi Max co the o vi tri bat ky trong mang. { if( a[i] == Max ) { printf("%5d",i+1); } } } int main() { int a[100];// Mang kieu so nguyen toi da 100 phan tu int n=0;//n la ten bien soluongphantu , so luong la so nguyen khoi tao bang 0 nhapsoluongphantu(n); nhapmang(a,n); xuatmang(a,n); int vitri=timduongdau(a,n); if(vitri==-1) { printf("\nKhong co phan tu duong trong mang!"); } else { printf("\nPhan tu duong dau tien trong mang la: %d dung o vi tri so %d",a[vitri], vitri+1 ); } int vitriduongcuoi = PhanTuDuongCuoiCungTrongMang (a,n); if( vitriduongcuoi == -1 ) { printf("\nKhong co phan tu duong trong mang!"); } else { printf("\nPhan tu duong cuoi cung trong mang la: %d dung o vi tri so %d", a[vitriduongcuoi], vitriduongcuoi+1 ); } ViTriMaxTrongMang(a,n); getch (); }
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत