#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
void main()
{
int a[5][5];
FILE *f;
int min,max,c;
f=fopen("K:\\1.txt\0","w+");
for (int i=0; i<5; i++)
{
for (int j=0; j<5; j++)
{
a[i][j]=rand()%5+1;
}
}
printf("--------------------\n");
for (int i=0; i<5; i++)
{
printf("");
for (int j=0; j<5; j++)
{
printf(" %i ",a[i][j]);
}
printf("\n");
}
printf("--------------------\n\n");
for(int i=0;i<5;i++)
{
for(int j=i;j<5-i;j++)
{
a[j][i]=0;
}
}
printf("-------------------\n");
for (int i=0; i<5; i++)
{
for (int j=0; j<5; j++)
{
printf(" %i ",a[i][j]);
fprintf(f,"%i ",a[i][j]);
}
printf("\n");
fprintf(f,"\n");
}
printf("-------------------\n");
fclose(f);
getch();}
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <locale>
#include <fstream>
using namespace std;
// Размерности
const size_t ROWS=5;
const size_t COLS=5;
// Имя файла
const char* const FILE_NAME="out.txt";
typedef int data_type;
// Печатает матрицу
template<class Elem,class T,size_t Rows,size_t Cols>
void print(basic_ostream<Elem>& out,const char* const msg,T (&matrix)[Rows][Cols])
{
out<<msg<<endl;
for(size_t i=0;i<Rows;++i)
{
for(size_t j=0;j<Cols;++j)
{
out<<setw(4)<<matrix[i][j]<<' ';
}
out<<endl;
}
}
int main()
{
locale::global(locale(""));
srand(static_cast<unsigned int>(time(0)));
// Матрица
data_type matrix[ROWS][COLS];
// Заполним матрицу случайными числами
data_type *cur=reinterpret_cast<data_type*>(matrix),
*end=reinterpret_cast<data_type*>(matrix)+ROWS*COLS;
while(cur!=end)
{
*cur++=rand()%50-25;
}
// Выведем ее
print(cout,"Исходная матрица:",matrix);
// Заполним нулями согласно условия
for(size_t j=COLS-1,i1=1,i2=ROWS-1;i1<i2;--j,++i1,--i2)
{
for(size_t i=i1;i<i2;++i)
{
matrix[i][j]=0;
}
}
print(cout,"Результат:",matrix);
// Так же выведем в файл
ofstream out(FILE_NAME);
if(out)
{
print(out,"Матрица после обработки:",matrix);
}
else
{
cout<<"Ошибка открытия файла"<<endl;
}
return 0;
}
Исходная матрица:
5 13 -7 -1 -4
-22 -4 9 4 0
17 0 15 -1 8
13 1 6 6 13
11 -25 15 -24 -13
Результат:
5 13 -7 -1 -4
-22 -4 9 4 0
17 0 15 0 0
13 1 6 6 0
11 -25 15 -24 -13
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.