Input.txt :
1. M 17 160 13.24
2. M 18 177 13.22
3. M 15 162 14.78
4. F 16 169 15.55
5. F 16 161 14.73
6. F 16 160 10.80
7. M 14 192 15.65
8. F 18 197 12.41
I am now having trouble with the function
(this function was given by the teacher)
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines);
I can only define dAvHeight dAvAge dAvTime as Ints, however later on i need to change them to float to calculate averages. Can anyone explain how i do that while keep using dAvHeight dAvAge dAvTime.
My question is why can i only define the averages as int if in the function it says double? does the function change the format?
Thanks again
Here is the updated code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int iNumberOfLines = 1;
int dAvHeight = 0, dAvAge = 0, dAvTime = 0;
typedef struct {
int ignore;
char Gender;
int Age;
int Height;
float Time;
} Student_t;
int determineNumberOfLines(FILE *pInputFile);
void readInputData(FILE *pInputFile, int iNumberOfLines, Student_t *pStudents);
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines);
// Student_t* searchFastest(Student_t *pStudents, int iNumberOfLines);
// void printToConsole(Student_t *pFastestStudent, double dAvHeight, double dAvAge, double dAvTime);
int main(int argc, char *argv[]) {
FILE *pInputFile = fopen ("resources/Input.txt", "r");
if (pInputFile == NULL){
printf("Fehler beim Öffnen");
return -1;
}
determineNumberOfLines(pInputFile);
//printf("LINES: %d\n",determineNumberOfLines(pInputFile));
Student_t pStudents[iNumberOfLines];
//printf("%d", iNumberOfLines);
readInputData(pInputFile, iNumberOfLines, pStudents);
calculateAverages(dAvHeight, dAvAge, dAvTime, pStudents, iNumberOfLines);
return 0;
}
int determineNumberOfLines(FILE *pInputFile){
fopen ("resources/Input.txt", "r");
int ch;
while(!feof(pInputFile))
{
ch = fgetc(pInputFile);
if(ch == '\n')
{
iNumberOfLines++;
}
}
fclose(pInputFile);
return iNumberOfLines;
}
void readInputData(FILE *pInputFile, int iNumberOfLines, Student_t *pStudents){
fopen ("resources/Input.txt", "r");
int i = 0;
char buffer [120];
while (fgets(buffer, sizeof buffer, pInputFile) != 0)
{
if (sscanf(buffer, "%d. %c %d %d %f", &pStudents[i].ignore, &pStudents[i].Gender, &pStudents[i].Age, &pStudents[i].Height, &pStudents[i].Time) <= sizeof buffer)
{
i++;
}
}
fclose(pInputFile);
}
void calculateAverages(double *dAvHeight, double *dAvAge, double *dAvTime, Student_t *pStudents, int iNumberOfLines){
int i;
int sumHeight = 0;
int sumAge = 0;
float sumTime = 0;
for (i = 0; i <= (iNumberOfLines-1); i++){
sumHeight = sumHeight + pStudents[i].Height;
}
dAvHeight = sumHeight/ iNumberOfLines;
printf("Average Height is = %.2f \n", dAvHeight);
for (i = 0; i<= (iNumberOfLines-1); i++){
sumAge = sumAge + pStudents[i].Age;
}
//dAvAge = sumAge/iNumberOfLines;
printf("Average Age is = %.2f \n", dAvAge);
for (i = 0; i<= (iNumberOfLines-1); i++){
sumTime = sumTime + pStudents[i].Time;
}
//dAvTime = sumTime/iNumberOfLines;
printf("Average Time is = %.2f \n", dAvTime);
}
If you replace:
dAvHeight = sumHeight/ iNumberOfLines;
with this:
*dAvHeight = (double)sumHeight / iNumberOfLines;
Than that should do the trick. You would also have to do something similar to the lines you commented out.
The asterisk is to make sure you are writing to the place where the pointer is pointing and the (double) is in place because otherwise it would first do the division and then the conversion, which could for exmple lead to:
(double)(5 / 2) = (double)2 = 2.0
instead of
(double)5 / 2 = 5.0 / 2 = 2.5
I resolved it by adding
double *dAvHeight;
dAvHeight = malloc(sizeof(dAvHeight));
double *dAvAge;
dAvAge = malloc(sizeof(dAvAge));
double *dAvTime;
dAvTime = malloc(sizeof(dAvTime));
in the main function and removing the int declarations in the start
and by doing what S3gfault suggested
Thanks !
Related
A directed and weighted graph is saved in a file through the list of its edges with the following format: v1 val v2, where indicating that v1 is connected to v2 with an edge with weight val(int > 0).
This is an example of the input file
fF 1 123
A0 2 fF
A0 5 h9
h9 3 123
123 2 F2
123 4 d1
F2 3 Dd
F2 4 d1
d1 2 Dd
d1 4 xd
d1 1 h9
Dd 5 xd
xd 4 A0
F2 3 fF
I have to write a C program that:
read the file and save the graph in an appropriate data structure
after receiving two vertices v1 and v2 and two ints k and p (k<=p), print the path thath starts in v1 and ends in v2 which respects the following constraints
is maximum the sum of the weights
are re-crossed at least k vertices
the complessive number of the re-crossing is at least p
the path ends when it arrives on the destination vertex
I solved the first point but I have no idea for the second. This is all the code I wrote:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "graph.h"
void main() {
int k = 1, p = 1;
char v1[21], v2[21];
graph_t G = GRAPHread("file.txt");
printf("Insert 2 vertex");
scanf("%s %s", v1, v2);
GRAPHfindPath(G, k, p, v1, v2);
}
edge.h
#ifndef EDGE_H
#define EDGE_H
typedef struct edge_s { int v; int w; int wt; } edge_t;
edge_t EDGEcreate(int v, int w, int wt);
#endif
edge.c
#include "edge.h"
edge_t EDGEcreate(int v, int w, int wt) {
edge_t e;
e.v = v;
e.w = w;
e.wt = wt;
return e;
}
ST.h
#ifndef ST_H
#define ST_H
typedef struct symbletable_s *st_t;
int STinsert(st_t st, char *key);
int STsearch(st_t st, char *k);
st_t STinit(int maxN);
#endif
ST.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ST.h"
struct symbletable_s { char **a; int M; };
st_t STinit(int maxN) {
int i;
st_t st = malloc(sizeof(*st));
st->M = maxN;
st->a = malloc(sizeof(char *)*st->M);
return st;
}
int hash(char *k, int M) {
int h = 0, base = 127;
for (; *k != '\0'; k++) h = (base*h + *k) % M;
return h;
}
int full(st_t st, int i) {
if (st->a[i] == NULL) return 1;
return 0;
}
int STinsert(st_t st, char *key) {
int i = hash(key, st->M);
while (full(st,i))
i = (i + 1) % st->M;
st->a[i] = malloc(sizeof(char)*(strlen(key) + 1));
memcpy(st->a[i], key, strlen(key) + 1);
return i;
}
int STsearch(st_t st, char *k) {
int i = hash(k, st->M);
while (full(st, i)) {
if (strcmp(k, st->a[i]) == 0) return i;
else i = (i + 1) % st->M;
}
return -1;
}
graph.h
#ifndef GRAPH_H
#define GRAPH_H
typedef struct graph_s *graph_t;
graph_t GRAPHread(char *s);
void GRAPHfindPath(graph_t G, int k, int p, char *v1, char *v2);
#endif
graph.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"
#include "ST.h"
#include "edge.h"
#define MAX 21
typedef struct node_s *link;
struct node_s { int v; int wt; link next; };
struct graph_s {
int V;
int E;
link *adj_l;
link z;
st_t tab;
};
link NEW(int v, int wt, link next) {
link x = malloc(sizeof *x);
x->v = v;
x->next = next;
x->wt = wt;
return x;
}
graph_t GRAPHinit(int V) {
int v;
graph_t G = malloc(sizeof *G);
G->V = V; G->E = 0; G->z = NEW(-1, -1, NULL);
G->adj_l = malloc(G->V*sizeof(link));
for (v = 0; v < G->V; v++)
G->adj_l[v] = G->z;
G->tab = STinit(V);
return G;
}
void insertE(graph_t G, edge_t e) {
int v = e.v, w = e.w, wt = e.wt;
G->adj_l[v] = NEW(w, wt, G->adj_l[v]);
G->adj_l[w] = NEW(v, wt, G->adj_l[w]);
G->E++;
}
graph_t GRAPHread(char *s) {
graph_t G;
char src[MAX], dst[MAX];
char v1[MAX], v2[MAX];
int nE = 0, i, i1, i2, wt;
FILE *fp = fopen(s, "r");
while (fscanf(fp, "%*s %*d %*s") != EOF)
nE++;
rewind(fp);
G = GRAPHinit(nE * 2);
for (i = 0; i < nE; i++) {
fscanf(fp, "%s %d %s", src, &wt, dst);
i1 = STinsert(G->tab, src);
i2 = STinsert(G->tab, dst);
insertE(G, EDGEcreate(i1, i2, wt));
}
fclose(fp);
return G;
}
void GRAPHfindPath(graph_t G, int k, int p, char *v1, char *v2) {
}
I had a question about circular array population essentially my code prints out random data into a txt file call car_data. I was wondering if someone could help me understand why under the add_it and write_it functions i am using the index%size? under ptr[<argument>]. I also had a question about the add_it part where the function goes to ptr[index++] so the index will increment in the main under the for loop so the array can be populated?
Thanks guys! I'm just trying to learn and will appreciate any clarity
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct sdata{
int brake_fluid_pressure;
int electrical_power;
int driver_brake_pressure;
int pressure;
float time;
} Data_slice;
Data_slice make(int f, int p, int d, int e, float t){
Data_slice piece; //members for each type of data
piece.brake_fluid_pressure = f;
piece.pressure = p;
piece.driver_brake_pressure = d;
piece.electrical_power = e;
piece.time = t;
return piece;
}
int rand_gen(int min, int max);
int add_it(Data_slice * ptr, int size, int index);
void write_it(char *car_data, Data_slice ptr[], int size, int index);
int main()
{
srand(time(NULL));
Data_slice data[100];
int i;
int num = 0;
for (i=0; i<1177; i++){
num = add_it(data, 100, num);
}
write_it("car_data.txt", data, 100, num);
return(0);
}
int rand_gen(int min, int max){
return (rand() % (max + 1 - min)) + min;
}
int add_it(Data_slice * ptr, int size, int index){
Data_slice temp = make(rand_gen(20,30),
rand_gen(10, 15),
rand_gen(20,40),
rand_gen(10,100),
0);
index = index % size;
ptr[index++] = temp; //THIS PART I DO NOT UNDERSTAND!
return index;
}
void write_it(char *car_data, Data_slice ptr[], int size, int index){
FILE * fout = fopen("car_data.txt", "w");
if(fout == NULL){
printf("Error writing! Program will exit");
exit(0);
}
int i, ind;
float t = 0.1;
for(i=index; i<index+size; i++){
ind = i % size;
ptr[ind].time = t;
fprintf(fout, "%5.1f%20d%20d%20d%20d\n", ptr[ind].time,
ptr[ind].brake_fluid_pressure,
ptr[ind].pressure,
ptr[ind].driver_brake_pressure,
ptr[ind].electrical_power);
t += 0.1;
}
fclose(fout);
}
% is the modulus operator. index % size returns the remainder of dividing index by size. This is used to make the index wrap around to 0 when it goes beyond the size of the array, and that's what makes the array "circular".
ptr[index++] = temp;
assigns temp to ptr[index], and then increments index. This updated value of index is then returned by the function, and main assigns it back to the num variable.
I've written a code with a main body plus a function merge() that merges two arrays arr1[] and arr2[] and stores them in a third array, *arr3[].
I'm still confused by the pointers and memory allocation. What sort of command should I place in my code to tell it to put the two merged arrays into a third array, *arr3[]?
I tried writing *arr3 = (double *) malloc (*sz3) but I received an error "ld returned 1 exit status".
Here is the code:
/* This is a function that will merge contents of arr1 and arr2 and store in arr3 as a
pointer */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void merge(double arr1[], double arr2[], double *arr3[], int sz1, int sz2, int *sz3);
void print_arr(double arr[], int sz);
int main(){
double v0[] = {};
double v1[] = {22.0, 30.1, 35.2, 49.2, 56.4, 65.1, 76.4, 79.6, 86.2, 88.9};
double v2[] = {4.6, 9.0, 13.0, 47.8, 66.9, 68.7, 71.0};
double v3[] = {80.1, 92.7, 97.4, 102.3, 105.0, 112.3, 121, 136.7, 163.4, 177.9};
int sz1 = 10, sz2 = 7, sz3 = 10, szres;
double *res;
printf("\nTEST 1\n======\n");
merge(v1, v2, &res, sz1, sz2, &szres);
print_arr(res,szres);
free(res);
printf("\nTEST 2\n======\n");
merge(v0, v3, &res, 0, sz3, &szres);
print_arr(res,szres);
free(res);
printf("\nTEST 3\n======\n");
merge(v2, v3, &res, sz2, sz3, &szres);
print_arr(res,szres);
free(res);
return 0;
}
/* function to print the array */
void print_arr(double arr[], int sz){
for(int i = 0; i < sz; i++){
printf("%7.2lf",arr[i]);
}
printf("\n");
}
void merge(double arr1[], double arr2[], double *arr3[], int sz1, int sz2, int *sz3) {
int i, j, k;
j = k = 0;
for (i = 0; i < (sz1+sz2);) {
if(j < sz1 && k < sz2) {
if (arr1[j] < arr2[k]) {
*arr3[i] = arr1[j];
j++;
} else {
*arr3[i] = arr2[k];
k++;
}
i++;
} else if (j == sz1) {
for (; i < (sz1 + sz2);) {
*arr3[i] = arr2[k];
k++;
i++;
}
} else {
for (; i < (sz1+sz2);) {
*arr3[i] = arr1[j];
j++;
i++;
}
}
}
}
Two things:
The function parameter double *arr3[] is an array of double *. You're instead passing in a pointer to a double *, so you need to define it as double **arr3:
void merge(double arr1[], double arr2[], double **arr3, int sz1, int sz2, int *sz3) {
To properly allocate the right amount of memory, you need to first assign a value to *sz3. Then you can allocate space for that many doubles.
Right before the initial for loop in merge, add the following:
*sz3 = sz1+sz2;
*arr3 = malloc((*sz3) * sizeof(double));
I'm developing a program that read from CSV file and calculate score with a method "calculateMLpa". The method receive array of char and array of 10 float, and transform array of float in matrix 3x3. When read the position 3rd number from array, insert in matrix the 4th number and same for 6th number.
I.E.
array value[]={0.000000;123.814934;234.000000;100.000000;166.000000; 203.086639;383.000000;186.000000;338.000000;173.098419 }
array traj[]={"0-0";"0-1";"0-2";"1-0";"1-1";"1-2";"2-0";"2-1";"2-2"}
Xn_val[]={"0","1","2"}
When transform in matrix the result is:
123.814934 234.000000 166.000000
166.000000 203.086639 186.000000
186.000000 338.000000 173.098419
While the expected for [0;2] is 100.000000 and for [1;2]=383.000000, but when print the currently value of traj it's correct.
How can I fix this problem?
The code is all here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include <ctype.h>
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
int csv_parse ( char *line, int size )
{
char *p;
char *dp;
int inquote;
int na;
int nTo_comma;
char prevc = ',';
char *list[256];
dp = NULL;
// inquote = 0;
na = 0;
prevc = ';';
nTo_comma=0;
for ( p = line; *p != '\n'; p++ )
{
nTo_comma++;
list[nTo_comma] = p;
if(*p == prevc)
{
printf("%s\t", list);
return na;
}
}
printf("\n");
return na;
}
double calculateMLpa(const char *Xn_val[], char *traj[], float value[], double alphaxixj, double tauxi, int sz, int dim) {
double mlx = 0;
double v;
double alphaxi;
char *state;
int i;
int p;
int j;
int k;
// int sz = sizeof(Xn_val) / sizeof(int);
// int dim = sizeof(traj) / sizeof(char);
double trns[sz][sz];
double m[sz];
char *trat="-";
// m[xi] values: the number of transitions leaving the state xi
printf("%d %d \n",sz,dim);
int cont=0;
for (i = 0; i <= sz; i++) {
m[i] = 0.0;
for (j = 0; j <= sz; j++) {
v = 0.0;
int newlength = strlen(Xn_val[i])+strlen(trat)+strlen(Xn_val[j])+1;
state = malloc(sizeof(char)*newlength);
if(state != NULL){
state[0] = '\0';
strcat(state,Xn_val[i]);
strcat(state,trat);
strcat(state,Xn_val[j]);
printf("%s ",state);
}else {
printf(stderr,"malloc failed!\n");
}
// for (k=0; k<=dim;++k){
if (traj[cont] != NULL ){
if (strcmp(traj[cont],state)==0){
v = value[cont+1];
printf("%f \n",v);
}
}
trns[i][j] = v;
printf("%f - \n",trns[i][j]);
if (strcmp(Xn_val[i],Xn_val[j])!=0)
m[i] = m[i] + v;
cont++;
}
}
for (i=0;i<=sz;++i){
for(j=0;j<=sz;++j){
printf("%f ",trns[i][j]);
}
printf("\n");
}
for (p=0;p<=sz;++p){
printf("%f - \n",m[p]);
}
printf("%f %f\n",trns[0][1],trns[0][2]);
alphaxi = alphaxixj * (((double) sz) - 1.0);
alphaxi = alphaxixj;
printf("%d ",sz);
for (i = 0; i <= sz; i++) {
for (j = 0; j <= sz; j++) {
// xi!=xj
if (strcmp(Xn_val[i], Xn_val[j])!=0) {
mlx = mlx + lgamma(alphaxixj + trns[i][j]) - lgamma(alphaxixj);
}
// xi
else {
mlx = mlx + lgamma(alphaxi) - lgamma(alphaxi + m[i]);
mlx = mlx + lgamma(alphaxi + m[i] + 1.0)+ (alphaxi + 1.0) * log(tauxi);
mlx = mlx - lgamma(alphaxi + 1.0)- (alphaxi + m[i] + 1.0) * log(tauxi + trns[i][j]);
}
}
}
return (mlx);
}
#define MAXFLDS 200 /* maximum possible number of fields */
#define MAXFLDSIZE 32 /* longest possible field + 1 = 31 byte field */
void parse(char *record, char *delim, char arr[][MAXFLDSIZE], int *fldcnt) {
char*p = strtok(record, delim);
int fld = 0;
while (p) {
strcpy(arr[fld], p);
fld++;
p = strtok('\0', delim);
}
*fldcnt = fld;
}
void main() {
printf("inizio\n");
FILE *pf;
int N=20;
bool first=true;
const char *a[]={"0","1","2"};
char *traject[]={"0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2"};
double bs=0;
char *trat="-";
pf=fopen("//home//user//prova.csv","r");
float array[10][10];
float *t;
char *str= "hello";
char *state;
t = (float *)malloc(N * sizeof(float));
int f=0;
if (pf)
{
size_t i, j, k;
char buffer[BUFSIZ], *ptr;
/*
* Read each line from the file.
*/
for ( i = 0; fgets(buffer, sizeof buffer, pf); ++i )
{
/*
* Parse the comma-separated values from each line into 'array'.
*/
for ( j = 0, ptr = buffer; j < ARRAYSIZE(*array); ++j, ++ptr )
{
array[i][j] = strtof(ptr, &ptr);
}
}
fclose(pf);}
else /* fopen() returned NULL */
{
perror(pf);
}
for(f=0; f<10; ++f){
if(f==0){}
else if(f==1 && array[f][8]==0)
array[f][8]=123.8149353;
t[f]=array[f][8];
//printf("%f \n",t[f]);
}
for (f=0;f<10; ++f){
printf("%f - ",t[f]);
}
//printf("%s, %s, %s \n",a[0],a[1],a[2]);
printf("start\n");
int sz = sizeof(a) / sizeof(char);
int dim = sizeof(traject) / sizeof(char);
printf("%d , %d \n",sz,dim);
bs=calculateMLpa(a,traject,t,1.0,0.1,sz,dim);
printf("done \n");
printf("%f ",bs);
}
EDIT
I try to pass array size
sz=sizeof(a)/sizeof(char)
dim = sizeof(traject) / sizeof(char);
but their value is 24 and 72 respectively, and the execution stops at 0-2 value 100.000000
Arrays passed to functions decay to pointers to the start of the array. So
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
Will not return anything meaningful when checking for its size in that case
To fix, pass the Array size as an additional Argument.
One major problem is that when you pass arrays to functions, they decay to pointers, and the sizeof trick you use to get the array size will not work.
You need to pass the actual array sizes as arguments.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
#define h 1
#define XY0 0
#define MAX_XY 5
#define N 2 //particles per subdomain
#define BLOCKS 4
#define a 1
#define b 1
float velocityX(float x, float y);
float velocityY(float x, float y);
int malloc2dfloat(float ***array, int length);
int main (int argc, char **argv)
{
typedef struct {
float xcoord;
float ycoord;
float velx;
float vely;
} particle;
int points= (int) floor((MAX_XY - XY0)/h) + 1;
int procsize = 2;
int myid, nproc;
MPI_Datatype particletype, oldtypes[1];
MPI_Aint offset[1], extent;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
int startElementX, startElementY, endElementX, endElementY;
particle* sub_pars = (particle*)malloc(sizeof(particle)*N);
offset[0] = 0;
int blockcounts[1];
blockcounts[0] = 4;
oldtypes[0] = MPI_FLOAT;
MPI_Type_struct(1, blockcounts, offset, oldtypes, &particletype);
MPI_Type_commit(&particletype);
particle* particles = (particle*)malloc(sizeof(particle) * N * procsize*procsize);
if (nproc != procsize*procsize){
printf("Must use np=4 -- split into 4 blocks");
MPI_Abort(MPI_COMM_WORLD,1);
}
srand(time(NULL)+myid);
if (myid == 0)
{
float mins[4];
startElementX = 0;
startElementY = 0;
endElementX = (points/procsize)-1;
endElementY = (points/procsize) -1;
}
else if (myid == 1)
{
startElementX = 0;
startElementY = (points/procsize);
endElementX = (points/procsize) -1;
endElementY = points - 1;
}
else if (myid == 2)
{
startElementX = (points/procsize);
startElementY = 0;
endElementX = points - 1;
endElementY = (points/procsize) -1;
}
else
{
startElementX = (points/procsize);
startElementY = (points/procsize);
endElementX = points-1;
endElementY = points-1;
}
int i;
float localmin;
float mag;
for (i=0; i<N; i++)
{
sub_pars[i].xcoord = ((startElementX + rand()/(RAND_MAX / (endElementX-startElementX+1)+1)))*h + XY0;
printf("%f\n", sub_pars[i].xcoord);
sub_pars[i].ycoord = ((startElementY + rand()/(RAND_MAX / (endElementY-startElementY+1)+1)))*h + XY0;
sub_pars[i].velx = velocityX(sub_pars[i].xcoord, sub_pars[i].ycoord);
sub_pars[i].vely = velocityY(sub_pars[i].xcoord, sub_pars[i].ycoord);
mag = sqrt(sub_pars[i].velx*sub_pars[i].velx + sub_pars[i].vely*sub_pars[i].vely);
if (i==0 || localmin > mag) localmin = mag;
}
printf("localmin of %d is %.2f \n", myid, localmin);
MPI_Allgather(&sub_pars, 1, particletype, particles ,1, particletype, MPI_COMM_WORLD);
MPI_Finalize();
if(myid == 0)
{
int k;
for (k=0; k<N*4; k++)
{
printf("test %.2f \n", particles[i].xcoord);
}
}
return 0;
}
float velocityX(float x, float y)
{
float temp = (a+(b*(y*y-x*x))/((x*x+y*y)*(x*x+y*y)));
return temp;
}
float velocityY(float x, float y)
{
float temp = (-1*(2*b*x*y)/((x*x+y*y)*(x*x+y*y)));
return temp;
}
It just returns the same value for all the particles, but I know they are being calculate correctly within each thread, so something is wrong with my MPI_Allgather, can someone please explain how it should look?
You have made a very common mistake: the & (address-of) operator in the first argument that you pass to MPI_Allgather is unnecessary. sub_pars is already a pointer and calling MPI_Allgather with &sub_pars passes a pointer to the pointer (a location somewhere in the stack frame of the main() routine) instead of pointer to the actual data.