Don;t think my idea are gonna help somehow I tried it and got compilation errors
Don;t think my idea are gonna help somehow I tried it and got compilation errors
OK so I change it back apart from he argv thing and it seems to work.
OK I compiled it and it ran OK.
This code works
I moved the malloc inside the loop
Code:#include <stdio.h> #include <math.h> #include <conio.h> typedef double real; typedef struct { real xpos; real ypos; real zpos; real uvel; real vvel; real wvel; real omegax; real omegay; real omegaz; real radius; real mass; } sphere; void update_position(sphere *ball, real dt) { //(*ball).xpos ball->xpos=ball->xpos + dt*ball->vvel; //ypos,zpos } void detect_ball(sphere *ball1, sphere *ball2, real *pdist) { *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+ (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos)); // if ( dist<= (ball1->radius + ball2->radius) +0.0075 ) // { // if ( dist>= ball1->radius + ball2->radius -0.0075 ) // { // *ptime=0; // } // } } int main(int argc, char *argv) { real dt=0.01; real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5, dist[2], ballmatxpos[101][2]; int nballs=1 ; int i,j,m,k; sphere *ball; mlimit=tmax/dt; //initialise positions for(i=0;i<=nballs;i++) { ball=(sphere *)malloc(nballs*sizeof(sphere)); ball[i].radius=0.5; ball[i].ypos=0.0; ball[i].ypos=0.0; ball[i].uvel=1.0; ball[0].vvel=1.0; ball[1].vvel=0.0; } //pendulum to be released // ball[0].xpos=0; //ball[0].ypos=0; // firstvvel=0; //timestep loop t=0.0; dist1=0.0; while(t<tmax) { for(m=0;m<=mlimit;m++) { //update position for(i=0;i<=nballs;i++) { update_position(&(ball[i]), dt); } //collision detection for(i=0;i<=nballs;i++) { for(j=0;j<=nballs;j++) { detect_ball(&(ball[i]),&(ball[j]), &dist1); if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 ) { if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075 ) { // ball2->uvel=ball1->vvel; //yes collision, modify velocities and rotation rates t=0; } } } } ballmatxpos[m][0]=t; ballmatxpos[m][1]=ball[0].xpos; printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t ); t+=dt; } } // ---------------//------------------------- FILE *pFile1; FILE *pFile2; pFile1 = fopen("graphing3.txt", "w"); pFile2 = fopen("graphing4.txt", "w"); if (pFile1 != NULL) { for(m=0;m<=mlimit;m++) { fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]); } fprintf( pFile1,"\n"); fclose(pFile1); } if (pFile2 != NULL) { for(m=0;m<=mlimit;m++) { fprintf( pFile2, " %3.4lf \n", ballmatxpos[m][1]); } fprintf( pFile2,"\n"); fclose(pFile2); } else { printf("inda dapat print bro"); } // ----------------------------------------------------- getch(); return 0; }
This line causes a problem I accidental change it from
toCode:ball[i].xpos=(real) i * (2*ball[i].radius + gap);
OK I said y not x but the issue is in the line I mention so it seems.Code:ball[i].ypos=0.0;
I ran it and got a segmentation fault.The code I posted runs OK
Furthermore, the spamming comment was in reference to posting multiple times in a row, all within a few minutes of each other. We're already halfway down the second page of this thread, and it shouldn't be that long. There's no reason you couldn't have added further comments to your previous posts with an edit.
Thanks for you opinion but I disagree with all of it,.
My opinion is that if you have nothing more constructive to do than criticise people who are trying to help them the most helpful thing you could do would be to shut
If you have nothing constructive to say say nothing.
I post in my style not yours, you don't own me.
If you don't like my posts there is a simple solution do not read them. You have free will don't you?
You mind is not so inflexible that such petty matters bother you surely?
Last edited by esbo; 01-11-2013 at 04:21 PM.
For anyone who wants a comedy read, just search for esbo's past posts.
esbo has been here on and off for a long while, but seems to have learnt sweet FA about programming in that time.
Best regarded as a troll IMO.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Actually, it was earlier today I stumbled upon the famous "pointer" thread by said poster. I was trying to find it again, but to no avail.
The above code works I changed this lineCode:#include <stdio.h> #include <math.h> #include <conio.h> typedef double real; typedef struct { real xpos; real ypos; real zpos; real uvel; real vvel; real wvel; real omegax; real omegay; real omegaz; real radius; real mass; } sphere; void update_position(sphere *ball, real dt) { //(*ball).xpos ball->xpos=ball->xpos + dt*ball->vvel; //ypos,zpos } void detect_ball(sphere *ball1, sphere *ball2, real *pdist) { *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+ (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos)); // if ( dist<= (ball1->radius + ball2->radius) +0.0075 ) // { // if ( dist>= ball1->radius + ball2->radius -0.0075 ) // { // *ptime=0; // } // } } int main(int argc, char *argv) { real dt=0.01; real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5, dist[2], ballmatxpos[101][2]; int nballs=1 ; int i,j,m,k; sphere *ball; mlimit=tmax/dt; //initialise positions for(i=0;i<=nballs;i++) { ball=(sphere *)malloc(nballs*sizeof(sphere)); ball[i].radius=0.5; // ball[i].ypos=0.0; // ball[i].xpos=(real) i * (2*ball[i].radius + gap); // ball[i].xpos=(real) i * (2*ball[i].radius + gap); ball[i].xpos=0.0; ball[i].ypos=0.0; ball[i].uvel=1.0; ball[0].vvel=1.0; ball[1].vvel=0.0; } //pendulum to be released // ball[0].xpos=0; //ball[0].ypos=0; // firstvvel=0; //timestep loop t=0.0; dist1=0.0; while(t<tmax) { for(m=0;m<=mlimit;m++) { //update position for(i=0;i<=nballs;i++) { update_position(&(ball[i]), dt); } //collision detection for(i=0;i<=nballs;i++) { for(j=0;j<=nballs;j++) { detect_ball(&(ball[i]),&(ball[j]), &dist1); if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 ) { if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075 ) { // ball2->uvel=ball1->vvel; //yes collision, modify velocities and rotation rates t=0; } } } } ballmatxpos[m][0]=t; ballmatxpos[m][1]=ball[0].xpos; printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t ); t+=dt; } } // ---------------//------------------------- FILE *pFile1; FILE *pFile2; pFile1 = fopen("graphing3.txt", "w"); pFile2 = fopen("graphing4.txt", "w"); if (pFile1 != NULL) { for(m=0;m<=mlimit;m++) { fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]); } fprintf( pFile1,"\n"); fclose(pFile1); } if (pFile2 != NULL) { for(m=0;m<=mlimit;m++) { fprintf( pFile2, " %3.4lf \n", ballmatxpos[m][1]); } fprintf( pFile2,"\n"); fclose(pFile2); } else { printf("inda dapat print bro"); } // ----------------------------------------------------- getch(); return 0; }
Worked on my machine anyway so an issue with that line in some way.Code:// ball[i].xpos=(real) i * (2*ball[i].radius + gap); ball[i].xpos=0.0;
It produced two files with data in anyway
graphing4.txt
Code:NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
graphing3.txt
Code:0.0000 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900 0.3000 0.3100 0.3200 0.3300 0.3400 0.3500
Then how do you explain your post #14? Nothing constructive there, so why say it? For that matter, posts 11-14 and 16-18 are basically you just thinking "out loud" on the forum, and post #11 is more likely to confuse somebody than help, with you just throwing around random syntax. Take the time to gather your thoughts, try out your ideas and make one meaningful post. All those little, one sentence, non-constructive posts just help derail the thread faster. That may be a matter of opinion, but I would wager a fair sum of money that it is an opinion agreed upon by the majority of the forum regulars.
Furthermore, Matticus' seg fault statement, at least, is not an opinion, but a pure fact. It is also a sign that your code definitely does not work. Again, test your code thoroughly before
We're not trying to be a-holes about this, we're just trying to keep the thread more on track of the OP's actual problem, instead of a thread for you to write your thoughts out as they happen.
Yes, this post is itself off-topic, but hopefully explains where we are coming from enough that we can all just drop this side-issue and return to helping people with programming, hopefully in a more productive manner.
Last edited by anduril462; 01-11-2013 at 04:37 PM.