J00ker

(9-25)Fisa de lucru

Sep 25th, 2014
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int a[1000], n, b[1000], m;
  6.  
  7. //1.Citirea vect.
  8. void Read(int t[], int &n)
  9. {
  10. cin >> n;
  11. for(int i = 0; i < n; i++)
  12. {
  13. cout << "[" << i << "] = ";
  14. cin >> t[i];
  15. }
  16. cout << "\n";
  17. }
  18.  
  19. //2.Ordonarea vect.
  20. void Desc(int a[], int n)
  21. {
  22. for(int i = 0; i < n-1; i++)
  23. for(int j = i+1; j < n; j++)
  24. if(a[i] < a[j])
  25. {
  26. int aux = a[i];
  27. a[i] = a[j];
  28. a[j] = aux;
  29. }
  30. }
  31.  
  32. //3.Afisarea vect.
  33. void Print(int a[], int n)
  34. {
  35. for(int i = 0; i < n; i++)
  36. cout << a[i] << " ";
  37. cout << "\n";
  38. }
  39.  
  40. //4.Afisarea elementelor comune
  41. void Comun(int a[], int n, int b[], int m)
  42. {
  43. int i = 0, j = 0, k;
  44. for(k = 0; (i < n)&&(j < m); k++)
  45. {
  46. if(a[i] == b[j])
  47. {
  48. cout << a[i] << " ";
  49. i++;
  50. j++;
  51. }
  52. else if(a[i] > b[j])
  53. i++;
  54. else j++;
  55. }
  56. }
  57.  
  58. //*.Suma elementelor
  59. int Suma(int a[], int n)
  60. {
  61. int s = 0;
  62. for(int i = 0; i < n; i++)
  63. s += a[i];
  64. return s;
  65. }
  66.  
  67. int main()
  68. {
  69. Read(a, n);
  70. Read(b, m);
  71.  
  72. Desc(a, n);
  73. Desc(b, m);
  74.  
  75. Print(a, n);
  76. Print (b, m);
  77.  
  78. Comun(a, n, b, m);
  79.  
  80. cout << Suma(a, n);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment