2차원 배열 버블정렬 질문이요 ㅠㅠ

익명 사용자의 이미지

#include
void max(char str[][4], int num);
int main()
{
char str1[21][4];
int num;
int t;
scanf("%d", &num);
for (t = 0; t < num; t++)
{
scanf("%s", str1[t]);
}
max(str1, num);
for (t = 0; t < num; t++)
{
printf("%s",str1[t]);
printf("\n");
}
}
void max(char str[][4],int num)
{
char temp;
int i, j,h;
for (h = 0; h < num; h++)
{
for (i = 0; i < num; i++)
{
for (j = i + 1; j < 4; j++)
{
if ((int)str[h][i] < (int)str[h][j])
{
temp = str[h][i];
str[h][i] = str[h][j];
str[h][j] = temp;
}
}
}
}
}

num만큼 4자리수 입력받고
max함수에서 버블정렬로 4자리로 만들 수 있는 최대값 만들어주는 건데
결과를 보면은 최댓값 나온후에
다음 최댓값이 그 문자열에 이어져서 나옵니다 ㅠㅠ 왜그런걸까요

예)
num = 3
6238
2938
2018
입력하면

결과값은
863298328210
98328210
8210
이런식으로 나와요 ㅠㅠ 원래는 붙어서 나오면 안되는것인데..

File attachments: 
첨부파일 크기
Image icon 6174dhfb.PNG3.63 KB
세벌의 이미지

https://kldp.org/node/158191
참고하시고...

printf("%s",str1[t]);

에서 s와 따옴표 사이에 띄어쓰기 해 보세요.

printf("%s ",str1[t]);

raymundo의 이미지

scanf의 %s 는 문자열을 받는 포맷이고, 이걸로 숫자 1234를 입력받으면 실제로 저장되는 건
'1', '2', '3', '4', '\0'
이렇게 다섯 개의 문자입니다.

이걸 str[0] 부터 넣게되면 차례대로

str1[0][0] 자리에 '1'
str1[0][1] 자리에 '2'
str1[0][2] 자리에 '3'
str1[0][3] 자리에 '4'
str1[0][4] 자리에 '\0'

이렇게 저장이 되는데, str1 배열은 [21][4]로 선언되어 있으니까, str1[0][4] 는 사실은 str1[1][0]의 자리입니다. 여기에 널 캐릭터가 저장되죠.

그리고 두번째 숫자를 입력받으면 다시 str[1][0] 부터 저장이 되면서 이 널 캐릭터를 덮어써버립니다.

결과적으로 str1[0] 부터 주욱 이어진 문자열이 되어버리는 겁니다.

좋은 하루 되세요!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.