Хелпикс

Главная

Контакты

Случайная статья





Листинг программы



Листинг программы

#include <iostream>

#include <time.h>

#include <limits.h>

#define SIZE 10

using namespace std;

 

void SetSize (short &);

void input_matrix (short [][SIZE], int [][SIZE], short, short);

void random_matrix (short [][SIZE], int [][SIZE], short, short);

void output_matrix (const short [][SIZE], short, short);

void form_vector (short [SIZE][SIZE], long [SIZE], short, short);

void output_vector (const long [], short);

void average (short [SIZE][SIZE], int, short, short, short, long);

 

int main ()

{

  setlocale(0, "Rus");

  short A [SIZE][SIZE], row, col, q, i=0, res=0;

  int test_A [SIZE][SIZE];

  long B [SIZE], sum=0;

  do

  {

  cout<<"Введите количество строк --> ";

  SetSize (row);

  cout<<"Введите количество столбцов --> ";

  SetSize (col);

  cout<<"\n Ввести вручную? (1 - да) \t", cin>>q, "\n";

        switch (q)

        {

               case 1: {

                                   input_matrix (A, test_A, row, col); break;

                             }

               default: {

                                   random_matrix (A, test_A, row, col); break;

                              }

        }

  output_matrix (A, row, col);

  form_vector (A, B, row, col);

  output_vector (B, row);

  average (A, res, row, col, i, sum);

  cout<<"Повторный ввод? (1 - да)"<<"\n";

  q=0; cin>>q;

  }

  while (q==1);

  cout<<"<Конец> \n";

}

void average (short A [SIZE][SIZE], int res, short row, short col, short i, long sum)

{

  for (short x=0; x<row; x++)

  for (short y=0; y<col; y++, i++)

        sum+=A [x][y];

        res=sum/i;

        cout<<"Average = "<<res<<"\n";

        return;

}

void form_vector (short A [SIZE][SIZE], long B [SIZE], short row, short col)

{

  for (short y=0; y<col; y++)

        {

               B [y]=1;

                      for (short x=0; x<row; x++)

                             B [y]*=A [x][y];

        }

                             cout<<"              ";

  return;

}

void input_matrix (short A [][SIZE], int A [][SIZE], short row, short col)

{

  cout<<"<Ввод вручную>\n";

for (short x=0; x<row; x++)

        {

               for (short y=0; y<col; y++)

               {

                      for (cout<<" \n Введите ["<<x+1<<"]["<<y+1<<"] элемент--> \t", cin>>A [x][y];

                 SHRT_MIN>A [x][y] || A [x][y]>SHRT_MAX;

                      cout<<"\n Ошибка ввода. Повторите ввод ["<<x+1<<"]["<<y+1<<"] элемента-->", cin>>A [x][y])

                      {}

                      A [x][y]=A [x][y];

               }

        }

cout<<"\n <Ввод завершен> \n";

return;

}

void output_matrix (const short A [][SIZE], short row, short col)

  {

        cout<<"Матрица\n";

        for (short x=0; x<row; x++)

               {

                      for (short y=0; y<col; y++)

                      cout<<A [x][y]<<"\t";

                      cout<<endl;

               }

        return;

  }

void output_vector (const long B[], short row)

{

        cout<<"\n Вектор: \n";

        for (int x=0; x<row; x++)

               cout<<B[x]<<"\t";

        cout<<endl;

        return;

}

void random_matrix (short A [][SIZE], int test_A [][SIZE], short row, short col)

{

srand( (unsigned)time( NULL ) );

for (int x=0; x<row; x++)

{

        for (int y=0; y<col; y++)

        {

               for (test_A [x][y]=rand()%(row*col)-rand()%(row*col);

                      SHRT_MIN>test_A [x][y] || test_A [x][y]>SHRT_MAX;

                      test_A [x][y]=rand()%(row*col)-rand()%(row*col))

                      {}

                      A [x][y]=test_A [x][y];}}

                      cout<<"\n Матрица \n";

                      return;

        }

void SetSize (short & x)

        {

               for (cin>>x, "\n";

               0>=x || x>10;

               cout<<"\n Ошибка \t", cin>>x, "\n")

        {}

  return;

}

 



  

© helpiks.su При использовании или копировании материалов прямая ссылка на сайт обязательна.