Finishing off an assignment

This is a discussion on Finishing off an assignment within the C Programming forums, part of the General Programming Boards category; My overall assignment is to write a program that draws 4 concentric circles, editing an example program that makes 1 ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    Finishing off an assignment

    My overall assignment is to write a program that draws 4 concentric circles, editing an example program that makes 1 circle. I've gotten it done almost but 1 part is causing me trouble. I need it to be something like this: (trouble area marked)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* Program 9
    |
    |
    */
    
    makeCircles( FILE *image, int nrows, int ncols )
    {
      const int level1  = 68; /* inner most to outer most */
      const int level2  = 130;
      const int level3  = 193;
      const int level4  = 255;
      const int levelOUT = 40;  /* outside of the circle  */
    
      int r, c  ;       /* row and col of current pixel */
      int rc, cc ;      /* row and col of center of image */
      int rd, cd ;      /* row and col difference from center */
      double radius, dist ; /* radius of circle, distance from center */
    
      /* calculate center of image, size of radius */
      rc = nrows/2;
      cc = ncols/2;
      if ( nrows<ncols ) radius = nrows/2;
      else               radius = ncols/2;
    
      /* write out the pixel data */
      for ( r=0; r<nrows; r++ )
      {
        rd = r - rc;
    
        for ( c=0; c<ncols; c++ )
        {
          cd   = c - cc;
          dist = sqrt( (double)(rd*rd + cd*cd) );
          if ( dist < radius*0.2 )
            fputc( level1, image );
         else
          if(dist < radius*0.4)
            fputc( level2, image );
          else
           if(dist < radius*0.6)
            fputc(level3, image);
           else
             if(dist < radius*0.8)
              fputc(level4, image);
             else
               fputc(levelOUT, image);
        }
      }
    }
    
    
    /* write out the PGM Header information
    |
    | The number of gray levels is assumed to be 255.
    |
    */
    void writeHeader( FILE *image, int nrows, int ncols )
    {
      fprintf( image, "P5 ");
      fprintf( image, "# Created by makeCircles\n");
    
      /* width, height, number of gray levels */
      fprintf( image, "%d %d %d ", ncols, nrows, 255 );
    }
    
    void openImage( char *argv[], FILE **image, int *nrows, int *ncols )/*TROUBLE*/
    {
       
       /* open the image file for writing */
      if ( (*image = fopen( argv[1], "wb") ) == NULL )
      {
        printf("file %s could not be created\n", argv[1] );
        exit( EXIT_FAILURE );
      }
    
      *nrows = atoi( argv[2] );
      if ( *nrows < 1 )
      {
        printf("number of rows must be positive\n");
        exit( EXIT_FAILURE );
      }
    
      *ncols = atoi( argv[3] );
      if ( *ncols < 1 )
      {
        printf("number of columns must be positive\n");
        exit( EXIT_FAILURE );
      }
    }
    
    
    int main( int argc, char *argv[] )
    {
        /* check the command line parameters */
      if ( argc != 4)
      {
        printf("concircles fileName.pgm nrows ncols\n");
        return 0;
      }
      
      int nrows = *argv[2]; 
      int ncols = *argv[3] ;
      FILE *image; /*trouble here maybe */
    
      
      openImage(argv, , &nrows, &ncols);/*trouble here*/
      
       
      /* write header information */
      writeHeader( image, nrows, ncols );
    
      /* Create the Image */
      makeCircles( image, nrows, ncols );
    
      /* close the file */
      fclose ( image );
      return 1;
    }
    I can get it to work just fine when I put the contents of openImage into main though, can someone help me out with how to call openImage so It'll run?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    openImage(argv, , &nrows, &ncols);/*trouble here*/
    Shouldn't that be:
    Code:
    openImage(argv, &image, &nrows, &ncols);/*trouble here*/
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    yup, now it works, yay. I wasn't sure if just argv was right for the first parameter and I tried various forms of image to get it to compile but thank you, that was quick and now it works :].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 03:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 02:21 PM

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