Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <fstream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. srand(time(NULL));
  12. int x, n, m, y;
  13. string nazwa_pliku;
  14. int **tab;
  15. cout << "Podaj wymiary macierzy ( wiersz , kolumny ) " << endl;
  16. cin >> m >> n;
  17. cout << "Podaj nazwe pliku w ktorym zapisana zostanie macierz ( bez .txt )" << endl;
  18. cin >> nazwa_pliku;
  19. nazwa_pliku = nazwa_pliku + ".txt";
  20. ofstream plik(nazwa_pliku.c_str());
  21. if (!plik) {
  22. cout << "Nie udalo sie otworzyc pliku " << nazwa_pliku << endl;
  23. }
  24. else {
  25. plik << m << " " << n << endl;
  26. tab = (int**)malloc(m * sizeof(int*));
  27. for (x = 0; x < m; x++) {
  28. tab[x] = (int*)malloc(n * sizeof(int));
  29. }
  30.  
  31. for (x = 0; x < m; x++) {
  32. for (y = 0; y < n; y++) {
  33. tab[x][y] = (rand() % 21) - 10;
  34. // cout << tab[x][y] << " ";
  35. plik << tab[x][y];
  36. if (y < n - 1)plik << " ";
  37. }
  38. if (x < m - 1)plik << endl;
  39. // cout << endl;
  40. }
  41. cout << endl << "Macierz " << m << " x " << n << " wygenerowana losowymi liczbami z przedzialu -10 do 10" << endl << endl;
  42. for (x = 0; x < m; x++) {
  43. free(tab[x]);
  44. }
  45. free(tab);
  46. }
  47. plik.close();
  48.  
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement