配列の入れ替え方

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
momon

配列の入れ替え方

#1

投稿記事 by momon » 8年前

配列に格納されている隣同士の値を入れ替えるプログラムを作りたいのですが、入れ替え方が全く見当が付きません。
例えば、
入れ替え前
1 2 3 4 5 6
入れ替え後
2 1 4 3 6 5

お手伝いをお願いします。

コード:

#include<stdio.h>
#define A 6

void irekae(int x, int *y);

int main(void)
{
	int d[A] = {1,2,3,4,5,6 };
	int i, j;

	for (i = 0; i < A; i++){
		printf("%2d", d[i]);
	}

	irekae(int j, int *d);


	return 0;
}

void irekae(int x, int *y)
{



}

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: 配列の入れ替え方

#2

投稿記事 by みけCAT » 8年前

素直に2個ずつ入れ替えればいいでしょう。

コード:

#include<stdio.h>
#define A 6

void irekae(int x, int *y);

int main(void)
{
	int d[A] = {1,2,3,4,5,6 };
	int i;

	for (i = 0; i < A; i++){
		printf("%2d", d[i]);
	}
	putchar('\n');

	irekae(A, d);

	for (i = 0; i < A; i++){
		printf("%2d", d[i]);
	}
	putchar('\n');

	return 0;
}

void irekae(int x, int *y)
{
	int i;
	for(i = 1; i < x; i += 2){
		int tmp;
		tmp = y[i - 1];
		y[i - 1] = y[i];
		y[i] = tmp;
	}
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

box
記事: 2002
登録日時: 13年前

Re: 配列の入れ替え方

#3

投稿記事 by box » 8年前

C以外で書くと、意外と簡単に書けたりする。

コード:

def print(a)
  a.size.times do |i|
    printf("%d%s", a[i], i == a.size - 1 ? "\n" : " ")
  end
end

def irekae(a)
  0.step(a.size - 2, 2) do |i|
    a[i], a[i + 1] = a[i + 1], a[i]
  end
end

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
puts "before"
print(a)

irekae(a)

puts "after"
print(a)
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

かずま

Re: 配列の入れ替え方

#4

投稿記事 by かずま » 8年前

参考にはならない謎のプログラム

コード:

void irekae(int x, int *y)
{
    int z = *y;
    x && (x&1 && (y[-1] = z), irekae(x-1, y+1), x&1 || (y[1] = z));
}

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: 配列の入れ替え方

#5

投稿記事 by ISLe » 8年前

別の方面から攻めてみました

コード:

void irekae(int x, int *y)
{
	int i;
	for(i = 1; i < x; i += 2) {
		y[i-1] ^= y[i] ^= y[i-1] ^= y[i];
	}
}

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: 配列の入れ替え方

#6

投稿記事 by みけCAT » 8年前

ISLe さんが書きました:別の方面から攻めてみました

コード:

void irekae(int x, int *y)
{
	int i;
	for(i = 1; i < x; i += 2) {
		y[i-1] ^= y[i] ^= y[i-1] ^= y[i];
	}
}
これはsequence pointの間でy[i-1]が複数回書き換えられているのでundefined behaviorであり、Wandboxで正常に動作しませんでした。
http://melpon.org/wandbox/permlink/XPGN64Gk9hbKCxyT

以下、N1256より引用
4. Conformance さんが書きました: 2 If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the
behavior is undefined.
6.5 Expressions さんが書きました: 2 Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: 配列の入れ替え方

#7

投稿記事 by ISLe » 8年前

みけCAT さんが書きました:
ISLe さんが書きました:別の方面から攻めてみました

コード:

void irekae(int x, int *y)
{
	int i;
	for(i = 1; i < x; i += 2) {
		y[i-1] ^= y[i] ^= y[i-1] ^= y[i];
	}
}
これはsequence pointの間でy[i-1]が複数回書き換えられているのでundefined behaviorであり、Wandboxで正常に動作しませんでした。
http://melpon.org/wandbox/permlink/XPGN64Gk9hbKCxyT

以下、N1256より引用
4. Conformance さんが書きました: 2 If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the
behavior is undefined.
6.5 Expressions さんが書きました: 2 Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.
申し訳ない。

コード:

void irekae(int x, int *y)
{
	int i;
	for(i = 1; i < x; i += 2) {
		y[i-1] ^= y[i];
		y[i] ^= y[i-1];
		y[i-1] ^= y[i];
	}
}
と訂正します。

ZORO
記事: 2
登録日時: 8年前

Re: 配列の入れ替え方

#8

投稿記事 by ZORO » 8年前

コード:

#include<stdio.h>
#define SIZE 6
 
void irekae(int s[], int max);
 
int main(void)
{
    int array[SIZE] = {1, 2, 3, 4, 5, 6};
    int i;
 	
	printf("Array before arranged:");
    for(i = 0; i < SIZE; i++) 
		printf("%2d", array[i]);
	putchar('\n');
	irekae(array,SIZE);
	printf("Array after arranged: ");
    for(i = 0; i < SIZE; i++) 
		printf("%2d", array[i]);
	 putchar('\n');
    return 0;
}
 
void irekae(int s[], int max) {
	void swap(int *a, int *b);	
	int i;
	
	for(i = 0; i < max; i += 2)
		swap(&s[i], &s[i+1]);
}

void swap(int *a, int *b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
参考になればいいなと思います。

閉鎖

“C言語何でも質問掲示板” へ戻る