converting printf to fprintf and fixing code problems

This is a discussion on converting printf to fprintf and fixing code problems within the C Programming forums, part of the General Programming Boards category; Heres the code please help Code: #include <stdio.h>#include <stdlib.h>#include <string.h>#define ALIVE 1#define DEAD 0#define GOL_DIM 10int MArray[GOL_DIM+2][GOL_DIM+2];int PArray[GOL_DIM+2][GOL_DIM+2];int generation = ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6

    converting printf to fprintf and fixing code problems

    Heres the code please help
    Code:
    #include <stdio.h>#include <stdlib.h>#include <string.h>#define ALIVE 1#define DEAD  0#define GOL_DIM 10int  MArray[GOL_DIM+2][GOL_DIM+2];int  PArray[GOL_DIM+2][GOL_DIM+2];int  generation = 0;char *commands[] =  {"help", "quit", "init", "next", "status", "print", "last",};typedef enum  { help, quit, init, next, status, print, last }  command_t;intcountMatrix(int currArray[][GOL_DIM+2], int dim){  /* count how many cells on the game board are still alive */...}voidinitMatrix(int currArray[][GOL_DIM+2], int dim){    int i, j;    bzero(currArray, sizeof(int) * (GOL_DIM+2) * (GOL_DIM+2));    for(i = 1; i < (dim+1); i++)    {      for (j = 1; j < (dim+1); j++)	{	  currArray[i][j] = random() % 2;	}    }}voidprintMatrix(int currArray[][GOL_DIM+2], int dim){    int i, j;    printf("Column:    ");    for(i = 0; i < (dim+2); i++) printf("%2d ",i);    printf("\n");    printf("           ");    for(i = 0; i < (dim+2); i++) printf("__ ");    printf("\n");        for(i = 0; i < (dim+2); i++)    {      printf("row = %2d > ", i);      for (j = 0; j < (dim+2); j++)	{	  printf(" %c ", (currArray[i][j] == 1) ? 'X' : ' ');	}      printf(" <\n");    }    printf("           ");    for(i = 0; i < (dim+2); i++) printf("__ ");    printf("\n");}intcountNeighbor(int currArray[][GOL_DIM+2], int curX, int curY){  /* count how many neighbors of [curX, curY] are still alive */...}intGOLrule(int count, int current){  /*   * given the number of neighbors alive (i.e., count) and the   * current status of the cell (i.e., current), what should be   * the status of the cell for next generation (as return).   */...}voidnextGeneration(int currArray[][GOL_DIM+2], int nextArray[][GOL_DIM+2], int dim){  /*   * currArray represents the status of every cell in the current   * generation. Compute the status of every cell for the next   * generation in the nexyArray. The dim variable is dimension of   * the game board -- should be 10.   */...}voidcopyMatrix(int currArray[][GOL_DIM+2], int nextArray[][GOL_DIM+2], int dim){  /*   * copy the value from nextArray to currArray   */...}voidcmd_help(void){  int i;  fprintf(stderr, "currently available commands:\n");  for (i = 0; i < (last + 1); i++)    {      if (i == ((i/5)*5)) fprintf(stderr, "\n");      fprintf(stderr, "* %-10s", commands[i]);    }  fprintf(stderr, "\n");  return;}intmain(void){  fprintf(stderr, "ecs30b, Game of Life -- version 0.99, 11-14-2008\n");  fprintf(stderr, "author: S. Felix Wu (email: wu@cs.ucdavis.edu)\n");  fprintf(stderr, "\n");  while (1)    {      command_t ct;      char bufs[200];      int i;      bzero(bufs, 200);      fprintf(stderr, "[#] ");      fscanf(stdin, "%s", bufs); getchar();      for (i = 0; i <= last; i++) if (strcmp(bufs, commands[i]) == 0) break;      ct = i;      switch(ct)	{	case help:	  cmd_help();	  break;	case quit:	  fprintf(stderr, "Good Bye\n");	  exit(0);	  break;	case init:	  generation = 0;	  initMatrix (MArray, GOL_DIM);	  fprintf(stderr, "Genreation %2d:\n", generation);	  printMatrix(MArray, GOL_DIM);	  break;	case status:	  fprintf(stderr, "[%3d] cells are still alive!\n",		  countMatrix(MArray, GOL_DIM));	  break;	case next:	  copyMatrix(PArray, MArray, GOL_DIM);	  nextGeneration(PArray, MArray, GOL_DIM);	  generation++;	  fprintf(stderr, "Genreation %2d:\n", generation);	  break;	case print:	  fprintf(stderr, "Genreation %2d:\n", generation);	  printMatrix(MArray, GOL_DIM);	  break;	case last:	  fprintf(stderr, "Genreation %2d:\n", generation - 1);	  printMatrix(PArray, GOL_DIM);	  break;	default:	  fprintf(stderr, "    ^^^^^^^^^ Unknown command\n");	  break;	}    }  /* finishing */  return 0;}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    It might help if you fixed the formatting of the code that you posted. (I don't know why the formatting occasionally gets thrown off like that though.)

    Also, you should elaborate on your problem, e.g., tell us what you are trying to do, how does it not work, etc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fprintf and printf ?
    By vsovereign in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 12:13 AM
  2. fprintf vs printf
    By Tiago in forum C Programming
    Replies: 2
    Last Post: 12-12-2009, 12:03 PM
  3. printf,sprintf,fprintf
    By clique in forum C Programming
    Replies: 1
    Last Post: 10-11-2008, 12:48 AM
  4. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  5. printf /fprintf with __int64
    By stormbringer in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21