• 締切済み

C++で配列のソート(Cstring)

#include<iostream> using namespace std; const int R_SIZE = 5; const int C_SIZE = 17; void selectionSort(char ary[][C_SIZE]); void printArray(char ary[][C_SIZE]); int main(){ char names[R_SIZE][C_SIZE] = { "Collins, Bill", "Smithm, Bart", "Allen, Jim", "Griffin, Jim", "Stamey, Marty"}; selectionSort(names); cout << "Sorted array is:" << endl << endl; printArray(names); return 0; }//end main() //********************************************* void selectionSort(char ary[][C_SIZE]){ int minIndex; char minValue[R_SIZE]; for(int ix = 0; ix < R_SIZE - 1; ix++) { minIndex = ix; strcpy(minValue, ary[ix]); for(int jx = ix + 1; jx < R_SIZE; jx++) { if(ary[jx] < minValue) { strcpy(minValue, ary[jx]); minIndex = jx; } } strcpy(ary[minIndex], ary[ix]); strcpy(ary[ix], minValue); } } //********************************************* void printArray(char ary[][C_SIZE]){ for(int ix = 0; ix < R_SIZE; ix++){ cout << ary[ix] << endl; } cout << endl; } ------------------------------------------ このプログラムでは、Allen, Jim           Collins, Bill           Griffin, Jim           Smithm, Bart           Stamey, Marty という順番に並んでほしいのですが、 実際は C→Sm→A→G→St という 違う順に並んでしまうようです。 どこが間違っているのか思いつかないので 教えてください。

みんなの回答

  • yosi_yosi
  • ベストアンサー率35% (165/468)
回答No.2

#1さんの指摘に追加して、 void selectionSort()中の char minValue[R_SIZE]; は char minValue[C_SIZE]; でないとバッファオバーフローとなってしまいます。 理由は...考えてみてください。すぐに分かると思います。(ヒント: strcpy(minValue, ary[ix]);)

  • gimmick
  • ベストアンサー率49% (134/270)
回答No.1

あまりしっかり読んでないのですが、  if(ary[jx] < minValue) のところは  if(strcmp(ary[jx], minValue) < 0) の間違いではないでしょうか? 上の方だと(char*)型のポインタ値の比較になってしまいますよ。

関連するQ&A

  • C++ ソートのやり方

    僕が作ったプログラムで、これはバブルソートなのかわからないので教えてください。 また、ほかのソートの仕方も教えてください。 よろしくお願いします。 汎用関数を使っているのでわかりにくいかもしれないですがお願いします。 #include <iostream> using namespace std; template <class X>void Sort(X *data, int size) { X temp; for (int i = 0; i < size; i++){ for (int j = i + 1; j < size; j++){ if (data[i]>data[j]){ temp = data[i]; data[i] = data[j]; data[j] = temp; } } } } int main() { int i[10]{1, 4, 3, 5, 2, 10, 2, 7, 6, 8}; char c[10]{'c', 'b', 'z', 'a', 'x', 'y', 'j', 'n', 'm', 'r'}; Sort(c, 10); Sort(i, 10); for (int j = 0; j < 10; j++){ cout << i[j] << ' '; } cout << endl; for (int j = 0; j < 10; j++){ cout << c[j] << ' '; } cout << endl; getchar(); return 0; }

  • C++ 静的クラスの役割が分からない

    こんばんは。 C++で静的オブジェクトがなんなのか試しにプログラミングしているんですが どんな役割があるのか試してもよく分かりません。 static class HOGE { public: int *hoge; private: int value[2]; char moji[6]; public: HOGE() { cout << "HOGE\n"; } void MOJI() { strcpy(moji, "MOJI\n"); } void TEST() { cout << "moji = " << moji << endl; } static void staticMOJI() { } }sObj; sObjからは静的メンバ関数と静的メンバ変数は呼び出せるようです。他は呼び出せません。 そのようにするためにstatic宣言をするものなのでしょうか? このsObjからメンバ変数(*hoge)を呼び出すことは無理なんでしょうか?

  • 下記、プログラム内の「char *」の役割

    C++初心者です。 縦長になってしまいますが、 『 #include <iostream.h> void show(int); void show(double); void show(char *);   ←左記の記述の使い方 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char *z) { cout << z << endl; } 』 のプログラムにおいて、「char *」の使い方がいまいち理解できません。 上記プログラムですとエラーが表示されないのですが、下記のプログラムだとエラーが発生します。 『 #include <iostream.h> void show(int); void show(double); void show(char);   //←---------上記と違う行 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char z) {  //←---------上記と違う行 cout << z << endl; } 』 なぜ、ポインタ(*)を付けないといけないのか分かりやすく教えていただけましょうか。

  • char型変数のアドレスを coutで表示するには

    #include <iostream> using namespace std; int main() { bool b; int i; short s; long l; float f; double d; char c; //上で宣言した変数のアドレスを表示 cout << "bool &b " << &b << endl; cout << "int &i " << &i << endl; cout << "short &s " << &s << endl; cout << "long &l " << &l << endl; cout << "float &f " << &f << endl; cout << "double &d " << &d << endl; cout << "char &c " << &c << endl; //「char &c 」とのみ表示される cout << '\n'; //char型のみ printf で再表示 printf("char &c %p\n", &c); //「char &c ********」と表示される return 0; } 上のプログラムを実行すると cout << "char &c " << &c << endl; のところだけ、アドレスが表示されません。 printfを使えば、char型の変数のアドレスも表示されるのですが…。 coutを使ってchar型のアドレスを表示させるにはどうすればいいのでしょうか。 よろしくお願いします。

  • C++

    今、下のようなプログラムを作っています #include <iostream> #include <iostream> using namespace std; int i=0, c=0, n; char str[10]; class X16karax10{ //16進から10進ヘ public: void keisan(); }; void X16karax10::keisan(void){ cout<<"16進を入力して下さい"<<endl; cout<<"英数字は大文字で入力してください(F→○ f→×)" <<endl; scanf("%s",str); while(str[i] != '\0'){ n = n * 0x10; c = str[i++]; if((c >= '0') && (c <= '9')){ n += c - '0'; } else if((c >= 'A') && (c <= 'F')){ n += c - 'A' + 10; } } cout<<("%d\n",n)<<"です\n"<<endl; } int main(){ for(i=0; ; i++){ X16karax10 p; p.keisan(); } } 16進を十進に変えるものなのですがreturn 0を使うと「X16karax10::keisan()' は値を返せない」と、でてしまうのですがどうしたらよいでしょうか?

  • C言語 シンプルソート

    C言語始めて1年の初心者です。 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 10000 void swapData(char *x, char *y); void simpleSort(char data[], int first, int last); int main(int argc, char *argv[]) { int data[MAXSIZE][300]; int i, j, count; FILE *fp; if(argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[0]); exit(0); } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "File %s is not found.\n", argv[1]); exit(0); } for(i = 0; i < MAXSIZE; i++) { if (fscanf(fp,"%s", &data[i]) == EOF) break; } simpleSort(data[], 0, i - 1); for(j = 0; j < i; j++) printf("%s\n", data[j]); } void swapData(char *x, char *y){ char tmp[300]; strcpy(tmp, x); strcpy(x, y); strcpy(y, tmp); } void simpleSort(char data[], int first, int last) { int i, j; for(i = first; i < last; i++){ for(j = i+1; j <= last; j++){ if(strcmp(&data[i], &data[j]) > 0) swapData(&data[i], &data[j]); } } } 読み込んだ文字データをシンプルソートするプログラムなんですが、コンパイルできません。 simpleSortの部分がおかしいみたいなんですが、見直しても先入観からか間違いを見つけられません・・・・ どなたか間違いを指摘していただけたら助かります。

  • C++で,配列に計算した値を入れたいです.

    C++で疑似乱数を発生させてサイコロをつくったのですが, サイコロを5回転がしたときのそれぞれの値をソートしたいと思い,配列に入れれば簡単だと考えてとりあえず配列に入れるプログラムを書いてみました. しかし,int array[i] = Dice(); のところでエラーが出てしまいます. ローベルのC++入門講座という本を使って独学で勉強している初心者なため,配列の使い方がいまいちよくわかりません. ご教授よろしくお願いします. 以下ソースコード #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void InitRand(){ srand((unsigned int) time (NULL)); } int Dice(){ return rand() % 6 + 1; } int main(void){ int n = 0; //nはサイコロを転がす回数 cout <<"サイコロを何回転がしますか?" << flush; cin >> n; cout << "回転がします" << endl; InitRand(); for(int i = 0; i < n; ++i){ cout << Dice() << endl; int array[i] = Dice(); //エラーが発生する.i回目のサイコロの値をi番目の配列に入れたい. } }

  • C言語 ソートについて

    #include <stdbool.h> #include <stdio.h> void swap(char *a, char *b) { } bool is_at(char c) { } void justify(char line[], int n) { } int main(void) { char line[] = "a@b@@@c@@d@@@ef@@g"; size_t n = sizeof(line) - 1; justify(line, n); printf("%s\n", line); return 0; } 上の雛形を使って文字列lineに含まれる@以外の文字を文字列の前の方に詰めていくプログラミングを作るという問題を解いていたのですが下のプログラミングまでは出来たのですが最後のjustifyの部分がわかりません 良ければ解答をお願いします #include <stdbool.h> #include <stdio.h> void swap(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } bool is_at(char c) { if(c == '@') { return true; } else { return false; } } void justify(char line[], int n) { for(int i=0;i<n-1;i++) { } } int main(void) { char line[] = "a@b@@@c@@d@@@ef@@g"; size_t n = sizeof(line) - 1; justify(line, n); printf("%s\n", line); return 0; }

  • プログラムのどこがエラーか教えてください(C++)

    問題は、buildingという基本クラスを作って、建物の階数、部屋数、床の総面積を格納するクラスを作成し、次にhouseという派生クラスを作り、buildingを継承し、寝室数と、浴室数を格納するクラスで、officeという派生クラスを作り、これもbuildingを継承し、消火器台数と、電話台数を格納するクラスを作成し、それぞれの結果を出力せよ。っていう問題です。 下に僕が作成したプログラムを書きます。雑なので見にくいかもしれませんがお願いします。 #include <iostream> using namespace std; class building{ int Floor,Room,Area; public: building(int f,int r,int a){ Floor=f; Room=r; Area=a; } void get_FRA(int &f,int &r,int &a){ f=Floor; Room=r; Area=a; } }; class house:public building{ int Bedroom,Bathroom; public: house(int f,int r,int a,int Be,int Ba):building(f,r,a){ Bedroom=Be; Bathroom=Ba; } void Hshow(); }; class office:public building{ int FireExtinguisher,Telephone; public: office(int f,int r,int a,int fe,int t):building(f,r,a){ FireExtinguisher=fe; Telephone=t; } void Oshow(); }; void house::Hshow() { int x,y,z; get_FRA(x,y,z); cout << "階数:" << x << "階建て" << endl; cout << "部屋数:" << y << "室" << endl; cout << "床の総面積:" << z << "m2" << endl; cout << "寝室:" << Bedroom << "室" << endl; cout <<"浴室:" << Bathroom << "室" << endl; } void office::Oshow() { int x,y,z; get_FRA(x,y,z); cout << "階数:" << x << "階建て" << endl; cout << "部屋数:" << y << "室" << endl; cout << "床の総面積:" << z << "m2"<< endl; cout << "消火器の数:" << FireExtinguisher << "個" << endl; cout << "電話の数:" << Telephone << "個" << endl; } int main() { house H_ob(5,2,300,3,2); office O_ob(3,5,500,2,8); H_ob.Hshow(); O_ob.Oshow(); return 0; } あと、プログラムの処理結果を貼っておきます。 できれば、詳しく教えていただければ幸いです。

  • char型配列について

    基本的なことですが、 char str[5]="Hello"; --> str[0]='H' str[1]='e' str[2]='l' str[3]='l' str[4]='o' str[5]='\0' では、ないのでしょうか? エラーが出ます。 //error C2117: 'str' : 指定された配列には、初期化子が多すぎます。 char str[6]="Hello"; では、コンパイルできます。 ---------------------------------- また、 #include<iostream> using namespace std; int main() { char str[6]="Hello"; cout << str << endl; for(int i=0;i<7;i++) { cout << "i=" << str[i]; if(str[i]=='\0'){cout << " NULL" << endl;} else{cout << endl;} } getchar();return 0; } ----------------------------------------------- とすると、 Hello i=H i=e i=l i=l i=o i= NULL <--ここで、NULLなら、 i=フ <--このぶんは、いらないと思うのですが、、、 となります。 str[6] i=6 は、何を意味するのでしょうか? Visual C++ NET を使用しています。 よろしくお願いします。

専門家に質問してみよう