👤

Pomocy! C++
Mam funkcję, która na tablicy dwuwymiarowej 8x8 jest wypełniona zerami oraz funkcję, która losuje miejsce 5 "losowych " na tej "szachownicy" i wstawia w te miejsce 1. Jak napisać program, który będzie zliczał ilość jedynek w każdym wierszu, kolumnie i po przekątnej tej tablicy?


Odpowiedź :

Odpowiedź:

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <vector>

#include <algorithm>

#include <map>

const int rows = 8;

const int columns = 8;

struct Point {

   int x;

   int y;

};

void fillWithZeros(int tab[][columns]) {

   for (int i = 0; i < rows; i++) {

       for (int j = 0; j < columns; j++) {

           tab[i][j] = 0;

       }

   }

}

void printArray(int tab[][columns]) {

   for (int i = 0; i < rows; i++) {

       for (int j = 0; j < columns; j++) {

           std::cout << tab[i][j] << " ";

       }

       std::cout << std::endl;

   }

}

void generateOnes(int tab[][columns]) {

   srand(time(nullptr));

   std::vector<Point> points;

   const int onesToGenerate = 5;

   while (points.size() != onesToGenerate) {

       Point point{};

       point.x = rand() % 8;

       point.y = rand() % 8;

       if (std::find_if(points.begin(), points.end(),

                        [&point](const Point &p) { return p.x == point.x && p.y == point.y; }) == points.end()) {

           points.push_back(point);

       }

   }

   for (const Point &p: points) {

       tab[p.x][p.y] = 1;

   }

}

std::map<int, int> sumOnesHorizontally(int tab[][columns]) {

   int sum = 0;

   std::map<int, int> rowsSum;

   for (int i = 0; i < rows; i++) {

       for (int j = 0; j < columns; j++) {

           sum += tab[i][j];

       }

       rowsSum[i + 1] = sum;

       sum = 0;

   }

   return rowsSum;

}

std::map<int, int> sumOnesPerpendicularly(int tab[][columns]) {

   int sum = 0;

   std::map<int, int> rowsSum;

   for (int i = 0; i < rows; i++) {

       for (int j = 0; j < columns; j++) {

           sum += tab[j][i];

       }

       rowsSum[i + 1] = sum;

       sum = 0;

   }

   return rowsSum;

}

int sumOnesVertically(int tab[][columns]) {

   int sum = 0;

   for (int i = 0; i < rows; i++) {

       for (int j = 0; j < columns; j++) {

           if (i == j) {

               sum += tab[i][j];

           }

       }

   }

   return sum;

}

int main() {

   int tab[rows][columns];

   fillWithZeros(tab);

   generateOnes(tab);

   printArray(tab);

   std::cout << std::endl;

   auto rowsSum = sumOnesHorizontally(tab);

   std::cout << "Ilosc jedynek w poszczegolnych wierszach\n" << std::endl;

   for (const auto&[row, sum]: rowsSum) {

       std::cout << row << " " << sum << std::endl;

   }

   std::cout << std::endl;

   auto columnsSum = sumOnesPerpendicularly(tab);

   std::cout << "Ilosc jedynek w poszczegolnych kolumnach\n" << std::endl;

   for (const auto&[column, sum]: columnsSum) {

       std::cout << column << " " << sum << std::endl;

   }

   std::cout << std::endl;

   std::cout << "Ilosc jedynek po przekatnej wynosi " << sumOnesVertically(tab);

   return 0;

}

Wyjaśnienie:

Było trochę dłuższe i bardziej skomplikowane niż mi się wydawało, więc jak zostawisz naj, to będzie mi miło