Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Для новичков > Масив С++


Автор: Merroy 18.12.2018, 11:04
Сформировать двумерный массив и выполнить вычисления.

Массив и результат записать в текстовый файл. Прочитать файл на экран.

Имя масс. - Y
Размер Стр., Столб. - 5,4
Содержание задания - Найти количество элементов массива Y б больших 8.
Тип данных - целые.

Помогите пожалуйста исправить код или написать новый. 

Код

#include "stdafx.h"
#include <iostream>
#include <random>
 
int main() {
 
    constexpr std::size_t rows = 5, cols = 4;
    constexpr int value = 8;
 
    int matrix[rows][cols];
 
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(-50, 50);
 
    std::size_t count = 0;
 
    std::cout << "Matrix: ";
    for (auto &row : matrix) {
 
        for (auto &el : row) {
 
            el = dist(gen);
 
            if (el > value)
                ++count;
 
            std::cout << el << ' ';
 
        }
        
        std::cout << '\n';
 
    }
    
    std::cout << "\nCount of elements: " << count << '\n';
        
    system("PAUSE");
    return 0;
}

Автор: kapbepucm 18.12.2018, 13:39
немного добавил:
Код
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <random>
 
int main()
{
  std::ofstream out_file;
  out_file.open ("my_file.txt", std::ios::out);

  constexpr std::size_t rows = 5, cols = 4;
  constexpr int value = 8;

  int matrix[rows][cols];

  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> dist(-50, 50);

  std::size_t count = 0;

  out_file << "Matrix: \n";
  for (auto &row : matrix)
  {
    for (auto &el : row)
    {
      el = dist(gen);
      if (el > value)
        ++count;
      out_file << el << ' ';
    }
    out_file << '\n';
  }  
  out_file << "\nCount of elements: " << count << '\n';
  out_file.close();

  char data[1000];
  std::ifstream in_file; 
  in_file.open("my_file.txt", std::ifstream::binary);
  in_file.read(data, 1000);
  std::cout << data;
  in_file.close();

  system("PAUSE");
  return 0;
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)