Advertisement
Mirbek

4Lab(2-режим)

Apr 12th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7. int main() {
  8.     setlocale(LC_CTYPE, "Russian");
  9.     srand(time(NULL));
  10.  
  11.     int i, j, M, N;
  12.     printf("Введите размер матрицы\n");
  13.     printf("M = ");
  14.     scanf("%d", &M);
  15.     printf("N = ");
  16.     scanf("%d", &N);
  17.  
  18.     double **ar;
  19.  
  20.     ar = (double **) malloc(M * sizeof(double *));
  21.  
  22.     for (i = 0; i < M; i++) {
  23.         ar[i] = (double *)malloc(N * sizeof(double));
  24.         for (j = 0; j < N; j++) {
  25.             ar[i][j] = 10 + rand() % (1000 * 90) / 1000.0;
  26.         }
  27.     }
  28.  
  29.     // Диапазон чисел
  30.     printf("\n");
  31.     printf("Диапазон случайных чисел равна от 10 до 100\n");
  32.  
  33.     // Вывод матрицы 10x10 в экран
  34.     for (i = 0; i < 10; i++) {
  35.         for (j = 0; j < 10; j++) {
  36.             printf("%5.2lf ", ar[i][j]);
  37.         }
  38.         printf("\n");
  39.     }
  40.  
  41.     // Запись двумерный массив в файл
  42.     FILE *fp;
  43.  
  44.     if ((fp = fopen("out.txt", "w")) == NULL) {
  45.         printf("Файл не найден");
  46.         return 0;
  47.     }
  48.  
  49.     for (i = 0; i < M; i++) {
  50.         for (j = 0; j < N; j++) {
  51.             fprintf(fp, "%5.2lf ", ar[i][j]);
  52.         }
  53.         putc('\n', fp);
  54.     }
  55.  
  56.     fclose(fp);
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement