2012-10-29 30 views
0

以下代碼適用於所有正向輸入,但負值是假的。如果負值初始設置爲0,它會起作用,因爲它們會添加。最後的例子。無法將負輸入設置爲零。負數組如何設置?

#include <stdio.h>                /* Necessary header */ 
#define MAX_RESPONDENTS 6 
#define MIN_RESPONSE_VALUE -100            /* Abbreviated MiRV */ 
#define MAX_RESPONSE_VALUE 86             /* Abbreviated MaRV */ 
#define RESPONSE_VALUE 187              /* Equals |(MiRV)| + or - |(MaRV)| + 1 */ 
#define STOP 3 
#define BREAK 1 

int main(void) 
{ 
    CountRating(); 

    return 0; 
} 

CountRating() 
{ 
    int ratingCounters, rating[RESPONSE_VALUE] = {0}, Counter, response; 

    for (ratingCounters = 0; ratingCounters < MAX_RESPONDENTS;) 
    { 
     int response = ratingCounters, stop = STOP; 
     printf("Enter a integer rating between %d and %d for the product: ", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE); 
     scanf("%d", &response); 

     if (response <= MAX_RESPONSE_VALUE && response >= MIN_RESPONSE_VALUE) 
      stop = STOP; 
     else 
     { 
      int stopElse = stop; 
      if (stopElse < BREAK) 
       break; 
      else 
      { 
       do 
       { 
        printf("\nNot within range. Try again.\nYou have %d more attempts before program outputs total:", stop); 
        scanf("%d", &response); 
        printf("\n"); 
        --stop; 
        if (stop < BREAK) 
        break; 
       } while (response > MAX_RESPONSE_VALUE || response < MIN_RESPONSE_VALUE); 
      } if (stop < BREAK) 
       break; 
     } 
     ++rating[response]; 
     ++ratingCounters; 

    } 
    printf("\nRating   Number of Responses\n"); 
    printf("------   -------------------"); 

    for (Counter = MAX_RESPONSE_VALUE; Counter >= MIN_RESPONSE_VALUE; --Counter) 
    { 
     printf("\n%3d%24d", Counter, rating[Counter]); 
    } 
} 

示例輸出:

Enter a integer rating between -100 and 86 for the product: 66 
Enter a integer rating between -100 and 86 for the product: 66 
Enter a integer rating between -100 and 86 for the product: 66 
Enter a integer rating between -100 and 86 for the product: 66 
Enter a integer rating between -100 and 86 for the product: 66 
Enter a integer rating between -100 and 86 for the product: 55 

Rating   Number of Responses 
------   ------------------- 
86      0 
85      0 
84      0 
83      0 
82      0 
81      0 
80      0 
79      0 
78      0 
77      0 
76      0 
75      0 
74      0 
73      0 
72      0 
71      0 
70      0 
69      0 
68      0 
67      0 
66      5 
65      0 
64      0 
63      0 
62      0 
61      0 
60      0 
59      0 
58      0 
57      0 
56      0 
55      1 
54      0 
53      0 
52      0 
51      0 
50      0 
49      0 
48      0 
47      0 
46      0 
45      0 
44      0 
43      0 
42      0 
41      0 
40      0 
39      0 
38      0 
37      0 
36      0 
35      0 
34      0 
33      0 
32      0 
31      0 
30      0 
29      0 
28      0 
27      0 
26      0 
25      0 
24      0 
23      0 
22      0 
21      0 
20      0 
19      0 
18      0 
17      0 
16      0 
15      0 
14      0 
13      0 
12      0 
11      0 
10      0 
    9      0 
    8      0 
    7      0 
    6      0 
    5      0 
    4      0 
    3      0 
    2      0 
    1      0 
    0      0 
-1      55 
-2     2686244 
-3     2686244 
-4      -3 
-5     4206858 
-6     4199517 
-7    1965606432 
-8      -2 
-9    -533773290 
-10    1965657301 
-11     2686916 
-12    1965975817 
-13     2685900 
-14      28 
-15     2685972 
-16      0 
-17     2686720 
-18    -1792050746 
-19    1965606432 
-20    1966156064 
-21      1 
-22    1965606483 
-23     2685944 
-24      17 
-25    1965552124 
-26     2685880 
-27    1966146752 
-28    1965532226 
-29     2685868 
-30    1966156064 
-31      1 
-32      0 
-33    1966156064 
-34      0 
-35      28 
-36     6105760 
-37      1 
-38    1965551966 
-39    1965572357 
-40      -2 
-41    -533740746 
-42    1965657301 
-43     2685928 
-44    1966156064 
-45     2685768 
-46      28 
-47    1965572357 
-48      0 
-49    1966156064 
-50      28 
-51    -1792050874 
-52    1965572357 
-53      1 
-54    1965572401 
-55     2685816 
-56     6097440 
-57    1965552169 
-58     2685752 
-59      36 
-60    1966146816 
-61      0 
-62    1966146816 
-63      36 
-64      0 
-65    -1792050942 
-66    1965552271 
-67    1965552263 
-68     2685756 
-69     6105754 
-70     4104 
-71     4096 
-72     6097428 
-73      36 
-74      -2 
-75    358724962 
-76    1999401429 
-77     2685928 
-78      -1 
-79     2685472 
-80    16777216 
-81     6094848 
-82      1 
-83      0 
-84     6105755 
-85     6105760 
-86     6095044 
-87      8 
-88      2 
-89    1999167428 
-90      1 
-91     6095044 
-92    -218103565 
-93     2685640 
-94    -254549010 
-95      0 
-96    1136989510 
-97    -503245737 
-98     2752512 
-99     6098688 
-100      0 
Process returned 0 (0x0) execution time : 9.734 s 
Press any key to continue. 
+1

這是失敗的,因爲在C中沒有負數組索引。'rating [0]'是數組中的第一個元素。 –

+0

是的,這是有道理的。 – user1728737

+0

您需要將您的值映射到正數組索引。如果你的範圍是-100到100,那麼你需要將它移動100,這樣指數的範圍是0-200。你所做的就像是要求購買一本書之前的100頁紙。對於所有意圖和目的,它們不存在。他們是某人0-100頁,當然**不是** _你的頁面-100 - > 0 – enhzflep

回答

3

Rating是一個大小爲187的數組。您正在對負值進行索引,這意味着C在數組開始前訪問內存並打印存儲的任何垃圾值。您只能合法地訪問0到186之間的值,所以無論何時訪問和存儲評級,您都應該在索引中添加100。這樣,數字-100對應索引0,-99對應索引1等。

2

數組的索引總是從零開始。你不應該傳遞一個數組的否定索引。如果您想要使用負向索引,請在索引中添加一個常量。

在您的示例代碼中,您可以將-MIN_RESPONSE_VALUE添加到索引,因此如果要訪問索引k,請訪問rating[k - MIN_RESPONSE]

1

您的代碼正在分配給數組的負索引。這通常不好 - 你只保留0:RESPONSE_VALUE-1的內存,寫入超出該範圍的任何內容都會產生不可預知的後果。你有兩個一般選項。一種是將您的範圍MIN_RESPONSE_VALUE:MAX_RESPONSE_VALUE映射到0:(MAX_RESPONSE_VALUE-MIN_RESPONSE_VALUE)。即減去MIN_RESPONSE_VALUE每當你指數的評價:

++rating[response-MIN_RESPONSE_VALUE]; 

另一種是重新指向響應,給它一個新的零點。這將節省您不必減去MIN_RESPONSE_VALUE所有的時間,但可能會產生混淆,以饗讀者:

rating -= MIN_RESPONSE_VALUE; 

前做此循環。這樣做後,評級[MIN_RESPONSE_VALUE]是一個有效的索引,即使它是負值。

1

其實負指數是完全可以接受的。問題是你的索引進入專用於數組的內存區域。改變rating[-1]你在陣列外改變你的堆棧,從而破壞它。難怪rating[RESPONSE_VALUE] = {0}這裏的數值沒有被初始化爲零,它們也在數組之外,並向您顯示堆棧中的一些數據。

0

花了我一陣子,但我明白了。謝謝。

#include <stdio.h>                /* Necessary header */ 
#define MAX_RESPONDENTS 6 
#define MIN_RESPONSE_VALUE -100            /* Abbreviated MiRV */ 
#define MAX_RESPONSE_VALUE 86             /* Abbreviated MaRV */ 
#define RESPONSE_VALUE 187              /* Equals |(MiRV)| + or - |(MaRV)| + 1 */ 
#define STOP 3 
#define BREAK 1 
#define DOUBLE_RESPONSE_VALUE 374 

int main(void) 
{ 
    CountRating(); 

    return 0; 
} 

CountRating() 
{ 
    int ratingCounters, rating[DOUBLE_RESPONSE_VALUE] = {0}, Counter, response; 

    for (ratingCounters = 0; ratingCounters < MAX_RESPONDENTS;) 
    { 
     int response = ratingCounters, stop = STOP; 
     printf("Enter a integer rating between %d and %d for the product: ", MIN_RESPONSE_VALUE, MAX_RESPONSE_VALUE); 
     scanf("%d", &response); 

     if (response <= MAX_RESPONSE_VALUE && response >= MIN_RESPONSE_VALUE) 
      stop = STOP; 
     else 
     { 
      int stopElse = stop; 
      if (stopElse < BREAK) 
       break; 
      else 
      { 
       do 
       { 
        printf("\nNot within range. Try again.\nYou have %d more attempts before program outputs total:", stop); 
        scanf("%d", &response); 
        printf("\n"); 
        --stop; 
        if (stop < BREAK) 
        break; 
       } while (response > MAX_RESPONSE_VALUE || response < MIN_RESPONSE_VALUE); 
      } if (stop < BREAK) 
       break; 
     } 
     if (response < 0) 
      ++rating[response + abs(RESPONSE_VALUE)]; 
     else 
      ++rating[response + abs(RESPONSE_VALUE)]; 
     ++ratingCounters; 

    } 
    printf("\nRating   Number of Responses\n"); 
    printf("------   -------------------"); 

    for (Counter = MAX_RESPONSE_VALUE; Counter >= MIN_RESPONSE_VALUE; --Counter) 
    { 
     printf("\n%3d%24d", Counter, rating[Counter + (RESPONSE_VALUE)]); 
    } 
}