0

Xcode では、いくつかのポイント (X、Y) がユーザーによって与えられるグラフを作成しようとしています。ユーザーは、グラフにプロットするポイントの数を選択できます。X 軸のポイントを NSMutableArray に保存し、Y 軸のポイントを NSMutableArray に保存しましたが、これらを昇順に並べ替える必要があるため、同じ x または y 軸プロットの 2 つのポイントがある場合にグラフのバグを修正できます。NSMutableArrays にはプリミティブ int が含まれています。私がしなければならないことの例は、4,8,5,3 などの軸の 1 つに 4 つの座標が与えられた場合、配列を 3,4,5,8 にソートする必要がある場合です。

以下のコードは非常に長いため、グラフ生成は含まれていません。

助けてくれてありがとう!-

int userInputCount;

printf("How many coordinates would you like to plot? ");
scanf("%i", &userInputCount);
printf("\n");

NSMutableArray * xCoordArray = [[NSMutableArray alloc] init];
NSMutableArray * yCoordArray = [[NSMutableArray alloc] init];

for (int i = 1; i <= userInputCount; i++){
        int xCoord;
        int yCoord;

        printf("(%i) Enter coordinates [X,Y]: ", i);

        scanf("%i,%i", &xCoord, &yCoord);

        NSNumber *xCoordinate =  [[NSNumber alloc] initWithInt:xCoord];
        NSNumber *yCoordinate =  [[NSNumber alloc] initWithInt:yCoord];

        [xCoordArray addObject:xCoordinate];
        [yCoordArray addObject:yCoordinate];

}
4

1 に答える 1

0

しばらく取り組んだ後、誰かが同じことを試みているかどうかを理解しました

for (int j = 0; j < 2; j++){
            for (int i = 0; i < [yCoordArray count] - 1; i++){

                int yExchangeIntOne = [[yCoordArray objectAtIndex:i] intValue];
                int yExchangeIntTwo = [[yCoordArray objectAtIndex:i +1] intValue];
                if (yExchangeIntOne > yExchangeIntTwo){
                    [yCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
                    i = 0;
                }

                int xExchangeIntOne = [[xCoordArray objectAtIndex:i] intValue];
                int xExchangeIntTwo = [[xCoordArray objectAtIndex:i +1] intValue];
                if (xExchangeIntOne > xExchangeIntTwo){
                    [xCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
                    i = 0;
                }

            }
        }

これは [xCoordArray addObject:xCoordinate] の直後に続きます。[yCoordArray addObject:yCoordinate]; ループの中

于 2013-04-27T01:50:59.767 に答える