1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need emergency help on c++ project

Discussion in 'C++' started by Atheos, Jan 20, 2010.

  1. Atheos

    Atheos New Member

    Joined:
    Jan 20, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello
    Im new to the forum and Im in need of help :/
    I was asked to make a program that resemble a room with people trying to run out of it. To do this i was told to use matrix with 1s and 0s. I noticed a problem tho, where ppl "go out" of the matrix in the wrong place. I checked it a few dozen times but i just dont see, where the problem lies. I would greatly appreciate help and maybe a possible fix solution. If you help me i promise ill pray everyday for you, to whichever deity u wish ;)

    The problem is: "people" should go outside through "doors", which are 39-59 top-fields of the matrix. However some of them go through left side of the top of the matrix. (See the excel file to see what i mean)
    Heres the code:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    void spawning();
    void makeMoves();
    int moveUp(int x, int y);
    int moveLeft(int x, int y);
    int moveRight(int x, int y);
    int moveBiasLeft(int x, int y);
    int moveBiasRight(int x, int y);
    void goOutside(int x, int y);
    void printingMatrixes();
    int room[100][100];
    int remaining = 0;
    FILE* f;
    FILE* g;
    void main()
    {
    f=fopen("statistics", "wt");
    g=fopen("graphs", "wt");
    spawning();
    fprintf(f, "\n\n");
    while(remaining>0) 
    {
    makeMoves();
    printingMatrixes();
    }
    printf("Press any key to terminate\n");
    fclose(f);
    fclose(g);
    }
    void spawning() //random spawns
    { 
    int x, y, z;
    srand((unsigned int)(time(NULL)));
    for(y=0; y<100; y++)
    for(x=0; x<100; x++)
    {
    z=rand()%10;
    if(z==0)
    {
    room[x][y]=1;
    fprintf(f, "%d, ", room[x][y]);
    if(x==99) fprintf(f, "\n");
    remaining++;
    }
    else
    {
    room[x][y]=0;
    fprintf(f, "%d, ", room[x][y]);
    if(x==99) fprintf(f, "\n");
    }
    }
    }
    
    int moveUp(int x, int y)
    {
    if(room[x][y-1]==0)
    {
    room[x][y-1]=1;
    room[x][y]=0;
    return 1;
    }
    else return 0;
    }
    int moveLeft(int x, int y)
    {
    if(room[x-1][y]==0)
    {
    room[x-1][y]=1;
    room[x][y]=0;
    return 1;
    }
    else return 0;
    }
    int moveRight(int x, int y)
    {
    if(room[x+1][y]==0)
    {
    room[x+1][y]=1;
    room[x][y]=0;
    return 1;
    }
    else return 0;
    }
    int moveBiasLeft(int x, int y)
    {
    if(room[x-1][y-1]==0)
    {
    room[x-1][y-1]=1;
    room[x][y]=0;
    return 1;
    }
    else return 0;
    }
    int moveBiasRight(int x, int y)
    {
    if(room[x+1][y-1]==0)
    {
    room[x+1][y-1]=1;
    room[x][y]=0;
    return 1;
    }
    else return 0;
    }
    void goOutside(int x, int y)
    {
    room[x][y]=0;
    fprintf(g, "%d, ", remaining);
    remaining--;
    }
    void makeMoves()
    {
    int x,y;
    int l_border = 39;
    int r_border = 60;
    for(y=0; y<100; y++)
    for(x=0; x<100; x++)
    {
    if(room[x][y]==0) continue;
    if(x<=l_border)
    {
    if(y==0) moveRight(x,y);
    else if(moveBiasRight(x,y)==0) moveUp(x,y);
    }
    if(x>l_border&&x<r_border)
    {
    if(y==0) goOutside(x,y);
    else
    {
    int val = 0;
    val = moveUp(x,y);
    if(val==0) val=moveBiasLeft(x,y);
    if(val==0) val=moveBiasRight(x,y);
    }
    }
    if(x>=r_border)
    {
    if(y==0) moveLeft(x,y);
    else if(moveBiasLeft(x,y)==0) moveUp(x,y);
    }
    }
    }
    void printingMatrixes()
    {
    int x, y;
    for(y=0; y<100; y++)
    for(x=0; x<100; x++) 
    {
    fprintf(f, "%d, ", room[x][y]);
    if(x==99) fprintf(f, "\n");
    if(x==99&&y==99) fprintf(f, "\n\n");
    }
    }
    
    Thanks in advance for any possible help and effort.
     
    Last edited by a moderator: Jan 21, 2010
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Do not attach your complete code and assignments. Attchment removed.
     

Share This Page