Working in the C language, please help: For this project, you will write a C pro
ID: 3760295 • Letter: W
Question
Working in the C language, please help:
For this project, you will write a C program that will simulate and animate an ecosystem populated by two types of creatures: doodlebugs and ants. In this assignment, doodlebugs are the predators and ants are the prey. As the simulation runs, you should usually see the doodlebug population and the ant population grow and shrink in a cyclic manner; however, the built-in randomness many cause one or both creatures to die out completely.
this is on a 20x20 grid, with 100 ants and 5 doodlebugs in the beginning, use a 2D array for these as well as pointers and functions
please help and comment, no C++, just C,
thank you
Explanation / Answer
#include "Stdio.h" #include "conio.h" #include "math.h" #include "graphics.h" void intgraph(); void display_cordinate(char bg_color,char line_color); int Create_poly(int poly[][2]); void fill_poly(int poly[][2],int points,char line_color,char fill_color); void Identity(float Matrix[3][3]); void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis); void main() { int poly[3][2],i,points,Xdis,Ydis; float Sx,Sy,Matrix[3][3],temp,Cos,Sin; char ch; intgraph(); setfillstyle(8,RED); points=Create_poly(poly); display_cordinate(DARKGRAY,WHITE); fill_poly(poly,points,WHITE,RED); getch(); Identity(Matrix); Xdis=0-poly[0][0]; Ydis=0-poly[0][1]; Translate_to_Relative(poly,points,Xdis,Ydis); Menu: restorecrtmode(); do { clrscr(); printf(" ================================"); printf(" 1. Scaling"); printf(" 2. Translation"); printf(" 3. Rotation"); printf(" 4. Reflection [ X axis ]"); printf(" 5. Reflection [ Y axis ]"); printf(" 6. Reflection [ X=Y axis ]"); printf(" 7. Draw "); printf(" 8. EXIT "); printf(" ================================"); printf(" Choose Ur Destiny:- "); ch=getche(); }while((ch<'1')||(ch>'8')); switch(ch) { case '1': printf(" Enter Scaling Ratio:- "); printf(" Sx:- "); scanf("%f",&Sx); printf(" Sy:- "); scanf("%f",&Sy); for(i=0;i<3;i++) { Matrix[i][0]=Matrix[i][0]*Sx; Matrix[i][1]=Matrix[i][1]*Sy; } goto Menu; case '2': printf(" Enter Translation [Relative]:- "); printf(" Tx:- "); scanf("%f",&Sx); printf(" Ty:- "); scanf("%f",&Sy); Matrix[2][0]=Matrix[2][0]+Sx; Matrix[2][1]=Matrix[2][1]+Sy; goto Menu; case '3': printf(" Enter Rotation Angle [Degree]:- "); scanf("%f",&Sx); Sx=(Sx*3.14)/180; Cos=cos(Sx); Sin=sin(Sx); for(i=0;i<=2;i++) { temp=Matrix[i][0]*Cos-Matrix[i][1]*Sin; Matrix[i][1]=Matrix[i][0]*Sin+Matrix[i][1]*Cos; Matrix[i][0]=temp; } goto Menu; case '4': Matrix[1][1]=Matrix[1][1]*-1; printf(" Done"); getch(); goto Menu; case '5': Matrix[0][0]=Matrix[0][0]*-1; printf(" Done"); getch(); goto Menu; case '6': Matrix[0][1]=1; Matrix[1][1]=1; printf(" Done"); getch(); goto Menu; case '7': for(i=0;i<=points;i++) { temp=poly[i][0]*Matrix[0][0]+poly[i][1]*Matrix[1][0]+Matrix[2][0]; poly[i][1]=poly[i][0]*Matrix[0][1]+poly[i][1]*Matrix[1][1]+Matrix[2][1]; poly[i][0]=temp; } case '8': getch(); // closegraph(); exit(0); } setgraphmode(2); display_cordinate(DARKGRAY,WHITE); Translate_to_Relative(poly,points,-Xdis,-Ydis); fill_poly(poly,points,CYAN,RED); getch(); restorecrtmode(); do { clrscr(); printf(" Do you Want to Switch to MENU [Y|N]:- "); ch=getche(); }while( (ch!='Y') && (ch!='N') ); if(ch=='Y') { Identity(Matrix); Xdis=0-poly[0][0]; Ydis=0-poly[0][1]; Translate_to_Relative(poly,points,Xdis,Ydis); goto Menu; } closegraph(); } void intgraph() { int g=DETECT,d; initgraph(&g,&d,"c: cgi"); } void fill_poly(int poly1[][2],int points,char line_color,char fill_color) { int pol[20],i; char str[2]; for(i=0;i<=points;i++) { pol[i*2]=poly1[i][0]; pol[i*2+1]=poly1[i][1]; } pol[i*2]=poly1[0][0]; pol[i*2+1]=poly1[0][1]; setcolor(line_color); setfillstyle(8,fill_color); fillpoly(points+1,pol); setcolor(fill_color); settextstyle(1,0,3); for(i=0;i<=points;i++) { sprintf(str,"%c",i+'a'); outtextxy(poly1[i][0],poly1[i][1],str); } } void Identity(float Matrix[3][3]) { int i,j; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { if(i==j) Matrix[i][j]=1; else Matrix[i][j]=0; } } } int Create_poly(int poly[][2]) { poly[0][0]=200; poly[0][1]=20; poly[1][0]=300; poly[1][1]=150; poly[2][0]=130; poly[2][1]=280; return 2; } void display_cordinate(char bg_color,char line_color) { int i; setbkcolor(bg_color); setcolor(line_color); for(i=0;i<=640;i+=50) { line(i,0,i,480); } for(i=0;i<=480;i+=50) { line(0,i,640,i); } rectangle(0,0,639,479); } void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis) { int i; for(i=0;i<=points;i++) { poly[i][0]=poly[i][0]+Xdis; poly[i][1]=poly[i][1]+Ydis; } } #include<graphics.h> #include<conio.h> #include<stdlib.h> main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p; initgraph(&gd,&gm,"C:\TC\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW); setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; } #include <graphics.h> #include <dos.h> #include <conio.h> main() { int i, j = 0, gd = DETECT, gm; initgraph(&gd,&gm,"C:\TC\ANT"); initgraph(&gd,&gm,"C:\TC\DOODLEBUG"); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(25,240,"Press any key to start"); getch(); setviewport(0,0,639,440,1); for( i = 0 ; i <= 500 ; i = i + 10, j++ ) { rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10); setcolor(j); delay(100); if( i == 420 ) break; clearviewport(); } getch(); closegraph(); return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.