Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming. 6. Your program should have four arrays of type double, each 500

ID: 673729 • Letter: C

Question

C programming.

6. Your program should have four arrays of type double, each 500 in size, for storing the x position, y position, x velocity and y velocity. Other arrays should also be declared as needed (whatever type and size you feel is appropriate). All arrays should be allocated inside of the main function.

7. Your program should seed the random number generator with the seed and then fill the first number of particles elements in each array. The x and y position arrays should be filled with random numbers between 2 and (graphics window width - 2) or (graphics window height - 2), respectfully. The x velocity and y velocity values should be randomly assigned with values in the range 0.1 to 10.0. Each particle should also have a random color assigned to it – the color should be bright (minimum value of 127 for each of RGB values). The color of a particular particle should be constant throughout a program run.

8. Your program should then loop until Q is pressed, and in the loop it should display each particle at its current x,y position, and then calculate its new position based on its current position and its specific velocity.

9. If a particle runs into the upper or lower edges of the graphics window, the y velocity for that particle should be reversed.

10. If a particle runs into the left or right edges of the window, then the x velocity for that particle should be reversed. 11. The background should be black. Particles should be unfilled circles of radius 3, and should always remain totally in the window, even as they bounce off the edge.

Explanation / Answer

Solution 6 :

struct demo { char f1; double f2 ;};

int main()

{

  double xpArray[500];

  double ypArray[500];

  double xvArray[500];

  double yvArray[500];

char cArray[10];

demo dArray[10];

  char *cptr = cArray;

double *xpptr = xpArray;

  double *ypptr = ypArray;

  double *xvptr = xvArray;

double *yvptr = yvArray;

demo *dptr = dArray;

for(int i=0; i < 5; i++)

{

cout <<double(xpptr) << ", " << double(ypptr) << ", " << double(xvptr) << ", " << double(yvptr) << "," << double(dptr) << endl;

xpptr++; ypptr++, xvptr++, yvptr++, dptr++;

}

cout << xpptr - xpArray << ", ";

cout << ypptr - ypArray << ", ";

cout << xvptr - xvArray << ", ";

cout << yvptr - yvArray << ", ";

cout << dptr - dArray << endl;

return 0;

}

Solution 7 :

Combined Solution 6,7,8,9,10 :

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main ()

{

int i,x,y,gH,gW,numPart;

float xPos[500],yPos[500];

printf("Give Graphics window width, height and number of particles (<500) seperated by commas");

scanf("%d,%d,%d",&gW,&gH,&numPart);

graphics_open(gW,gH);

while (!get_key_state('Q')){

graphics_fill(100,100,100);

for (x=0;x<gW;x++)

{

xPos[x]=(rand()%(gW-4))+2;   

}

for (y=0;y<gH;y++){

yPos[y]=(rand()%(gH-4))+2;   

}

for (i=0;i<numPart;i++){

            graphics_circle_fill(xPos[x],yPos[y],5,0,0,100);   

}   

graphics_update();

         

}

graphics_close();

return 0;

}