You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
970 B

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stdio.h>
  4. using namespace std;
  5. void array_alloc(double **arr, long arrsize)
  6. {
  7. try
  8. {
  9. arr = new double* [arrsize];
  10. arr[0] = new double [arrsize*arrsize];
  11. for(long i = 1; i < arrsize; i++)
  12. arr[i] = arr[i-1] + arrsize;
  13. }
  14. catch (bad_alloc& ex)
  15. {
  16. cerr << "Could not allocate memory for array" << endl;
  17. exit(1);
  18. }
  19. }
  20. void array_destroy(double **arr)
  21. {
  22. delete [] arr[0];
  23. delete [] arr;
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. double **A=NULL;//, **B=NULL, **C=NULL;
  28. long rozmiar=0;
  29. char *endptr;
  30. if (argc < 3)
  31. {
  32. cerr << "Usage: " << argv[0] << " <n> <size>" << endl;
  33. exit(1);
  34. }
  35. rozmiar = strtol(argv[2], &endptr, 10);
  36. if (*endptr)
  37. {
  38. cerr << "Invalid array size format" << endl;
  39. exit(1);
  40. }
  41. if (rozmiar <= 0)
  42. {
  43. cerr << "The number of matrix dimension must be positive" << endl;
  44. exit(1);
  45. }
  46. array_alloc(A, rozmiar);
  47. getchar();
  48. //jeszcze czyszczenie pamięci
  49. exit(0);
  50. }