Thread: Pointers trouble

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Pointers trouble

    I am really stuck here so just looking for some hints and advice. I dont know how to explain what my assignment is without actually posting what I have. So far I am only through the first bullet. I dont know where to go from there. I have read through the entire chapter again and I just dont know what parameters to pass to function drawImage and how to write the other two required functions. So, this is everything I have, I am not asking anyone to do this assignment for me, I simply want some pointers, no pun intended:

    The Assignment
    The objective of this program is to create an image (25 x 25) with a square of a user-defined size centered on the image.
    The program will:
    • Ask the user what size of square to draw (Maximum size of a side is 23, make sure to error check this entry)
    • Call the function drawImage, included in the drawImage.h header file
    • Call a function to print the image to the screen, using an * to demarcate the square
    • Call a function that uses a pair of pointers to search through the image to determine where the edge of the square is located. This function should generate a new image that has only the outline of the square.
    • Lastly recall the print function to print the image with the outline.

    Submit the code as hw7-2.c

    Hints:

    1. Use these function prototypes
    void printImage(int image[]);
    void edgeDetection(int image[], int edge[]);

    2. If using a one-dimension array to define a two-dimension image the following is a useful method for incrementing through the array: r + c*SIZE. Where r is the current row, c the current column and SIZE is the length of a row.

    Given header file
    Code:
    /******************************************************************
     *                                                                *
     * Programmer:                                   *
     * Name: drawImage                                                *
     * Function: Draws a square onto the center of a canvas           * 
     * Input: 1-D Array, width of array, length of square,            *
     * whether the square side is even or odd length                  *
     *     line and give a description of what it is>                 *
     * Output: An array with 1s where the square is on the image      *
     *                                                                *
     ******************************************************************/
    
    
    
    //Function to draw a square centered within an image
    void drawImage( int image[], int size, int side, int even)
    {
    	int r, c;
    
    	//loop through the image setting the appropriate pixels to "white" if the square is present
    	for (r = 0; r < size; r++)
    	{
    
    		for (c = 0; c < size; c++)
    		{
    			if ((size/2 - side/2) > r || (size/2 + side/2 - even) < r )
    			{
    				//Leaves the value of the array as it is since no part of the image appears in this row
    			}
    			else if( (size/2 - side/2) > c || (size/2 + side/2 - even) < c )
    			{
    				//Leaves the value of the array as it is since no part of the image appears in these columns
    			}
    			else
    			{
    				image[r+c*size] = 1;
    			}
    		}
    	}
    }
    This all I have so far, dont know what to do next, I do know pointers are necessary:
    Code:
    #include <stdio.h>
    #include "drawImage.h"
    
    /*function prototypes*/
    void printImage(int image[]);
    void edgeDetection(int image[], int edge[]);
    
    /*function main begins program execution*/
    int main (){
    	
    	int SIZE;
    	
    	printf("Please enter the size of the square you would like to draw"  
    			"\n(maximum size is 23):");  /*print statement*/
    	scanf("%d",&SIZE);
    
    	
    	if(SIZE>23){
    		printf("The size of the square cannot be larger than 23, please start again\n");
    		
    		return 0;
    	} /*end if*/
    
    	return 0; /*indicate program ended successfully*/
    } /*end function main*/

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    first off, I just need help with what to pass on to drawImage. It requires 4 inputs. All I have in function main is the length of the square. I dont know what to pass for the other three parameters.

  3. #3
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    You have to create the array to begin with:

    Code:
    int array[25 * 25];
    Here's a start:
    Code:
    // clear array
    int i;
    for(i = 0; i < 25 * 25; i ++)
        array[i] = 0;
    even = (side & 1);
    drawImage(array, 25, side, even);
    The assignment is confusing and written badly to begin with, so don't feel too bad. Most C programming teachers couldn't cut it at a real job, this is an obvious example.
    Last edited by joed; 03-22-2006 at 07:30 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by joed

    The assignment is confusing and written badly to begin with, so don't feel too bad. Most C programming teachers couldn't cut it at a real job, this is an obvious example.
    lol. Thanks. I thought I was the only one who had no idea what they were telling me to do. Ill give your stuff a try. Do I actually put "even" in drawImage()?

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by joed
    You have to create the array to begin with:

    Code:
    int array[25 * 25];
    Here's a start:
    Code:
    // clear array
    int i;
    for(i = 0; i < 25 * 25; i ++)
        array[i] = 0;
    even = (side & 1);
    drawImage(array, 25, side, even);
    The assignment is confusing and written badly to begin with, so don't feel too bad. Most C programming teachers couldn't cut it at a real job, this is an obvious example.
    I dont understand the assignment for even. So is that not if the side is an even number? Or is it a typo? Could you just explain it for me please.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >even = (side & 1)

    if side is even ,then even is set to 0.
    if side is odd,then even is set to 1.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    >even = (side & 1)

    if side is even ,then even is set to 0.
    if side is odd,then even is set to 1.
    Oh, I didnt know that. Thanks.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Okay, so the way Im understanding this is that the drawImage function is creating a square of 1's right? So in my printImage function I can just tell it to change 1's to *? If so, I am only passing the array to printImage per the assignment. So how will that work?

    EDIT: Or can I set this function equal to an array or something? then just pass that on? maybe pointers involved?

    EDIT: That wont work because drawImage doesnt return a value.
    Last edited by saahmed; 03-23-2006 at 01:20 AM.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >void edgeDetection(int image[], int edge[]);

    what do u think this is for?

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    >void edgeDetection(int image[], int edge[]);

    what do u think this is for?
    Im working on the printImage function. It doesnt require the use of this function. Or maybe I dont know what youre getting at.

    I know I need to pass an array to printImage.

    EDIT: Wait, passing the array to drawImage actually changes the array right? So if I pass it to printImage and just go through the array changing all the 1's to * that should work?
    Last edited by saahmed; 03-23-2006 at 01:23 AM.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    u dont have to change the 1's to *.
    where ever u find a 1 ,note that position,
    and print an * on the corresponding position on the screen.

    since the array is 1d u will have to convert the 1d array index value to a 2d screenposition coordinate.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    u dont have to change the 1's to *.
    where ever u find a 1 ,note that position,
    and print an * on the corresponding position on the screen.

    since the array is 1d u will have to convert the 1d array index value to a 2d screenposition coordinate.
    Okay, I think I understand what youre saying, but how would I go about doing that?

    EDIT:
    something like this?

    Code:
    for (r = 0; r < size; r++)
    	{
    
    		for (c = 0; c < size; c++)
    		{  
    			if(image[r+c*size] = 1){
    but what do I put in the if statement.
    Last edited by saahmed; 03-23-2006 at 01:38 AM.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >If using a one-dimension array to define a two-dimension image the following is a useful method for incrementing through the array: r + c*SIZE. Where r is the current row, c the current column and SIZE is the length of a row.

    that was suggested as a hint.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >if(image[r+c*size] = 1)

    image[r+c*size]==1

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    >if(image[r+c*size] = 1)

    image[r+c*size]==1
    whoops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with pointers..
    By elfjuice in forum C Programming
    Replies: 7
    Last Post: 11-25-2007, 01:19 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2004, 12:39 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM