Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. const int N=3, M=3;
  6.  
  7. void leggi(int matrice[N][M]);
  8. void matricecasuale(int matrice[N][M]);
  9. void stampa(int matrice[N][M]);
  10. void diagonale(int matrice [N][M]);
  11. void trasposta(int matrice[N][M], int trasposta[M][N]);
  12. void sostituzione(int matrice[N][M]);
  13.  
  14. int main()
  15. {
  16. int matrice[N][M], trasposta[M][N], scelta;
  17.  
  18. cout<<"- Premere 0 per uscire -" <<endl;
  19. cout<<"- Premere 1 per inserire manualmente i valori della matrice -" <<endl;
  20. cout<<"- Premere 2 per caricare casualmente la matrice -" <<endl;
  21. cout<<"- Premere 3 per stampare la matrice -" <<endl;
  22. cout<<"- Premere 4 per stampare la diagonale principale -" <<endl;
  23. cout<<"- Premere 5 per trasporre la matrice -" <<endl;
  24. cout<<"- Premere 6 per sostiruire tutti i valori maggiori di N con 100 -" <<endl;
  25. cin>>scelta;
  26.  
  27. do
  28. {
  29. switch (scelta)
  30. {
  31. case(0):
  32. cout<<"Grazie per avermi usato!" <<endl;
  33. break;
  34.  
  35. case(1):
  36. leggi(matrice);
  37. break;
  38.  
  39. case(2):
  40. matricecasuale(matrice);
  41. break;
  42.  
  43. case(3):
  44. stampa(matrice);
  45. break;
  46.  
  47. case(4):
  48. trasposta(matrice,trasposta);
  49. break;
  50.  
  51. case(5):
  52. diagonale(matrice);
  53. break;
  54.  
  55. case(6):
  56. sostituzione(matrice);
  57. break;
  58. }
  59. }while(scelta!=0);
  60.  
  61. return 0;
  62. }
  63.  
  64. void leggi(int matrice[N][M])
  65. {
  66. cout << "Inserisci i valori della matrice: " <<endl;
  67. for (int i=0; i<N; i++)
  68. {
  69. for(int j=0; j<M; j++)
  70. {
  71. cin >> matrice[i][j];
  72. }
  73. }
  74. }
  75.  
  76. void matricecasuale(int matrice[N][M])
  77. {
  78. srand(time(null));
  79. for (int i=0; i<N; i++)
  80. {
  81. for(int j=0; j<M; j++)
  82. {
  83. matrice[i][j] = rand % 20 + 1;
  84. }
  85. }
  86. }
  87.  
  88. void stampa(int matrice[N][M])
  89. {
  90. cout << "La matrice e': " <<endl;
  91. for (int i=0; i<N; i++)
  92. {
  93. for(int j=0; j<M; j++)
  94. {
  95. cout << matrice[i][j] <<" ";
  96. }
  97. cout <<endl;
  98. }
  99. }
  100.  
  101. void diagonale(int matrice [N][M])
  102. {
  103. for (int i=0;i<N;i++)
  104. {
  105. cout << matrice [i][i] << " ";
  106. }
  107. cout << endl;
  108. }
  109.  
  110. void trasposta(int matrice[N][M], int trasposta[M][N])
  111. {
  112. for (int i=0; i<N; i++)
  113. {
  114. for(int j=0; j<M; j++)
  115. {
  116. trasposta[j][i]=matrice[i][j];
  117. }
  118. }
  119. cout << "La matrice trasposta e': " <<endl;
  120. for(int i=0; i<M; i++)
  121. {
  122. for(int j=0; j<N; j++)
  123. {
  124. cout << trasposta[i][j] << " ";
  125. }
  126. cout <<endl;
  127. }
  128. }
  129.  
  130. void sostituzione(int m[N][M])
  131. {
  132. int numero;
  133. cout<<"Inserire un numero da cercare: ";
  134. cin>>numero;
  135. for (int i=0; i<N; i++)
  136. {
  137. for(int j=0; j<M; j++)
  138. {
  139. if (matrice[i][j] > numero)
  140. {
  141. matrice[i][j] = 100;
  142. }
  143. }
  144. }
  145. cout <<"La matrice aggiornata e' : " <<endl;
  146. for (int i=0; i<N; i++)
  147. {
  148. for(int j=0; j<M; j++)
  149. {
  150. cout << matrice[i][j] << " ";
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement