Advertisement
WadeRollins2710

[C++] [Array example] [FULL]

Mar 27th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 KB | None | 0 0
  1. // Array.cpp : Defines the entry point for the console application.
  2. //Bài tập mảng
  3. //Oh vl viết đc Tiếng Việt này!!! :D
  4. //Tiếng Vieetjtttttttttttt!!!!
  5. //Vietnamese!!!!!
  6. //Ờ tất nhiên rồi sao không viết đc chứ??? :v
  7.  
  8. //Introduction
  9. //This use windows.h, so it is only available on Visual Studio
  10. //Cannot run on Turbo C++
  11.  
  12. #include "stdafx.h"
  13. #include "stdio.h"
  14. #include "conio.h"
  15. #include "windows.h"
  16. #include "WinDef.h"
  17. #include "stdlib.h"
  18.  
  19. int A[100], n, i, j, d, dem, F[100], Back_up[100], Backup_N;
  20.  
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23.     return 0;
  24. }
  25.  
  26. void Input()
  27. {
  28.     system("cls"); system("color 0f");
  29.     printf("Input the number of the element of the array: "); scanf_s("%d", &n);
  30.     while ((n < 1) || (n > 100))
  31.     {
  32.         printf("Input again: "); scanf_s("%d", &n);
  33.     }
  34.     for (i = 1; i <= n; i++)
  35.     {
  36.         printf("Input the element number %d: ", i); scanf_s("%d", &A[i]);
  37.     }
  38.     system("cls"); Sleep(1000); system("color 0e");
  39.     for (i = 1; i <= n; i++)
  40.         printf("%d ", A[i]);
  41.     Sleep(1000); system("color 0f");  printf("\n");
  42.     for (i = 1; i <= n; i++)
  43.         Back_up[i] = A[i];
  44.     Backup_N = n;
  45. }
  46.  
  47. void Re_order()
  48. {
  49.     int tg;
  50.     printf("Array in increasing order: ");
  51.     for (i = 1; i < n; i++)
  52.         for (j = i + 1; j <= n; j++)
  53.             if (A[i] >= A[j])
  54.             {
  55.                 tg = A[i]; A[i] = A[j]; A[j] = tg;
  56.             }
  57.     for (i = 1; i <= n; i++)
  58.         printf("%d ", A[i]);
  59.     printf("\n"); Sleep(1000);
  60.     printf("Array in dereasing order: ");
  61.     for (i = 1; i < n; i++)
  62.         for (j = i+1; j <= n; j++)
  63.             if (A[i] <= A[j])
  64.             {
  65.                 tg = A[i]; A[i] = A[j]; A[j] = tg;
  66.             }
  67.     for (i = 1; i <= n; i++)
  68.         printf("%d ", A[i]);
  69.     printf("\n"); Sleep(1000);
  70. }
  71.  
  72. int Nt(int m)
  73. {
  74.     int i, dem = 0;
  75.     for (i = 1; i <= m; i++)
  76.         if (m%i == 0) dem++;
  77.     if (dem == 2) return 1;
  78.     else return 0;
  79. }
  80.  
  81. int Cp(int m)
  82. {
  83.     int i; d = 0;
  84.     for (i = 1; i < m; i++)
  85.         if (i*i == m) d++;
  86.     if (d != 0) return 1;
  87.     else return 0;
  88. }
  89.  
  90. int Hh(int m)
  91. {
  92.     int S = 0, i;
  93.     for (i = 1; i < m; i++)
  94.         if (m%i == 0) S = S + i;
  95.     if (S == m) return 1;
  96.     else return 0;
  97. }
  98.  
  99. void Find_Number()
  100. {
  101.     dem = 0;
  102.     printf("Prime number: ");
  103.     for (i = 1; i <= n; i++)
  104.         if (Nt(A[i]) == 1)
  105.         {
  106.             printf("%d ", A[i]); dem++;
  107.         }
  108.     if (dem == 0) printf("None!");
  109.     printf("\n"); dem = 0; Sleep(500);
  110.     printf("Square number: ");
  111.     for (i = 1; i <= n; i++)
  112.         if (Cp(A[i]) == 1)
  113.         {
  114.             printf("%d ", A[i]); dem++;
  115.         }
  116.     if (dem == 0) printf("None!");
  117.     printf("\n"); system("color 0f"); dem = 0; Sleep(500);
  118.     printf("Perfect number: ");
  119.     for (i = 1; i <= n; i++)
  120.         if (Hh(A[i]) == 1)
  121.         {
  122.             printf("%d ", A[i]); dem++;
  123.         }
  124.     if (dem == 0) printf("None!");
  125.     printf("\n");  Sleep(500);
  126. }
  127.  
  128. void Count_Value()
  129. {
  130.     int d, dem = 0;
  131.     for (i = A[n]; i <= A[1]; i++)
  132.     {
  133.         d = 0;
  134.         for (j = 1; j <= n; j++)
  135.             if (A[j] == i) d++;
  136.         if (d != 0) dem++;
  137.     }
  138.     printf("There are %d different value in the array \n",dem);
  139. }
  140.  
  141. void Insert()
  142. {
  143.     int x, pos;
  144.     printf("Input the position you want to insert number: "); scanf_s("%d", &pos);
  145.     while ( (pos<1) || (pos>n) )
  146.     {
  147.         printf("Position out of range, "); Sleep(1000);
  148.         printf("Input again: "); scanf_s("%d", &pos);
  149.     }
  150.     printf("Input the number you want to insert: "); scanf_s("%d", &x); n++;
  151.     while ((x < -32768) || (x>32767))
  152.     {
  153.         printf("Number is out of Integer range, "); Sleep(1000);
  154.         printf("Input again: "); scanf_s("%d", &x);
  155.     }
  156.     for (i = n; i >= pos; i--)
  157.         A[i + 1] = A[i];
  158.     A[pos] = x;
  159.     printf("Inserted Array: ");
  160.     for (i = 1; i <= n; i++)
  161.         printf("%d ", A[i]);
  162.     printf("\n"); Sleep(1000);
  163. }
  164.  
  165. void Delete()
  166. {
  167.     int pos;
  168.     printf("Input the position you want to delete: ");
  169.     scanf_s("%d", &pos);
  170.     while ((pos<1) || (pos>n))
  171.     {
  172.         printf("Position out of range, "); Sleep(1000);
  173.         printf("Input again: "); scanf_s("%d", &pos);
  174.     }
  175.     n--;
  176.     for (i = pos; i <= n; i++)
  177.         A[i] = A[i + 1];
  178.     printf("Deleted Array: ");
  179.     for (i = 1; i <= n; i++)
  180.         printf("%d ", A[i]);
  181.     printf("\n");
  182.     Sleep(1000);
  183. }
  184.  
  185. int F_num_1(int i)
  186. {
  187.     int S = 0, j, boolean=0;
  188.     for (j = 1; j <= i; j++)
  189.     {
  190.         S = S + j;
  191.         if (S == i) return 1;
  192.     }
  193.     return boolean;
  194. }
  195.  
  196. void Find_Number_2()
  197. {
  198.     int d = 0;
  199.     printf("The number which is the total of some first natural number: ");
  200.     for (i = 1; i <= n; i++)
  201.         if (F_num_1(A[i]) == 1)
  202.         {
  203.             d++;
  204.             printf("%d ", A[i]);
  205.         }
  206.     if (d == 0) printf("None!"); Sleep(1000);
  207.     printf("\n");
  208. }
  209.  
  210. int F_num_2(int i)
  211. {
  212.     int S = 1, j, boolean = 0;
  213.     for (j = 1; j <= i; j++)
  214.     {
  215.         S = S * j;
  216.         if (S == i) return 1;
  217.     }
  218.     return boolean;
  219. }
  220.  
  221. void Find_Number_3()
  222. {
  223.     int d = 0;
  224.     printf("The number which is the integration of some fisrt natural number: ");
  225.     for (i = 1; i <= n; i++)
  226.         if (F_num_2(A[i]) == 1)
  227.         {
  228.             d++;
  229.             printf("%d ", A[i]);
  230.         }
  231.     if (d == 0) printf("None!"); Sleep(1000);
  232.     printf("\n");
  233. }
  234.  
  235. int Palindrome(int i)
  236. {
  237.     int a=10, c=0, x, l;
  238.     char S[256];
  239.     _itoa_s(i, S, a); l = strlen(S);
  240.     for (x = 0; x <= (l / 2) -1 ; x++)
  241.         if (S[x] == S[l - (x + 1)]) c++;
  242.     if (c == l / 2) return 1;
  243.     else return 0;
  244. }
  245.  
  246. void F_Palind()
  247. {
  248.     int count = 0;
  249.     printf("Palindrome number: ");
  250.     for (i = 1; i <= n; i++)
  251.         if (Palindrome(A[i]) == 1)
  252.         {
  253.             count++;
  254.             printf("%d ", A[i]);
  255.         }
  256.     if (count == 0) printf("None!");
  257.     printf("\n"); Sleep(1000);
  258. }
  259.  
  260. int Fmod(int j, int i)
  261. {
  262.     if (j%i == 0) return 1;
  263.     else return 0;
  264. }
  265.  
  266. void Mod_Num()
  267. {
  268.     int count = 0;
  269.     printf("Pair(s) of multiples-divisor number: ");
  270.     for (i = 1; i < n; i++)
  271.         for (j = i + 1; j <= n; j++)
  272.             if (Fmod(A[i], A[j]) == 1)
  273.             {
  274.                 count++;
  275.                 printf("%d-%d ", A[i], A[j]);
  276.             }
  277.     if (count == 0) printf("None!"); printf("\n");
  278.     Sleep(1000);
  279. }
  280.  
  281. void Swap(int i, int j)
  282. {
  283.     int middle;
  284.     middle = A[i]; A[i] = A[j]; A[j] = middle;
  285. }
  286.  
  287. void Re_Order_Odd( )
  288. {
  289.     int pos = 1;
  290.     for (i = 1; i <= n; i++)
  291.         if (A[i] % 2 == 0)
  292.         {
  293.             Swap(i, pos);
  294.             pos++;
  295.         }
  296.     printf("Re_ordered array: ");
  297.     for (i = 1; i <= n; i++)
  298.         printf("%d ", A[i]);
  299.     printf("\n"); Sleep(1000);
  300. }
  301.  
  302. void Make_Finbonaci()
  303. {
  304.     F[1] = 1; F[2] = 1;
  305.     for (i = 3; i <= n; i++)
  306.         F[i] = F[i - 1] + F[i - 2];
  307.     printf("Finbonaci array: ");
  308.     for (i = 1; i <= n; i++)
  309.         printf("%d ", F[i]);
  310.     printf("\n"); Sleep(1000);
  311. }
  312.  
  313. void Terminate(int i)
  314. {
  315.     int k;
  316.     n--;
  317.     for (k = i; k <= n; k++)
  318.         A[k] = A[k + 1];
  319. }
  320.  
  321. void Terminate_F(int i)
  322. {
  323.     int k;
  324.     Backup_N--;
  325.     for (k = i; k <= Backup_N; k++)
  326.         F[k] = F[k + 1];
  327. }
  328.  
  329. void Delete_Same()
  330. {
  331.     for (i = 1; i < n; i++)
  332.         for (j = i + 1; j <= n; j++)
  333.             if (A[i] == A[j])
  334.                 Terminate(i);
  335.     for (i = 1; i < Backup_N; i++)
  336.         for (j = i + 1; j <= n; j++)
  337.             if (F[i] == F[j])
  338.                 Terminate_F(i);
  339. }
  340.  
  341. void The_same_num()
  342. {
  343.     int count, main_Count=0;
  344.     for (i = 1; i <= n; i++)
  345.     {
  346.         count = 0;
  347.         for (j = 1; j <= n; j++)
  348.             if (F[i] == A[j]) count++;
  349.         if (count != 0) main_Count++;
  350.     }
  351.     printf("Finbonaci array and A array has %d same number(s)\n", main_Count);
  352.     Sleep(1000);
  353. }
  354.  
  355. void Mod_Array()
  356. {
  357.     int y, Mod[100], tg;
  358.     for (i = 1; i <= n; i++)
  359.         A[i] = Back_up[i];
  360.     printf("Input number to mod: "); scanf_s("%d", &y);
  361.     for (i = 1; i <= n; i++)
  362.         Mod[i] = A[i] % y;
  363.     printf("Array in increasing order based by the array moded to %d: ", y);
  364.     for (i = 1; i < n; i++)
  365.         for (j = i + 1; j <= n; j++)
  366.             if (Mod[i] >= Mod[j])
  367.             {
  368.                 tg = A[i]; A[i] = A[j]; A[j] = tg;
  369.                 tg = Mod[i]; Mod[i] = Mod[j]; Mod[j] = tg;
  370.             }
  371.     for (i = 1; i <= n; i++)
  372.         printf("%d ", A[i]);
  373.     printf("\n"); Sleep(1000);
  374.     printf("Array in dereasing order based by the array moded to %d: ", y);
  375.     for (i = 1; i < n; i++)
  376.         for (j = i + 1; j <= n; j++)
  377.             if (Mod[i] <= Mod[j])
  378.             {
  379.                 tg = A[i]; A[i] = A[j]; A[j] = tg;
  380.                 tg = Mod[i]; Mod[i] = Mod[j]; Mod[j] = tg;
  381.             }
  382.     for (i = 1; i <= n; i++)
  383.         printf("%d ", A[i]);
  384.     printf("\n"); Sleep(1000);
  385. }
  386.  
  387. void main()
  388. {
  389.     Input();
  390.     Re_order();
  391.     Find_Number();
  392.     Count_Value();
  393.     Insert();
  394.     Delete();
  395.     Find_Number_2();
  396.     Find_Number_3();
  397.     F_Palind();
  398.     Mod_Num();
  399.     Re_Order_Odd();
  400.     Make_Finbonaci();
  401.     Delete_Same();
  402.     The_same_num();
  403.     Mod_Array();
  404.     _getch();
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement