S
S
Serge X2015-03-29 20:03:07
C++ / C#
Serge X, 2015-03-29 20:03:07

Why does it incorrectly count the number of repetitions of each element of the matrix?

#include "stdafx.h"
#include <iostream>

using namespace std;

int N;

int main() {
  setlocale(0, "");
  
  cout << "Введите размер матрицы: ";
  cin >> N;
  int ** A = new int *[N];
  for (int i = 0; i < N; i++)
    A[i] = new int[N];

  for (int i = 0; i < N; i++)
  for (int j = 0; j < N; j++)
    A[i][j] = ((rand() % 10));


  cout << "Сгенерированная матрица: " << endl << endl;
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
      cout << A[i][j] << " ";
    cout << endl;
  }

  cout << endl;

  int reps = 0;
  int i, j;
  int inext, jnext;
  int temp;

  i = 0; j = 0;

  for (inext = 0; inext < N; inext++){
    for (jnext = (inext ? 0 : 1); jnext < N; jnext++)
    {
      if (A[i][j] == A[inext][jnext]){
        reps++;
        continue;
      }

      cout << A[i][j] << " : " << reps + 1 << endl;

      reps = 0;

      i = inext;
      j = jnext;

    }
  }

  cin.get();
  cin.get();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Vitrenko, 2015-03-29
@Vestail

Why do you need to count the number of elements when the size is known? О_о
amount = N*N;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question