Quantcast

Maximum PC

It is currently Thu Jul 31, 2014 5:16 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: File input program
PostPosted: Sun Feb 13, 2005 3:59 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Program reads from file the number of rows and number of collumns followed by the directions to be placed into a 2d array and then the entry point of the array.

Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Assignment 2
// 
///////////////////////////////////////////////////////////////////////////////


#include <iostream>                // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file
#include <iomanip>

using namespace std;            // Standard included statement


struct maze{
        int row;
      int col;
      int row_entry;
      int col_entry;
      char maze_values [100][100];

};

void read_data( maze square[], int &x, ifstream ifile);

int main ()                         // Main function
{                                   // Opens main function


   int x=0;
   maze square[20];
   ofstream outfile;
   ifstream ifile;
   outfile.open("output.txt",ios::app);
   ifile.open("mazedata.txt",ios::in);
   
   read_data(square, x, ifile);
   
   
   


   return 0;
}






void read_data( maze square[], int &x, ifstream ifile)
{

   int row=0, col=0;
   
   do
   {
      ifile>>square[x].row;
      cout<<square[x].row;

      ifile>>square[x].col;
      cout<<square[x].col;
      
      do
      {

            if(col <square[x].col && row<square[x].row)
            {
               ifile>>square[x].maze_values[row][col];
               cout<<square[x].maze_values[row][col];
            }

            else
            {
               col = 0;
               row ++;
               cout<<endl;
            }
            
      }while(????)
      
      ifile>>square[x].row_entry;

      ifile>>square[x].col_entry;

      x++;

   }while(!ifile.eof())


}




Text file

Code:
5 6SEESSWSNEEEEESEEESSSNWWSEEENES 0 0 10 9WNNWWWSESWESNWNSWSSSNNENNNNSSNNSNEWESWNNSSWWWWEENNESWNWWSNWWWWNWENWSNESNNSSWSNNENEEEEEENSN 9 6 5 12ENSWWWWSWWWWSESNNSESNNENSSWSNNEESENSWESNNSWWWNSSWWWNNWNEENWW 3 11
4 4WWSENEESSWSWSSWE 0 2


Top
  Profile  
 
 Post subject:
PostPosted: Sun Feb 13, 2005 4:31 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Given a two-dimensional array as shwon below. The array represents a maze that a person must traverse in order to get to the other side.

0 1 2 3 4 5
0 S E E S S W
1 S N E E E E
2 E S N E E E
3 S S N W W S
4 E E E N E S

Each cell of the array contains a code that tells you which direction to go next in the maze.
N = Go north S = go south E = go east W = go west

Given an entry point into the maze consisting of a row index and a column index, the person must travel in the direction specified by the code found in each cell. The person exits the maze when the row index equals the number of rows in the array or the column index equals the number of columns in the array.

1. The program

You are to write a program which allow you to traverse a maze of arbitrary shape. The program must read the array into the program from a file, and then read the entry point. The program must be able to read more than one maze with each maze having only one specified entry point.

II Program Structure

Function subprograms:

1. a function to read the maze array
2. a function to print the maze array
3. a function to traverse the maze array, must provide the row and column indexes of the exit point to the calling program

The names of the input and output files should be global.


III. Data

A. The data consists of:
1. the size of the array
2. values for each cell of the array
3. the index values for the entry point of the maze
B. The set of data for the array shown on the first page should be:

5 6SEESSWSNEEEEESWEESSNWWSEEENES 0 0

c. a file (mazedata.txt) containing at least two sets of data will be sent to your for gradin purposes. read sets of data until you encounter an end of file

IV. Output

1. The entroy point
2. The maze with an appropriate label
3. The exit point

Sample:

Entry point: row = 0 column = 0

The maze

S E E S S W
S N E E E E
E S N E E S
S S N W W S
E E E N E S

Exit point: row = 1 column =5




Provided input file: (copy and paste into word to see how it is formatted)


5 6SEESSWSNEEEEESEEESSSNWWSEEENES 0 0 10 9WNNWWWSESWESNWNSWSSSNNENNNNSSNNSNEWESWNNSSWWWWEENNESWNWWSNWWWWNWENWSNESNNSSWSNNENEEEEEENSN 9 6 5 12ENSWWWWSWWWWSESNNSESNNENSSWSNNEESENSWESNNSWWWNSSWWWNNWNEENWW 3 11
4 4WWSENEESSWSWSSWE 0 2


Top
  Profile  
 
 Post subject:
PostPosted: Sun Feb 13, 2005 4:52 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Updated code without the calculation function
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1053 - Computer Science II
//  Assignment 2
//  This program reads data from a file
//  First two numbers are the size of the 2d array
//  The letters are directions North, South, East, and West to travel
//  The last two numbers are the starting points on the array
///////////////////////////////////////////////////////////////////////////////


#include <iostream>              // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file
#include <iomanip>             // Allows output manipulation
#include <string>             // Allows the use of the data type string

using namespace std;            // Standard included statement

//  Data type used to store the data from the file
struct maze{
      int row; 
      int col;
      int row_entry;
      int col_entry;
      string letters; 
      char maze_values [100][100];
      int row_exit;
      int col_exit;

};

//  This function reads the data from file and puts it into the proper places in the struct data type
void read_data( maze square[], int &x);

//  This function calculates the exit point of the maze and puts the row and collumn exit into the struct
void calculate( maze square[], int &x);

//  This function outputs the calculations and data to the screen
void print(maze square[], int &x);

//  Sets up for data input and output from a file
ofstream outfile;
ifstream ifile;

int main ()                         // Main function
{                               



   //  X is used as a counter for how many pieces of data there are
   int x=0;
   //  Sets up 20 data types of maze
   maze square[20];
   
   //  opens the output and input file
   outfile.open("output.txt",ios::trunc);
   ifile.open("mazedata.txt",ios::in);


   // calls the function to read that data from file and places data into the array
   read_data(square, x);

   //  Calculates through the array and where it exits
   calculate(square, x);
   //  Outputs the data to screen and file
   print(square,x);
   
   
   

   // closes the two files
   outfile.close();
   ifile.close();

   return 0;
}







void read_data( maze square[], int &x)
{
   //  reads the data into a string
   string read_line;
   



   // sets up counters for the loops
   int row=0, col=0, y;
   
   while(!ifile.eof())
   {
     
      ifile>>square[x].row;
 
      ifile>>square[x].col;

      ifile>>square[x].letters;


     //  loops through and places the string into the proper place on the 2d array
     int k = 0;
     for(int i = 0; i<square[x].row; i++)
     {
        for(int j=0; j < square[x].col; j++)
        {
      
           square[x].maze_values[i][j] = square[x].letters[k];
          
           k++;
        }

     }

      y=0;
      x++;


   }

}




//  Prints data to screen and to file

void print( maze square[], int &x)
{


   int row = 0, col=0;
   int count = 0;


   //  outputs the information to screen and file with a loop

   do{
      cout<<"Entry Point:   Row Index = "<<square[count].row_entry;
     outfile<<"Entry Point:   Row Index = "<<square[count].row_entry;
      cout<<"     Column Index = "<<square[count].col_entry<<endl<<endl;
     outfile<<"     Column Index = "<<square[count].col_entry<<endl<<endl;
      cout<<"  The Maze"<<endl<<endl;
     outfile<<"  The Maze"<<endl<<endl;

      for(int i = 0; i<square[count].row; i++)
     {
        for(int j=0; j < square[count].col; j++)
        {
      
            cout<<square[count].maze_values[i][j]<<" ";
            outfile<<square[count].maze_values[i][j]<<" ";
          
         
        }
        cout<<endl;
        outfile<<endl;
     }

   





     cout<<endl;
     outfile<<endl;
      cout<<"Exit Point:    Row Index = "<<square[count].row_exit;
     outfile<<"Exit Point:    Row Index = "<<square[count].row_exit;
      cout<<"     Column Index = "<<square[count].col_exit<<endl<<endl;
     outfile<<"     Column Index = "<<square[count].col_exit<<endl<<endl;
      count ++;

   }while(count <x);
}


//  Calculates the path of the array to the exit point and saves into the struct

void calculate( maze square[], int &x)
{
   int row_track, col_track, y = 0;

   do
   {
      row_track = square[y].row_entry;
      col_track = square[y].col_entry;

      while(row_track >=0 && col_track >=0 && row_track < square[y].row && col_track < square[y].col)
      {

         if(square[y].maze_values[row_track][col_track] == 'N')
            row_track --;

         else if(square[y].maze_values[row_track][col_track] == 'S')
            row_track ++;

         else if(square[y].maze_values[row_track][col_track] == 'E')
            col_track++;

         else if(square[y].maze_values[row_track][col_track] == 'W')
            col_track --;
      }

      //  Sets the tracking data so that it isn't outside the array when it finishes
      if(row_track < 0)
         row_track ++;
      if(col_track < 0)
         col_track ++;
      if(row_track > square[y].row)
         row_track --;
      if(col_track > square[y].col)
         col_track --;
      square[y].row_exit=row_track;
      square[y].col_exit=col_track;


      y++;
   }while(y <=x);




}


Top
  Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 2:15 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
This is a pretty good little exercise for the beginners out there. I'm suprised that it hasn't gotten more responses from people around here. For students/hobbyists considering it, it'll probably take you from 1/2 hr to and hr to finish depending on your experience. The professional crowd should pump it out in about 15 minutes.

More interesting versions would allow for mazes that are irregular shapes (ie, not rectangular), include a third dimension (or even more), transport points, etc. Also, there are some interesting solutions possible w/o using a two-dimensional array. More AI like problems would include possible loops (the array could not be represented as a tree in graph theory) and require the program to recognize and take an alternate path.

I do have one slight objection with the language though (I've bolded salient words for emphasis).

_alex911 wrote:
Given an entry point into the maze consisting of a row index and a column index, the person must travel in the direction specified by the code found in each cell. The person exits the maze when the row index equals the number of rows in the array or the column index equals the number of columns in the array.

<snip>

Sample:
Entry point: row = 0 column = 0

The maze

S E E S S W
S N E E E E
E S N E E S
S S N W W S
E E E N E S

Exit point: row = 1 column =5

Here we have a maze with 5 rows and 6 cols. We exit the maze when the row index equals the number of rows in the array or the column index equals the number of columns in the array. IMO, the exit point should be row = 1 and col = 6, not 5. At (1,5) the col index is 5 and there are 6 cols in the array. Maybe this was just a typo?

Here is a Java version that doesn't do the file input as specified, but includes an overloaded printMaze method which makes it easy to view the path through the array.

Code:
import java.util.*;

public class MazeArray {
   
    public static char[][] maze = null;
    public static int entryRow;
    public static int entryCol;
    public static int exitRow;
    public static int exitCol;
   
   
    public static void readMaze(String s) {
       
        //get the numbers...
        s = s.toUpperCase();
        String[] input = s.split("[ A-Z]+");
        maze = new char[Integer.parseInt(input[0])][Integer.parseInt(input[1])];
        exitRow = Integer.parseInt(input[2]);
        exitRow = Integer.parseInt(input[3]);
       
        //create the char[][]
        s = s.replaceAll("[ 0-9]+","");
        for (int i = 0; i < maze.length; i++)
            maze[i] = s.substring(i*maze[i].length, (i+1)*maze[i].length).toCharArray();
    }
   
    public static void printMaze() {
        System.out.println("entry row: " + entryRow);
        System.out.println("entry col: " + entryCol);
        System.out.println("maze:");
        for (int i = 0; i < maze.length; i++)
            System.out.println(String.valueOf(maze[i]));
        System.out.println("exit row: " + exitRow);
        System.out.println("exit col: " + exitCol);
    }

    //version of print maze used to watch the path taken through the maze
    //capital letter is the current position in the maze
    public static void printMaze(int r, int c) {
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[0].length; j++) {
                if (i == r && j == c)
                    System.out.print(maze[i][j]);
                else
                    System.out.print(Character.toLowerCase(maze[i][j]));
            }               
            System.out.println();
        }
        System.out.println();
    }       
   
    public static void traverseMaze() {
        exitRow = entryRow;
        exitCol = entryCol;
        while (exitRow < maze.length && exitCol < maze[0].length) {

            System.out.println("x,y  " + exitRow + "," + exitCol);
            printMaze(exitRow,exitCol);

            switch(maze[exitRow][exitCol]) {
                case 'N': exitRow--; break;
                case 'E': exitCol++; break;
                case 'S': exitRow++; break;
                case 'W': exitCol--; break;
            }
        }
    }
   
    public static void main(String[] args) {       
        readMaze("5 6SEESSWSNEEEEESNEEESSNWWSEEENES 0 0");
        traverseMaze();
        printMaze();
        System.exit(0);
    }   
}


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group