С++ Заполнение Змейкой

  • Автор темы prettynetty
  • Дата начала
P

prettynetty

#1
заполнение матрицы по диагонали змейкой по принципу
1 3 4 10
2 5 9 11
6 8 12 15
7 13 14 16.
Программа работает при вводе данных с клавиатуры. А мне нужно, чтобы можно было данные брать и из файла. не могу под файл сделать:)
<!--shcode--><pre><code class='SELECT AnyThing FROM SomeThing'>#include <stdio.h>
#include <stdlib.h>

int **allocate_matrix(int nrow, int ncol)
{
int **p, i;

if ((p =
(int **) malloc(nrow * sizeof(int *))) == NULL)
return NULL;
for (i = 0; i < nrow; i++)
if ((p =
(int *) malloc(ncol * sizeof(int))) == NULL) {
while (--i >= 0)
free(p);
free(p);
return NULL;
}
return p;
}

void free_matrix(int **m, int nrow)
{
while (--nrow > 0)
free(m[nrow]);
free(m);
}

int **snake_fill(int **m, int ord,
int direction, int (*gen)(void))
{
int i, j;

/*
* direction == 1 means direction from
* the left bottom corner to the right top one.
* ... == 0 means the opposite.
* Function parameter of the same name sets
* the direction to be started from.
*/
i = j = 0;
/*
* First cycle fills the first part which
* includes diagonal:
*
* * * * *
* * * *
* * *
* *
*/
for (;;) {
if (direction == 0) {
do {
m[i++][j--] = gen();
} while (j >= 0);
j++;
direction ^= 1;
if (i == ord)
break;
} else { /* from the right top to the left bottom */
do {
m[i--][j++] = gen();
} while (i >= 0);
i++;
direction ^= 1;
if (j == ord)
break;
} /* end of else block */
} /* end of cycle for */

if (direction == 0) {
i = 1;
j = ord - 1;
} else {
i = ord - 1;
j = 1;
}

/*
* Second cycle fills the second part:
*
* *
* * *
* * * *
*/
while (i != ord && j != ord) {
if (direction == 0) {
do {
m[i++][j--] = gen();
} while (i < ord);
i--;
j += 2;
} else {
do {
m[i--][j++] = gen();
} while (j < ord);
j--;
i += 2;
} /* end of else block */
direction ^= 1;
} /* end of cycle for */
return m;
}

int getnumber(void)
{
int n;

scanf("%d", &n);
return n;
}

int main()
{
int **m, ord, i, j;

printf("enter a size of the square matrix:\n");
scanf("%d", &ord);
m = allocate_matrix(ord, ord);

printf("enter the elements:\n");
snake_fill(m, ord, 0, getnumber);

putchar('\n');
for (i = 0; i < ord; i++) {
for (j = 0; j < ord; j++)
printf("%2d ", m[j]);
putchar('\n');
}
free_matrix(m, ord);
return 0;
}[/CODE]
 
R

rrrFer

#2
Код:
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
int **allocate_matrix(int nrow, int ncol)
{
int **p, i;

if ((p = 
(int **) malloc(nrow * sizeof(int *))) == NULL)
return NULL;
for (i = 0; i < nrow; i++)
if ((p[i] =
(int *) malloc(ncol * sizeof(int))) == NULL) {
while (--i >= 0)
free(p[i]);
free(p);
return NULL;
}
return p;
}

void free_matrix(int **m, int nrow)
{
while (--nrow > 0)
free(m[nrow]);
free(m);
}

int **snake_fill(int **m, int ord,
int direction, int (*gen)(std ::ifstream&))
{
int i, j;

/*
* direction == 1 means direction from 
* the left bottom corner to the right top one.
* ... == 0 means the opposite.
* Function parameter of the same name sets
* the direction to be started from.
*/
i = j = 0;
/* 
* First cycle fills the first part which
* includes diagonal:
*
*	 * * * *
*	 * * *
*	 * *
*	 *
*/
for (;;) {
if (direction == 0) {
do {				  
m[i++][j--] = gen();
} while (j >= 0);
j++;
direction ^= 1;
if (i == ord)
break;
} else { /* from the right top to the left bottom */
do { 
m[i--][j++] = gen();
} while (i >= 0);
i++;
direction ^= 1;
if (j == ord)
break;
} /* end of else block */
} /* end of cycle for */

if (direction == 0) {
i = 1;
j = ord - 1;
} else {
i = ord - 1;
j = 1;
}

/* 
* Second cycle fills the second part:
*
*			 *
*		 * *
*	  * * *
*/
while (i != ord && j != ord) {
if (direction == 0) {
do {				  
m[i++][j--] = gen();
} while (i < ord);
i--;
j += 2;
} else {
do {
m[i--][j++] = gen();
} while (j < ord);
j--;
i += 2;
} /* end of else block */
direction ^= 1;
} /* end of cycle for */
return m;
}

int getnumber( std ::ifstream &ifst )
{
int n;

ifst >> n;
return n;
}

int main()
{
int **m, ord, i, j;
std ::ifstream ifst( "input.txt" );

ifst >> ord;
m = allocate_matrix(ord, ord);

printf("enter the elements:\n");
snake_fill(m, ord, 0, getnumber);

putchar('\n');
for (i = 0; i < ord; i++) {
for (j = 0; j < ord; j++)
printf("%2d ", m[i][j]);
putchar('\n');
}
free_matrix(m, ord);

ifst .close();
return 0;
}
вроде бы изменил,
а еще поправь gen(ifst)
 
R

rrrFer

#3
вот так т.е. :
Код:
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
int **allocate_matrix(int nrow, int ncol)
{
int **p, i;

if ((p = 
(int **) malloc(nrow * sizeof(int *))) == NULL)
return NULL;
for (i = 0; i < nrow; i++)
if ((p[i] =
(int *) malloc(ncol * sizeof(int))) == NULL) {
while (--i >= 0)
free(p[i]);
free(p);
return NULL;
}
return p;
}

void free_matrix(int **m, int nrow)
{
while (--nrow > 0)
free(m[nrow]);
free(m);
}

int **snake_fill(int **m, int ord,
int direction, int (*gen)(std ::ifstream&), std:: ifstream &ifst)
{
int i, j;

/*
* direction == 1 means direction from 
* the left bottom corner to the right top one.
* ... == 0 means the opposite.
* Function parameter of the same name sets
* the direction to be started from.
*/
i = j = 0;
/* 
* First cycle fills the first part which
* includes diagonal:
*
*	 * * * *
*	 * * *
*	 * *
*	 *
*/
for (;;) {
if (direction == 0) {
do {				  
m[i++][j--] = gen(ifst);
} while (j >= 0);
j++;
direction ^= 1;
if (i == ord)
break;
} else { /* from the right top to the left bottom */
do { 
m[i--][j++] = gen(ifst);
} while (i >= 0);
i++;
direction ^= 1;
if (j == ord)
break;
} /* end of else block */
} /* end of cycle for */

if (direction == 0) {
i = 1;
j = ord - 1;
} else {
i = ord - 1;
j = 1;
}

/* 
* Second cycle fills the second part:
*
*			 *
*		 * *
*	  * * *
*/
while (i != ord && j != ord) {
if (direction == 0) {
do {				  
m[i++][j--] = gen(ifst);
} while (i < ord);
i--;
j += 2;
} else {
do {
m[i--][j++] = gen(ifst);
} while (j < ord);
j--;
i += 2;
} /* end of else block */
direction ^= 1;
} /* end of cycle for */
return m;
}

int getnumber( std ::ifstream &ifst )
{
int n;

ifst >> n;
return n;
}

int main()
{
int **m, ord, i, j;
std ::ifstream ifst( "input.txt" );

ifst >> ord;
m = allocate_matrix(ord, ord);

printf("enter the elements:\n");
snake_fill(m, ord, 0, getnumber, ifst );

putchar('\n');
for (i = 0; i < ord; i++) {
for (j = 0; j < ord; j++)
printf("%2d ", m[i][j]);
putchar('\n');
}
free_matrix(m, ord);

ifst .close();
return 0;
}
 
C

Crunchbanger

#4
заполнение матрицы по диагонали змейкой по принципу
1 3 4 10
2 5 9 11
6 8 12 15
7 13 14 16.
Программа работает при вводе данных с клавиатуры. А мне нужно, чтобы можно было данные брать и из файла. не могу под файл сделать:blush:
<!--shcode--><pre><code class='SELECT AnyThing FROM SomeThing'>#include <stdio.h>
#include <stdlib.h>

int **allocate_matrix(int nrow, int ncol)
{
int **p, i;

if ((p =
(int **) malloc(nrow * sizeof(int *))) == NULL)
return NULL;
for (i = 0; i < nrow; i++)
if ((p =
(int *) malloc(ncol * sizeof(int))) == NULL) {
while (--i >= 0)
free(p);
free(p);
return NULL;
}
return p;
}

void free_matrix(int **m, int nrow)
{
while (--nrow > 0)
free(m[nrow]);
free(m);
}

int **snake_fill(int **m, int ord,
int direction, int (*gen)(void))
{
int i, j;

/*
* direction == 1 means direction from
* the left bottom corner to the right top one.
* ... == 0 means the opposite.
* Function parameter of the same name sets
* the direction to be started from.
*/
i = j = 0;
/*
* First cycle fills the first part which
* includes diagonal:
*
* * * * *
* * * *
* * *
* *
*/
for (;;) {
if (direction == 0) {
do {
m[i++][j--] = gen();
} while (j >= 0);
j++;
direction ^= 1;
if (i == ord)
break;
} else { /* from the right top to the left bottom */
do {
m[i--][j++] = gen();
} while (i >= 0);
i++;
direction ^= 1;
if (j == ord)
break;
} /* end of else block */
} /* end of cycle for */

if (direction == 0) {
i = 1;
j = ord - 1;
} else {
i = ord - 1;
j = 1;
}

/*
* Second cycle fills the second part:
*
* *
* * *
* * * *
*/
while (i != ord && j != ord) {
if (direction == 0) {
do {
m[i++][j--] = gen();
} while (i < ord);
i--;
j += 2;
} else {
do {
m[i--][j++] = gen();
} while (j < ord);
j--;
i += 2;
} /* end of else block */
direction ^= 1;
} /* end of cycle for */
return m;
}

int getnumber(void)
{
int n;

scanf("%d", &n);
return n;
}

int main()
{
int **m, ord, i, j;

//printf("enter a size of the square matrix:\n");

FILE * fin = fopen("file.in", "r");

fscanf(fin, "%d", &ord);
m = allocate_matrix(ord, ord);

printf("enter the elements:\n");
snake_fill(m, ord, 0, getnumber);

putchar('\n');
for (i = 0; i < ord; i++) {
for (j = 0; j < ord; j++)
printf("%2d ", m[j]);
putchar('\n');
}
free_matrix(m, ord);

fclose(fin);
return 0;
}[/CODE]


И все!
Только не забудь создать файл file.in
и прописать туда значения. Иначе
программа выдасть ошибку сегментирования.
Прости обработчик ошибок не добавил.