2017-02-08 7 views
-2

私は、ユーザに2つの数字MとNを入力するよう求めるプログラムを書いています。プログラムは2次元配列配列[M] [N]を作成します。複数の異なる関数を使ってC

  • プリントアレイ():M×N個のマトリックス
  • PopulateRandom()における印刷要素:M 1からすべての要素にランダムな値を割り当て*含まれる

    フォー他の関数を使用する必要がN

  • LinearSearch():値が繰り返されるかどうかを調べる
  • LeftShift():配列のすべての要素を左にシフトします。最初の要素は最後の要素になります。

私のコードで問題が発生し、2つの値を入力すると、プログラムはデフォルトの「Enter 1 or 0」という文字列を無限に印刷し続けます。誰かが私を助けることができますか?

#include <stdbool.h> 
#include <stdio.h> 
#include <stdlib.h> 

bool LinearSearch(int[][N], int); 
void PopulateRandom(int[][N]); 
void PrintArray2D(int[][N]); 
void LeftShift(int[][N]); 

// Prints elements of array in a M x N table 
void PrintArray2D(int array[][N]) { 
    int i, j; 

    for (i = 0; i < M; i++) { 
    for (j = 0; j < N; j++) { 
     printf("%d", array[i][j]); 
    } 
    printf("\n"); 
    } 
} 

// Assigns elements of the array "array" random values 
void PopulateRandom(int array[][N]) { 
    int i, j, x; 

    for (i = 0; i < M; i++) { 
    for (j = 0; j < N; j++) { 
     bool found = 1; 
     while (found == 1) { 
     x = rand() % M * N - 1; 
     found = LinearSearch(array, x); 
     } 
     array[i][j] = x; 
    } 
    } 
} 

// functino performs a linear search to see if there are any repeated values 
// in the array. 
bool LinearSearch(int array[][N], int num) { 
    bool p_flag = 0; 
    int i, j; 

    for (i = 0; i < M; i++) { 
    for (j = 0; j < N; j++) { 
     if (array[i][j] == num) { 
     p_flag = 1; 
     } 
    } 
    } 
    return p_flag; 
} 

void LeftShift(int array[][N]) { 
    int i; 
    int j; 

    for (i = 0; i < M; i++) { 
    for (j = 0; j < N; j++) { 
     if (j == N - 1) { 
     if (i == M - 1) { 
      array[i][j] = array[0][0]; 
     } else { 
      array[i][j] = array[i + 1][0]; 
     } 
     } else { 
     array[i][j] = array[i][j + 1]; 
     } 
    } 
    } 
} 

int main(void) { 
    int option; 

    printf("If you would like to search ann array, enter 1 \n: "); 
    printf("If you would like to exit, enter 0 \n: "); 
    scanf("%d", &option); 

    while (option != 0) { 
    switch (option) { 
     case 1: { 
     printf("Enter two numbers M and N: "); 
     scanf("%d %d", &M, &N); 
     int array[M][N]; 
     PopulateRandom(array); 
     PrintArray2D(array); 
     LeftShift(array); 
     PrintArray2D(array); 
     break; 
     } 
     case 0: 
     break; 
     default: 
     printf("Enter 1 or 0"); 
    } 
    } 
} 
+0

'while(option!= 0)'はオプション '0'が指定されていない限り無限ループですループ内で 'option'を決して変更しないので、入力されます。おそらく、あなたはscanf( "%d"、&option);をループ内で動かし、scanfの戻り値もチェックするべきです。 –

+0

@ M.Mまたは無効なオプションが選択されたとき。 1を選択すると、それも永遠にループします。 –

+0

デバッガでコードをステップ実行する方法を学ぶ - このようなものは明白になる – pm100

答えて

2

これを試してみてください:

int main(void) { 
    int option = 1; 
    while (option != 0) { 
     printf("If you would like to search ann array, enter 1 \n: "); 
     printf("If you would like to exit, enter 0 \n: "); 
     scanf("%d", &option); // << into the loop 
     switch (option) { 
1

while (option != 0)で無限ループがあるように起こります。 whileループを変更する必要があります。

int option = 1; /* initialisation prevents UB, if you're moving the `scanf` into the loop */ 
while(option != 0) 
{ 
    scanf("%d", &option); 
} 

scanfが成功/失敗に対応する値を返し、これはまた、不要な(しかし、それにもかかわらず、有用な)初期化の必要性を回避するであろう、ループの条件式に配置することができることに留意:

int option; 
while (scanf("%d", &option) == 1 && option != 0) { 
    switch (option) { ... } 
} 

入力が行単位で処理されることを期待する場合は、ユーザーがオプションを入力したときに無効なものなど、無効なものを入力した場合に何が起こるかを考えてください。

最初のケースでは、入力失敗に対応する戻り値は破棄されますが、入力ストリームは変更されず(未来の読み込みでは無効です)、望ましくない無限ループが発生します。

2番目のケースでは、入力失敗に対応する戻り値は破棄されません。実際には、ループを終了させます。エラーメッセージを表示して有効な選択を促すメッセージを表示する場合は、次のように入力します。

int option; 
for (int v = scanf("%d", &option); v != EOF && option != 0; v = scanf("%d", &option)) { 
    if (!v) { 
     puts("ERROR! Invalid selection; try again..."); 
     scanf("%*[^\n]"); /* This discards the junk... */ 
     getchar();  /* This discards the newline following the junk, 
          * which is unnecessary in your situation  */ 
     continue; 
    } 
    switch (option) { ... } 
} 
+0

Yea iそのエラーを修正しましたが、今実行するとMとNの値を入力しても終了しませんが、文字通り何もしません。 –

+0

SOにようこそ!正しい方向に動いているうちに、この回答は少し不完全です私がそれを構築すればいいのですか? – Sebivor

+0

@Sebええ、確かに、私は実際にコーディングのノブです:P – Gautam

関連する問題