2016-07-20 3 views
2

나는 16 진수와 같이 원하는 기본 코드로 변환 할 수있는 정수 배열을 취할 코드를 작성하고 있습니다. 어떤 이유 가변 길이 배열이 변환되지 않는 이유는 무엇입니까?

는, 단말이 프로그램을 통해 그것의 방법을하고

출력합니다 "변환 번호 ="여기

내 코드입니다 :에서

#include <stdio.h> 
#include <cs50.h> 

int convertedNumber[64]; 
int base; 
int digit = 0; 

void getNumberAndBase(void) { 
    int size; 

    printf("How many numbers to be converted??\n"); 
    size = GetInt(); 

    int array[size]; 

    for (int i = 0; i < size; i++) { 
     printf("Number to be converted?\n"); 
     array[i] = GetInt(); 
    } 

    printf("Base?\n"); 

    do { 
     base = GetInt(); 

     if (base < 2 || base > 16) { 
      printf("Bad base - must be between 2 and 16. Try again!\n"); 
     } 
    } while (base < 2 || base > 16); 

    void convertNumber(int size, int array[size]); 
} 

void convertNumber(int size, int numberToConvert[size]) { 
    for (int i = 0; i < size; i++) { 
     do { 
      convertedNumber[digit] = numberToConvert[i] % base; 
      digit++; 
      numberToConvert[i] /= base; 
     } while (numberToConvert[i] != 0); 
    } 
} 

void displayConvertedNumber(void) { 
    const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 
    int nextDigit; 

    printf("Converted number = "); 

    for (--digit; digit >= 0; --digit) { 
     nextDigit = convertedNumber[digit]; 
     printf("%c", baseDigits[nextDigit]); 
    } 

    printf("\n"); 
} 

int main(void) { 
    void getNumberAndBase(void), displayConvertedNumber(void); 

    getNumberAndBase(); 
    displayConvertedNumber(); 

    return 0; 
} 
+2

'무효 convertNumber (INT 크기, int 배열 [크기]);'당신이 지속적으로 코드를 포맷하십시오 기능 –

+0

을 _call_ 어떻게하지, 그것은 읽기 어렵다. 감사합니다 – user3078414

+0

또는'(void) convertNumber (int size, int 배열 [크기]); ' – babon

답변

5

당신의 코드, ~ convertNumber() 함수로 호출하지 않았습니다. 당신은 전화를 걸

convertNumber (size, array); 

void convertNumber(int size, int array[size]); 

에서 getNumberAndBase() 함수의 마지막 부분을 변경해야합니다.

말했다

, 당신은 main() 내부 convertedNumber, basedigit을 정의해야 다음 함수의 인자의 일부로 전달 (그 안에 호출되는 함수의 사용을 만들 수있을합니다). 일반적으로 세계화 될 이유가 없습니다.

또한 함수 선언을 main() 밖으로 옮깁니다. 파일 범위에 넣으십시오.

+0

또 다른 좋은 점은 main()에서'convertedNumber','base' 및'digit'에 대한 선언을 이동하여 매개 변수로 전달하는 것입니다. 글로벌 범위에 있어야하는 이유는 없습니다. (글로벌 사용이 필요한 경우가 있지만 일반적으로 다른 방법으로 피해야합니다.) –

+0

@ DavidC.Rankin 네, 전 세계를 놓친 것 같아요. –

1

코드에 몇 가지 버그가 있습니다. 가능한 한 변경되지 않은 코드의 작업 버전이 있습니다. (코드를 정리할 충동을 샀습니다.) 내가 만든 변경 사항에 주석을 추가했습니다.

#include <stdio.h> 
#include <cs50.h> 

/* I moved your prototypes up here rather than leaving them inline. */ 
void displayConvertedNumber(void); 
void convertNumber(int size, int *array); 

int convertedNumber[64]; 
int base; 
int digit = 0; 

void getNumberAndBase (void) 
{ 
    /* I moved your variable declarations here. If you want your C to be 
     portable, define your variables at the beginning of your function. 
     Don't expect "int i = 0;" to work in your for loop on all C compilers. */ 
    int size; 
    int array[size]; 
    int i; 

    printf("How many numbers to be converted??\n"); 
    size = GetInt(); 

    for(i = 0; i < size; i++){ 
     printf("Number to be converted?\n"); 
     array[i] = GetInt(); 
    } 

    printf("Base?\n"); 

    do{ 
     base = GetInt(); 

     if(base < 2 || base > 16) 
     { 
      printf("Bad base - must be between 2 and 16. Try again!\n"); 
     } 
    } while(base < 2 || base > 16); 

    /* I corrected your call to this function. */ 
    convertNumber(size, array); 
} 

void convertNumber (int size, int numberToConvert[size]) 
{ 
    int i; 

    for(i = 0; i < size; i++) 
    { 
     do{ 
      convertedNumber[digit] = numberToConvert[i] % base; 
      digit++; 
      numberToConvert[i] /= base; 
     } 
     while(numberToConvert[i] != 0); 

     /* I added a call to display the number here. The way 
      you've written your code means each number has to be 
      displayed after it is converted. You cannot convert 
      them all first and then attempt to display them since 
      you're using a single variable and index (convertedNumber 
      and digit) for the conversion. */ 
     displayConvertedNumber(); 
    } 
} 

void displayConvertedNumber (void) 
{ 
    const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 
    int nextDigit; 

    printf("Converted number = "); 

    for(--digit; digit >= 0; --digit) 
    { 
     nextDigit = convertedNumber[digit]; 
     printf("%c", baseDigits[nextDigit]); 
    } 

    printf("\n"); 

    /* I reset your digit variable here. Otherwise it would have 
     been left at -1 since that was the exit condition for your 
     loop above. */ 
    digit = 0; 
} 

int main (void) 
{ 
    getNumberAndBase(); 
    /* I removed the other function call because now everything is 
     handled in this function. */ 

    return 0; 
} 
관련 문제