[완료]C에서 달팽이 출력하는거...

onam3125의 이미지

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	int **tilt;
	int x=0;
	int y=-1;
	int t=1;
	int p, i;
	int cnt=1;
	int jmax, max;
	int sel, prove=0;
 
	printf("===============================================\n");
	printf("\t\t 프로그램입니다..\n");
	printf("===============================================\n");
 
select :
	printf("의 크기를 입력하세요 : ");
	scanf("%d", &p);
		tilt = (int**)malloc(sizeof(int)*p);
 
	if(prove!=1)
	{
		for(i=0;i<= p; i++)
		{
			tilt[i] = (int*)malloc(sizeof(int)*p);
		}//2차원 배열 할당
	}
 
	max=p;
	jmax=max;
 
	while(0<=jmax)
	{
		for(i=0;i<jmax;i++) //행 
		{
			y=y+t;
			tilt[x][y]=cnt;
			cnt++;
		}
		jmax--;
		for(i=0;i<jmax;i++)// 열 
		{
			x=x+t;
			tilt[x][y]=cnt;
			cnt++;
		}
 
		t=t*-1;
	}
 
	for(x=0;x<max;x++)// 출력
	{    
		for(y=0;y<max;y++)
		{
			printf("%4d",tilt[x][y]);
		}
		printf("\n");
	}
 
	for(i=0; i< p; i++)
	{
		free(tilt[i]);
	}
	//메모리 버퍼 해제.
 
	printf("계속하시겠습니까? Yes<1> / No <any> : ");
	scanf("%d", &sel);
	switch(sel)
	{
	case 1:
		{
			fflush(stdin);
			fflush(stdout);
			prove = 1;
			tilt=0;
			goto select;
 
		}
	default :
		{
			printf("===============================================\n");
			printf("프로그램을 종료합니다.\n");
			printf("===============================================\n");
			return 0;
		}
	}
 
 
 
}

snowall의 이미지

1. 반복실행될 때 에러가 나는 특정 부분이 어딘지 말을 해주셔야 뭐가 문제인지 답을 드릴 수 있겠죠.

2. 위에꺼 되면 아래꺼도 쉽게 됩니다. 시작을 어디서 하느냐의 문제니까 잘 생각해 보세요.

그리고 소스가 완전하지 않은 것 같은데 code태그를 써서 감싸주시면 더 좋을것 같네요

피할 수 있을때 즐겨라! http://melotopia.net/b

onam3125의 이미지

scanf("%d", &p);
		tilt = (int**)malloc(sizeof(int)*p);
 
	if(prove!=1)
	{
		for(i=0;i<= p; i++)
		{
			tilt[i] = (int*)malloc(sizeof(int)*p);
		}//2차원 배열 할당
	}

tilt = (int**)malloc(sizeof(int)*p); 까지는 괜찮은거 같고...
prove 지나자 마자 for(i=0;i<= p; i++)이쪽이 디버깅 되지도 않고(중단점 찍었는데도)
바로 에러 찍고 버로우 타더라구요 ;;

snowall의 이미지

에러가 뭔가요?

피할 수 있을때 즐겨라! http://melotopia.net/b

익명 사용자의 이미지

tilt[i]를 free 한 후에,
free(tilt); 를 빼 먹으셨네요.

onam3125의 이미지

free(tilt); 부분에서 토하더라구요 ㅠㅠ..
heap corruption detected after normal block (#66)

메모리 버퍼 때문에 ...

onam3125의 이미지

2009709219_10주차_과제_달팽이배열.exe의 0x76fe15ee에 처리되지 않은 예외가 있습니다. 0xC0000005: 0xfdfdfe15 위치를 기록하는 동안 액세스 위반이 발생했습니다.

래요...음 ;; 이거가지고 알 수 있나 ;;

shint의 이미지

//
이미 만들어진 배열을 회전'하는 방법'이 있네요.
http://blog.naver.com/kimcblog/70101261277

//
그냥 만들어 본다면.
좌측 하단'을 시작으로. +1씩 이동합니다.
방향'은 위. 우. 아래. 좌. 순서'를 반복하며.

방향'을 옮기는 조건은
- 해당위치가 각각의 모서리 인경우.
- 또는. 값'이 이미 존재하는 경우.

입니다.

5 x 5 의 배열을 만들고 해도 되고.
25 개의 선형배열을 만들고 해도 되겠습니다.

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

onam3125의 이미지

위 오류 해결되면 해봐야 겠네요 ㅠㅠ..

snowall의 이미지

goto 없이 만들어 보세요

피할 수 있을때 즐겨라! http://melotopia.net/b

onam3125의 이미지

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	int **tilt, **temptilt;
	int x=0;
	int y=-1;
	int t=1;
	int p, i, j;
	int cnt=1;
	int jmax, max;
	int sel, prove=0;
 
select :
	x=0;y=-1;t=1;cnt=1; prove=0;
	printf("의 크기를 입력하세요 : ");
	scanf("%d", &p);
		tilt = (int**)malloc(sizeof(int)*p);
		temptilt= (int**)malloc(sizeof(int)*p);
 
	if(prove!=1)
	{
		for(i=0;i<= p; i++)
		{
			tilt[i] = (int*)malloc(sizeof(int)*p);
			temptilt[i] = (int*)malloc(sizeof(int)*p);
		}//2차원 배열 할당
	}
 
	max=p;
	jmax=max;
 
	while(0<=jmax)
	{
		for(i=0;i<jmax;i++) //행 
		{
			y=y+t;
			tilt[x][y]=cnt;
			cnt++;
		}
		jmax--;
		for(i=0;i<jmax;i++)// 열 
		{
			x=x+t;
			tilt[x][y]=cnt;
			cnt++;
		}
 
		t=t*-1;
	}
 
	for (i=0; i<max; i++)
 {
  for (j=0; j<max; j++)
  {
   temptilt[i][j]=tilt[j][max-(i+1)];
  }
 }//복사
 
 
 for (i=0; i<max; i++)
 {
  for (j=0; j<max; j++)
  {
   tilt[i][j]=temptilt[i][j];;
  }
 }//붙여넣기
 
 
 
	for(x=0;x<max;x++)// 출력
	{    
		for(y=0;y<max;y++)
		{
			printf("%4d",tilt[x][y]);
		}
		printf("\n");
	}
 
	for(i=0; i< p; i++)
	{
		free(tilt[i]);
	}
 
	//메모리 버퍼 해제.
 
	printf("계속하시겠습니까? Yes<1> / No <any> : ");
	scanf("%d", &sel);
	switch(sel)
	{
	case 1:
		{
			fflush(stdin);
			fflush(stdout);
			prove = 1;//초기화
			goto select;
 
		}//반복실행
	default :
		{
			printf("===============================================\n");
			printf("프로그램을 종료합니다.\n");
			printf("===============================================\n");
			return 0;
		}
	}
 
 
 
}

이런식으로 해서 끝냈습니다 헤헿.

조언 주신분들 감사합니다 (-_-)(_ _)7

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.