1
#include <stdio.h>
#include <conio.h>

int main()
{
   int array[100], num1, c, n, num2;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   printf("Enter the number to swap\n");
   scanf("%d", &num1);

   printf("Enter the number to swap with\n");
   scanf("%d", &num2);

    printf("%d is swap place with %d.\n", num1, num2);


    for (c = 0; c < n; c++)
   {
   if (array[c] == num1)
   {
    array[c] = num2;
   }
   else if (array[c] == num2)
   {
    array[c] = num1;
   }
  }

  printf("The new output will be\n");

        getch();       
        return 0;
 }

こんにちは、コードの途中までやっていますが、続行する方法がわかりません。リスト内のスワップ番号をコーディングしています。誰かが私を助けることができますか?

配列内の要素の数を入力してください: 5 5 つの要素を入力してください 2 4 6 8 0 入れ替える最初の数字を入力してください: 8 入れ替える 2 番目の数字を入力してください: 2 8 は 2 と場所を入れ替えます。出力は次のようになります: 8 4 6 2 0

-1 を入力してプログラムを終了するにはどうすればよいですか? 例: 配列の要素数を入力: -1 出力: プログラムを終了します。

4

2 に答える 2

0

それ以上の指示がない限り、配列を実行し、出現するたびにをに変更する必要があり8ます2。その逆も同様です。

int i;

for (i = 0; i < n; i++)
{
  if (array[i] == num1)
  {
    array[i] = num2;
  }
  else if (array[i] == num2)
  {
    array[i] = num1;
  }
}
于 2013-01-23T16:13:00.613 に答える
0

配列を反復処理して、最初と2番目の番号がどこにあるかを見つけ、それらを交換する必要があります

于 2013-01-23T16:13:48.903 に答える