2013-06-20 8 views
-6

이 질문에 대해 유감이지만 다른 곳으로 갈 곳이 없으며 해결 방법을 찾을 수 없습니다. 저는 Pascal 프로그래밍 언어에 익숙합니다. 그래서이 C 언어는 저에게 익숙한 것처럼 보이지만 while 루프의 전체 구조를 변경하는 if 함수를 추가하는 것은 저에게 너무 복잡합니다. 제발 도와주세요.안녕하세요, 누군가이 루프로 나를 도울 수 있습니까

The array variable consists of a sequence of ten numbers. Inside the while loop, you must write two if conditions, which change the flow of the loop in the following manner (without changing the printf command):

  • If the current number which is about to be printed is less than 5, don't print it.
  • If the current number which is about to be printed is greater than 10, don't print it and stop the loop.

    Notice that if you do not advance the iterator variable i and use the continue derivative, you will get stuck in an infinite loop.
#include <stdio.h> 

int main() 
{ 
    int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4}; 
    int i = 0; 

    while (i < 10) 
    { 
     /* your code goes here */ 

     printf("%d\n", array[i]); 
     i++; 
    } 
    return 0; 
} 
+0

Java, C++ 및 Pascal 태그가 필요한 이유는 무엇입니까? –

+4

당신은'continue'와'break'에 익숙합니까? –

+0

C에 익숙하다면'if'와'break'에 익숙 할 것입니다. –

답변

1

C 플로우 컨트롤에 대한 이해를 테스트하고 있습니다. 원하는 내용은 다음과 같습니다.

if (array[i] < 5) {i++; continue; } // increment, go back to while 
if (array[i] > 10) break;   // leave while 
1

당신은 정말 breakif 문을 사용하려고합니다. 이러한 개념을 알아야 좋은 언어이 될 수 있습니다.

if (array[i] > 10) 
     break; 
if (array[i] >= 5) 
     printf("%d\n", array[i]); 
+0

두 번째 if는'if (array [i] <5) {i ++; 잇다; }' – ctn

+1

@ctn이 필요 없다고 생각합니다. 그의 질문을 오해하지 않는 한. 소리가 5보다 작 으면 아무 것도하지 않고 5에서 10까지 인쇄하고 10을 넘으면 소리가납니다. 괜찮을거야. – greedybuddha

+1

네, 정확하게 공식화되지 않았습니다. printf 명령문을 "printf 명령을 변경하는 경우"에 포함 시키면 논쟁의 여지가 있습니다. 요구 사항은 분명히 학생에게 '계속'과 '중단'을 가르치기위한 것입니다. – ctn

관련 문제