2017-11-13 1 views
-5

입력 (명령 줄 인수)에서 일련의 숫자를 읽고 해당 발생 횟수를 인쇄하고 발생 횟수가 가장 많은 숫자에 대한 발생 횟수를 인쇄하려면이 프로그램을 작성했습니다 시퀀스 내에서.이 프로그램을 수정하고 개선하는 방법

발생 횟수를 알려주는 방법을 모르겠습니다. 또한이 코드는 작동하지 않습니다. 누구든지 나를 도울 수 있습니까? 이 코드를 수정하고 발생을 찾기 위해 개선해야합니다.

구조를 사용하지 않고이 코드를 작성할 수있는 간단한 방법이 있습니까?

#include<stdio.h> 

    void main(int argc, char* argv[]) 
    { 
     struct numbers 
     { 
      int value; 
      int index; 
     }; 

     struct numbers num[201]; 

     int i,j,k=0,l,m,counter; 

     for(i=1; i<argc; i++) 
     { 
      for(j=i+1; j<argc; j++) 
      { 
       if (atoi(argv[i])==atoi(argv[j])) 
       { 
        counter ++; 
       } 
      } 
      num[k].value = argv[i]; 
      num[k].index = counter; 
      k++; 
     } 
     int max; 

     for (l=1; l<sizeof(num); i++) 
     { 
      max = num[0].index; 
      for(m=l+1; m<sizeof(num); m++) 
      { 
       if(num[m].index > max) 
       { 
        max = num[m].index; 
       } 
      } 
     } 
     printf("%d",max); 
    } 
+3

_ "이 코드는 작동하지 않습니다"아닙니다

아래 코드는 다음의 아주 기본적인 구현 문제 설명. 왜 안돼? 무슨 일이 일어날 것인가? 대신에 무슨 일이 일어난거야? 어떤 정확한 오류 메시지를 받았습니까? 입력 및 원하는 출력의 예는 무엇입니까? –

+0

구조체 번호 num [201]; unguarded num [k] .index = counter; 매우 위험합니다. argc가 201보다 클 때마다 배열 오버 플로우가 발생합니다 – Eresse

답변

0

루프는 다음과 같이해야한다 :

for(i=1; i<argc; i++) { 
    for(j=0; j<k; j++) {    // search value in array 
    if (atoi(argv[i])==num[j].value) 
     break; 
    } 
    if(j<k) num[j].index++;   // value found - increment count 
    else {       //value not found - new entry 
    num[k].value = atoi(argv[i]); 
    num[k].index = 1; 
    k++; 
    } 
} 
0

당신은 아주 간단하게이를 수 있으며, 동적 메모리 (필요 없음 할당)하지 않고 당신은 결과를 정렬 할 필요가없는 경우. 것 가장 높은 발생과 수는

직선 전달 솔루션을 계산 각 번호에 대한 발생

  1. 디스플레이 수는
  2. 이 & 표시를 확인 입력 :

    당신은 두 가지 요구 사항을 갖고있는 것 같다 각 숫자에 대해 명령 행 인수 목록을 한 x 씩 _ 생 횟수를 = 고 그 수를 인쇄하십시오. 이렇게하는 동안 "가장 빈번한 번호"를 녹음하면 끝에있는 번호가 가장 높은 번호를 인쇄 할 수 있습니다. 그것은 이와 같은 출력을 줄 것이다

    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(int argc, char **argv) 
    { 
        // This will be our argument iterator (to run through the argument list) 
        int arg_index; 
    
        // This is the number we're currently looking for (counting occurrences for) 
        int cur_num; 
    
        // This is the number of occurrences for the number we're currently processing 
        int cur_num_count; 
    
        // This variable will hold the number with the most occurrences 
        int top_num; 
    
        // This variable will hold the occurrence count for the number with the most occurrences 
        int top_num_count = 0; 
    
        // This variable will be used to determine whether we've already processed a given number 
        int has_been_processed; 
    
        // Temporary Iterator 
        int i; 
    
        // Run through arguments (skipping the first, which is the program name) 
        for(arg_index = 1; arg_index < argc; arg_index = arg_index + 1) 
        { 
         // Acquire Number and reset occurrences counter 
         cur_num = atoi(argv[arg_index]); 
         cur_num_count = 0; 
    
         // Determine if we've already processed this number 
         has_been_processed = 0; 
         for(i = 1; i < arg_index; i = i + 1) 
         { 
          if(atoi(argv[i]) == cur_num) { has_been_processed = 1; } 
         } 
    
         // Process this number if it has not yet been processed 
         if(has_been_processed == 0) 
         { 
          // Run through argument list counting occurrences 
          for(i = 1; i < argc; i = i + 1) 
          { 
           // Count occurrences 
           if(atoi(argv[i]) == cur_num) { cur_num_count = cur_num_count + 1; } 
          } 
    
          // Compare count to "top" count (set as "top" number) 
          if(cur_num_count > top_num_count) 
          { 
           top_num = cur_num; 
           top_num_count = cur_num_count; 
          } 
    
          // Display Number & Count 
          printf(" * %i -> %i occurrences\n", cur_num, cur_num_count); 
         } 
        } 
    
        // Display "top" number (highest occurrence count) 
        printf("Top number: %i (%i occurrences)\n", top_num, top_num_count); 
    } 
    

    :

    이 _
    $ ./num 743 44 34 34 0 13 18 104 4827 364 743 0 55 284 44 876 990 44 
    * 743 -> 2 occurrences 
    * 44 -> 3 occurrences 
    * 34 -> 2 occurrences 
    * 0 -> 2 occurrences 
    * 13 -> 1 occurrences 
    * 18 -> 1 occurrences 
    * 104 -> 1 occurrences 
    * 4827 -> 1 occurrences 
    * 364 -> 1 occurrences 
    * 55 -> 1 occurrences 
    * 284 -> 1 occurrences 
    * 876 -> 1 occurrences 
    * 990 -> 1 occurrences 
    Top number: 44 (3 occurrences) 
    
관련 문제