- ベストアンサー
openMPによる人口のシミュレーション
- openMPを使用して人口のシミュレーションを高速化する方法を模索しています。
- 人口が増えるたびに計算回数が増えるため、効率的な方法を見つけたいと考えています。
- 現在のソースコードの一部を示しましたが、文字制限のため全てを掲載できません。
- みんなの回答 (5)
- 専門家の回答
関連するQ&A
- openMPにしたいのですがお手伝いください。
void calc_local_density(INDIVIDUAL *list){ INDIVIDUAL *indiv1, *indiv2; double sum; indiv1=list->next_indiv; for(; indiv1; indiv1=indiv1->next_indiv){ sum = 0.0; indiv2=list->next_indiv; for(; indiv2; indiv2=indiv2->next_indiv){ sum += weight( distance(indiv1, indiv2) ); } indiv1->local_density = sum; } } } この部分をopenMPにしたいのですが、 内側のfor文はreductionでできると考えたのですが 外側のfor文は依存があり上手くいきません。 (1)このopenMPのコツや知恵をお貸ししていただいたらうれしいです♪ (2)また2重for文のときにopenMPを使用する際に どのように書けばいいのかがわからず困っています。 ご協力お願いいたします。。
- ベストアンサー
- C・C++・C#
- アルゴリズム 線形リスト
最近リストについて習い始めました。入力したデータと同順に並ぶリストを作成しようと思い、コードを打ったのですが…動作中止の表示がでてしまいました。どこが間違っているのか、ずっと悪戦苦闘して組んでいるのですが、全く出口が見えてきません。何が間違えているのか、はたまた根本的に違うのか、ご指導して頂けると有難いです。 以下、コードです。 #include <stdio.h> #include <stdlib.h> #include <string.h> struct hito{ char name[20]; int age; struct hito *next; }; void main(void){ struct hito *p, *head, *dummy; char new_name[20]; int new_age; dummy = (struct hito *)malloc(sizeof(struct hito)); head = dummy; dummy->next = p; dummy = p; while (scanf("%s %d" , new_name, &new_age) != EOF) { p = (struct hito *)malloc(sizeof(struct hito)); strcpy(p->name, new_name); p->age = new_age; p->next = head; head = p; } while(p != NULL) { printf("\t%-20s %3d\n" , p->name, p->age); p = p->next; } }
- ベストアンサー
- C・C++・C#
- リスト構造を使ってSortとSearchをするプログラム
タイトルのとおりのプログラムを組んでみました ----------himajin.c----------- #include <stdio.h> #include <string.h> #include <stdlib.h> // 参考:http://www9.plala.or.jp/sgwr-t/c/sec15-5.html // コンパイラ:BCC 5.5 // アイテムの加え方が違う。 struct Item { int data; /* データ */ struct Item *next; /* 次のデータへのポインタ */ }; struct Item *head; struct Item *tail; struct Item *addItem(struct Item *p,int newdata){ struct Item *t; t->data = newdata; t->next = tail; p->next = t; return t; } int main(){ struct Item *item; int inputdata; head = item; item->next = tail; while (scanf("%d", &inputdata) != EOF) { item = addItem(item,inputdata); } /* search(5); sort(); */ return 0; } int sort(){ struct Item *j; struct Item *p; struct Item *t; p = head; t = head; while (p->next != tail){ while (p->data > t->data){ j = t; t = t->next; } j->next = p; p->next = t; p = p->next; } } int search(int searchdata){ struct Item *t; t = head; tail->data = searchdata; while (searchdata != t->data){ t = t->next; } if(t == tail){ printf("Not Found"); } else{ printf("Found"); /*後で考える*/ } return 0; } ---- ...が、コマンドラインで himajin.exe 5 と入力してみたところ、いきなりプログラムが落ちました。何がいけないのでしょうか?
- ベストアンサー
- C・C++・C#
- MIPSアセンブラ言語について
#include NULL 0 struct list{ struct list +next; int value; }; int sumvalue(sturuct list *head){ struct list *cur=NULL; int sum=0; for(cur = head; cur !=NULL; cur=cur->next){ sum += cur->value; } return sum; } このC言語で書かれた関数をMIPSアセンブラで記述するとどうなるのでしょうか?ポインタで混乱してます。
- 締切済み
- その他(プログラミング・開発)
- 線形リストについて。
このたび線形リストを学習し、自分でサンプルコードを書くことにしました。 しかし、動かすことが出来ず、理解することが出来なかったため、ご相談致します。 コード: #include <stdio.h> #include <stdlib.h> #define N 10 typedef struct node *link; struct node { int item; link next; }; void main() { int i; link head ,t; head = NULL; //リストに要素を入れる。 for(i = 0,t = head; i < N; i++, t = t->next) { t = malloc(sizeof *t); t ->next = NULL; t ->item = i; } //リストを表示する for(t = head; t != NULL; t = t->next) { printf("%d\n", t->item); } } 実行結果: リストを表示するのfor文内、t->itemでエラー。 原因はheadを上手くポインタで指せていないことにあると思います。 お聞きしたいことは2点です。 1.なぜ意図したように上手く動かないのか。 2.このように動かしたい場合、何処を変更すれば良いのか。 以上です。 宜しくお願いします。
- ベストアンサー
- C・C++・C#
- 領域を確保した後の文字の並び替え
領域を確保した後、アドレスを書き変える勉強をしています。入力した文字を逆から表示していくことに成功したので今度はソートで小さい順に並び替えをさせようとしまして、色々試したのですが何度やっても上手くいきません。 どのように組めばいいか教えてください。よろしくお願いします。 #include<stdio.h> #define MAX 40 struct parts * ListChar(char moji[]); typedef struct parts{ struct parts *next; char moji; }PARTS; main(void){ char moji[MAX]; PARTS *list; PARTS *head; scanf("%s",moji); head = ListChar(moji); for(list=head ; list!=NULL ; list=list->next){ printf("%c",list->moji); } printf("\n"); for(list=head ; head!=NULL ; list=head){ head = list->next; free(list); } } struct parts * ListChar(char moji[]){ int i,j,len1; char dam; PARTS *head; PARTS *list; PARTS *back; PARTS *start; PARTS *list2; len1 = strlen(moji); for(i=0 ; i<len1 ; i++){ list = (struct parts *)malloc(sizeof(struct parts)); /*領域の確保*/ list->next = NULL; if(i == 0){ head =list; }else{ back->next =list; } back = list; list->moji = moji[i]; } head = back; return head; }
- 締切済み
- C・C++・C#
- C言語 リスト
(1) /* list.c */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; main() { struct node *head, *p; int x; head = NULL; while(scanf("%d",&x) != EOF) { p = (struct node *)malloc(sizeof(struct node)); p->num = x; p->next = head; head = p; } // リストの要素のnumの値を先頭から順に表示する p=head; while(p!=NULL) { printf("%d\n" ,p->num); p = p->next; } } (2) struct node *q; q = head; while(q->next != NULL) q = q->next; (1)を(2)を使い新しいノードをリストの最後に追加するようにしたいのですが どう書いたら良いのか教えていただきたいです
- 締切済み
- その他(学問・教育)
- C言語のリストのソートについて質問します。
C言語のリストのソートについて質問します。 こんばんは、aida13です。以前の質問が自己解決しました。回答者のみなさん本当にすみませんでした。 今回はそのアルゴリズムで改善したい点がありましたので投稿させていただきました。以下のソートを実行するとソート自体はうまくいくのですが、見ての通りwhileやifなどを使って何度も実行しなおさないと完全にソートできません(「5 4 3 2 1」→「4 3 2 1 5」(一回ソート))。これを一回でソートできるようにしたいのです。また、降順も同様にそのままsecondとheadを入れ替えたのですが…今度は何度やっても「1 2 3 4 5」→「2 1 3 4 5」(一回ソート)からそれ以上動きません。アドバイスをお願いします。また、出来る限りこの形を保てる形でお願いします。 以下自作ソート #include < stdio.h > #include < stdlib.h > struct ans { int data; struct ans *next; }; struct ans *sort_up(struct ans *head) { int temp; struct ans *second_head; second_head = (struct ans *)malloc(sizeof(struct ans)); second_head = head->next; if( head->next == NULL ) { return head; } else { if( head->data > second_head->data ) { temp = head->data; head->data = second_head->data; second_head->data = temp; sort_up(head->next); } else { sort_up(head->next); } return head; } } struct ans *add_list(int x, struct ans *head) { struct ans *new_head; new_head = (struct ans *)malloc(sizeof(struct ans)); new_head->data = x; new_head->next = head; return new_head; } void show_list(struct ans *head) { if( head == NULL ) { printf("\n\n"); } else { printf("%d ",head->data); show_list(head->next); } } void main() { struct ans *head; head = NULL; head = add_list(1,head); head = add_list(2,head); head = add_list(3,head); head = add_list(4,head); head = add_list(5,head); do { printf("input:"); scanf("%d",&a); if(a==0) { head = sort_up(head); show_list(head); } } while(a==0); }
- ベストアンサー
- C・C++・C#
- リスト構造のソートで悩んでます。。。
リスト構造のソートで悩んでます。プログラムの内容はファイルからデータをリスト構造の構造体に読み込み、名前順にソートし結果を表示する。というものです。データの追加や削除はできるのですがソートとなると頭が混乱してしまいお手上げ状態になってしまいました。。。。。 読み込み用のデータとプログラムソースを以下に記載するのでどなたか良きアドバイスをお願いしますm(_ _)m ○データ Sakuragi 16 Rukawa 16 Miyagi 17 Akagi 18 Mitsui 18 ○ソース #include <stdio.h> #include <stdlib.h> #include <string.h> #define MENBER 5 typedef struct data{ char name[BUFSIZ]; int age; struct data *next; }LIST; LIST *newLIST(void); LIST *sort(LIST *); int main(int argc,char *argv[]){ FILE *fp; LIST *p; LIST *np; LIST *npb; LIST *head; char namae[BUFSIZ]; int toshi,i; if((fp=fopen(argv[1],"r"))==NULL){ printf("no file\n"); exit(1); } head = newLIST(); npb =head; for(i=0;i<MENBER;i++){ np = newLIST(); fscanf(fp,"%s %d",namae,&toshi); strcpy(np->name,namae); np->age = toshi; npb->next =np; npb = np; } sort(head); for(p=head->next;p != NULL;p=p->next){ printf("%s\t%d\n",p->name,p->age); } for(p=head->next;p != NULL;p=np){ np = p->next; free(p); } fclose(fp); return(0); } LIST *newLIST(){ LIST *p; p = (LIST *)malloc(sizeof(LIST)); p->next = NULL; return(p); } LIST *sort(LIST *head){ }
- ベストアンサー
- C・C++・C#
- クイックソートの処理速度に関する実験 要素1万個、
クイックソートの処理速度に関する実験 要素1万個、2万個、3万個の配列変数にランダムな値を代入し、・その後クイックソートで小さい順に並べ替える #include<stdio.h> #include<stdlib.h> #include<time.h> #define ASIZE 10000 #define RAND_SEED 0x1131000 void my_sort(int left, int right, int a[]); int main(void){ clock_t start, end; int i,a[ASIZE]; srand(RAND_SEED); for(i=0;i<ASIZE; i++){ a[i]=rand(); } start=clock(); my_sort(0, ASIZE-1, a); end=clock(); printf("%.3f秒でした" ,(end-start)/(double)CLOCKS_PER_SEC); getchar(); return 0; } void my_sort(int left, int right, int a[]){ ここに入れるプログラムがわかりません return; }
- 締切済み
- C・C++・C#
お礼
返信が遅くなってしまい申し訳ございませんでした。 mainのfor文にやはり依存性があり、 ポインタを使っているのでなかなか並列できない状態でございます。 また質問するかと思いますのでその都度はまたお願いできますでしょうか? 親切に教えていただきありがとうございました!!