Advertisement
vqhien

Bt6 chương 6

Apr 16th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. // ConsoleApplication24.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. using namespace std;
  8. void Max(int &iMax, int a[], int n)
  9. {
  10.     iMax = a[1];
  11.     for (int i = 2; i <= n; i++)
  12.     {
  13.         if (iMax < a[i]) iMax = a[i];
  14.     }
  15. }
  16. void Min(int &iMin, int a[], int n)
  17. {
  18.     iMin = a[1];
  19.     for (int i = 2; i <= n; i++)
  20.     {
  21.         if (iMin > a[i]) iMin = a[i];
  22.     }
  23. }
  24. void Sum(int a[], int n, int &sum)
  25. {
  26.     sum = 0;
  27.     for (int i = 1; i <= n; i++)
  28.     {
  29.         sum += a[i];
  30.     }
  31. }
  32.  
  33. void main()
  34. {
  35.     int a[50], n, i, iMax, iMin, sum, pIck;
  36.     cout << "bam 1 de nhap mang: ";
  37.     cin >> pIck;
  38.     if (pIck == 1)
  39.     {
  40.  
  41.         cout << "nhap so luong phan tu cua mang: ";
  42.         cin >> n;
  43.         while (n <= 0)
  44.         {
  45.             cout << "nhap lai so luong phan tu cua mang: ";
  46.             cin >> n;
  47.         }
  48.         for (i = 1; i <= n; i++)
  49.         {
  50.             cout << "nhap phan tu cua mang: ";
  51.             cin >> a[i];
  52.         }
  53.         cout << "nhap 2 de tim phan tu lon nhat, 3 de tim phan tu nho nhat, 4 de tinh tong cac phan tu ";
  54.         cin >> pIck;
  55.         if (pIck == 2)
  56.         {
  57.             Max(iMax, a, n);
  58.             cout << iMax << endl;
  59.         }
  60.         if (pIck == 3)
  61.         {
  62.             Min(iMin, a, n);
  63.             cout << iMin << endl;
  64.         }
  65.         }
  66.         if (pIck == 4)
  67.         {
  68.  
  69.         Sum(a, n, sum);
  70.         cout << sum;
  71.         }
  72.  
  73.     _getch();
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement