Thread: Pointers trouble

  1. #31
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include <windows.h> 
    
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    
    
    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;
    			}
    		}
    	}
    }
    
    
    
    
    
    printimage(int image[],int size)
    {
    int r,c;
      for (r = 0; r < size; r++)
    	{
    
    		for (c = 0; c < size; c++)
    		{     if((image[r+c*size])==1)
    			{gotoxy(r,c);
                            printf("*");}
                    }
             }
    }
    
    
    
    
    
    
    
    
    
    
    
    main(){
    int r;
    int image[25*25];
    for(r=0;r<625;r++)
     {
      image[r]=0;
     }
    
    
    int size =25;
    int side =10;
    int even =1;
    drawImage(image,size,side,even);
    printimage(image,size);
    
    }
    i ran this on my machine,u can skip the gotoxy part,but it should print out a line of *'s without the gotoxy.
    Last edited by qqqqxxxx; 03-23-2006 at 02:57 AM.

  2. #32
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    hmmm... well I am gonna have to work on this a little, Im still not getting anything. But, I have to study for an exam I have tommorrow, or today I guess.

    Thanks for your help qqqxxxx.

  3. #33
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    int size=0;
    
    	for(r=0;r<25;r++){
    		if(image[r]==1){
    			size++;
    		}
    	}
    and i went through your code again,this is why u r not gettin anything

    here u r just checking the first row of the canvas,the very top row so u will get size=0 even after the for loop.

    but the square is located on center.

    and u want size to be 25,and u r trying to get the side length.
    Last edited by qqqqxxxx; 03-23-2006 at 03:20 AM.

  4. #34
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    Code:
    even = !(side & 1);
    sorry!

  5. #35
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    Code:
    int size=0;
    
    	for(r=0;r<25;r++){
    		if(image[r]==1){
    			size++;
    		}
    	}
    and i went through your code again,this is why u r not gettin anything

    here u r just checking the first row of the canvas,the very top row so u will get size=0 even after the for loop.

    but the square is located on center.

    and u want size to be 25,and u r trying to get the side length.
    I cant think of anyway to get the length of the side of the square without actually passing that to the function (which I am not supposed to do per the assignment).

    EDIT: I think I thought of something but I dont know how to do it. Each element in the array is a different memory location which are in consecutive order? So if it goes through the memory locations, dereferences them, and starts counting when it hits a 1, then stops when it hits a zero, that should work. Could somebody walk me through how I could go about doing that? I know it involves pointers.
    Last edited by saahmed; 03-24-2006 at 03:24 AM.

  6. #36
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    int r=0,side=0;
    while(image[r]!=1)r++;
    while(image[r]==1){r++;side++;}

  7. #37
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by qqqqxxxx
    Code:
    int r=0,side=0;
    while(image[r]!=1)r++;
    while(image[r]==1){r++;side++;}
    but that wouldnt go through just the first line would it?

  8. #38
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    nope first it finds a 1.

  9. #39
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Im thinking the best way to do this, if possible, is to create a pointer. Go through the each element of the array and dereference it. If it is a 0 leave it. If it is a 1 change it to *. After all the point of this assignment is to practice pointers.

    Is this possible?

    EDIT: maybe im just thinking of the next function, which would require me to find the edge of the square. In that case I would need to check adjacent memory locations , thus requiring a pointer.

    This is what he posted on the site as a hint yesterday but thats for the second function:

    Use two pointers to search through the array, and deference these pointers to obtain the value for adjacent points in the array. Check to see if the dereferenced value for the two pointers is unequal, and then place a 1 in the edge[] array corresponding to the location that the pointer of higher value points to.
    The purpose of this assignment is to practice dereferencing pointers!
    Last edited by saahmed; 03-24-2006 at 04:05 AM.

  10. #40
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    image[1] is the same as *(image +1);

    arrays and pointers have alot in common.

    Code:
    int im[10];
    im[10]=10;
    int *p=im;
    printf("%d",*(p+10));

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