Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 06.11.2014, 14:27   #1
Джон Крейк
 
Регистрация: 26.10.2014
Сообщений: 6
По умолчанию Вывод матрици

Подскажите, как сделать вторую матрицу похожей на матрицу


Код:
#include<iostream>
#include <iomanip>
#include<stdlib.h>
#include<cmath>
#include <time.h>
#include<windows.h>
#define ROWS 10
#define COLS 10
#include <cstdlib>

void newMatr(int matr[][COLS], int nRow, int nCol);
void matrixToZero(int matr[][COLS], int nRow, int nCol);


using namespace std;

int main()
{
int nRow, nCol, matr[ROWS][COLS];

    int i,j;
    cout<<"N=";
    cin>>nRow;
    cout<<"M="; 
    cin>>nCol;
    cout<<"Input matrix A \n";
     for (i=0; i<nRow; i++)
    for (j=0; j<nCol; j++)
    cin>>matr[i][j]; 
    cout<<"matrix A \n";
    for (i=0; i<nRow; i++)
    {
        for (j=0; j<nCol; j++)
    cout<<matr[i][j]<<"\t"; 
    cout<<endl; 
       }
    matrixToZero(matr, nRow, nCol);
    f2(matr, nRow, nCol);


return 0;
}

void matrixToZero(int matr[][COLS], int nRow, int nCol){

    for(int i=0;i<nRow;++i)
           for(int j=0;j<nCol;++j)
           {
              if (i<j)
                 matr[i][j]=0;
              else if(i>j)
                 matr[i][j]=0;
              else
                 matr[i][j]=1;

              cout<<matr[i][j]<<"\n";

           }

}


потому, что выходит просто ряд символов

Последний раз редактировалось Джон Крейк; 06.11.2014 в 17:13.
Джон Крейк вне форума Ответить с цитированием
Старый 07.11.2014, 10:24   #2
8Observer8
Старожил
 
Аватар для 8Observer8
 
Регистрация: 02.01.2011
Сообщений: 3,323
По умолчанию

Matrix.h
Код:
#ifndef MATRIX_H
#define MATRIX_H

#include <vector>
#include <cstddef>

template <typename Type>
class Matrix
{
public:
    Matrix( Type initValue, size_t nrows = 2, size_t ncols = 2 ) :
        m_initValue( initValue ),
        m_matrix( nrows ),
        m_nrows( nrows ),
        m_ncols( ncols )
    {
        // Create columns
        for ( size_t row = 0; row < nrows; ++row ) {
            m_matrix[row].resize( ncols, initValue );
        }
    }

    void setValue( Type value, unsigned int row, unsigned int col )
    {
        m_matrix[row][col] = value;
    }

    Type value( unsigned int row, unsigned int col ) const
    {
        return m_matrix[row][col];
    }

    void addRow()
    {
        std::vector<Type> row( m_ncols, m_initValue );
        m_matrix.push_back( row );
        ++m_nrows;
    }

    void addColumn()
    {
        for ( size_t row = 0; row < m_nrows; ++row ) {
            m_matrix[row].push_back( m_initValue );
        }
        ++m_ncols;
    }

    size_t countRows() const
    {
        return m_nrows;
    }

    size_t countCols() const
    {
        return m_ncols;
    }

private:
    Type m_initValue;
    std::vector< std::vector<Type> > m_matrix;
    size_t m_nrows;
    size_t m_ncols;
};

#endif // MATRIX_H
main.cpp
Код:
#include <cstdlib>      // std::srand, std::rand
#include <iostream>     // std::cout std::cin
#include <ctime>        // std::time
#include "Matrix.h"

// Show the matrix
template <typename Type>
void showMatrix( const Matrix<Type> &matrix );

int main( )
{
    // Enter a number of rows
    size_t nrows;
    std::cout << "\nEnter a number of rows:\n> ";
    std::cin >> nrows;

    // Enter a number of rows
    size_t ncols;
    std::cout << "\nEnter a number of columns:\n> ";
    std::cin >> ncols;

    // Create a matrix
    Matrix<int> matrix( 0, nrows, ncols );

    // Initialize random seed
    std::srand ( std::time( NULL ) );

    // Fill the matrix
    for ( size_t row = 0; row < matrix.countRows(); ++row ) {
        for ( size_t col = 0; col < matrix.countCols(); ++col ) {
            matrix.setValue( rand() % 100, row, col );
        }
    }

    // Add a row
    matrix.addRow();

    // Add a column
    matrix.addColumn();

    // Show the matrix
    showMatrix( matrix );

    return 0;
}

// Show the matrix
template <typename Type>
void showMatrix(const Matrix<Type> &matrix )
{
    std::cout << '\n';
    for ( size_t row = 0; row < matrix.countRows(); ++row ) {
        for ( size_t col = 0; col < matrix.countCols(); ++col ) {
            std::cout << matrix.value( row, col ) << " ";
        }
        std::cout << '\n';
    }
    std::cout << '\n';
}
Output
Цитата:
Enter a number of rows:
> 10

Enter a number of columns:
> 3

31 39 47 0
1 1 88 0
21 27 14 0
0 88 61 0
54 62 91 0
25 1 92 0
22 44 63 0
37 34 92 0
29 74 32 0
95 1 78 0
0 0 0 0

Press <RETURN> to close this window...
8Observer8 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
матрици tetu115 Паскаль, Turbo Pascal, PascalABC.NET 5 22.12.2013 17:54
матрици kirito_17 Помощь студентам 5 03.12.2013 18:20
Матрици MiZZanTTroP Помощь студентам 5 01.11.2012 10:37
Поворот матрици(С++) Lemo Помощь студентам 1 21.10.2009 19:31
матрици Forro Паскаль, Turbo Pascal, PascalABC.NET 5 14.04.2008 20:31