Thread: 2d array question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    14

    2d array question

    hi all,

    im new to programming in general and im probably asking something very simple here.....

    well i have a 2d symmetric array where i search for a value and if found i want to give the value of zero to the whole row

    here what i have managed until now:
    Code:
    #include "stdafx.h"
    #include<stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int array1[3][3]={{  0,113,303},
    					  {113,  0,196},
    					  {303,196,  0}};
    	int i;
    	int j;
    
    
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			/* check if number 113 exists */
    
    			if(array1[i][j]==113){
    				/*if yes make it zero*/
    				array1[i][j]=0;
    
    				/*also, make the values for row i =0*/
    			
    			}
    				printf("%2d ",array1[i][j]);
    			
    
    		}
    		
    		printf("\n");
    		
    	}
    
    	scanf("%d",&i);
    
    	return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Replace this part:
    Quote Originally Posted by goofy26 View Post
    Code:
    			if(array1[i][j]==113){
    				/*if yes make it zero*/
    				array1[i][j]=0;
    
    				/*also, make the values for row i =0*/
    			
    			}
    with an inner for() loop:
    Code:
    int ii;  /* at the top please! */
    
    			if(array1[i][j]==113){
    				for (ii=0;ii<3;ii++) array[i][ii]=0;
                                    break;      /* might as well be done with this row */
    			}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    hi,

    thanks for your reply, i have put your suggestion in but i still dont get zeros on the first row:

    Code:
    #include "stdafx.h"
    #include<stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int array1[3][3]={{  0,113,303},
    					  {113,  0,196},
    					  {303,196,  0}};
    	int i;
    	int j;
    	int ii;  /* at the top please! */
    
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			/* check if number 113 exists */
    
    				if(array1[i][j]==113){
    					for (ii=0;ii<3;ii++){
    					 array1[i][ii]=0;
                         break; 
    					 }/* might as well be done with this row */
    				
    				/*also, make the values for row i =0*/
    			
    				}
    				printf("%2d ",array1[i][j]);
    			
    
    		}
    		
    		printf("\n");
    		
    	}
    
    	scanf("%d",&i);
    
    	return 0;
    }
    the result is quite different now

    its:

    0 113 303
    0 0 196
    303 196 0

    the result that im after is

    0 0 0
    0 0 196
    303 196 0

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the break is in the wrong place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, this:
    Code:
    			if(array1[i][j]==113){
    				for (ii=0;ii<3;ii++) array[i][ii]=0;
                                    break;     
    			}
    and this
    Code:
                            if(array1[i][j]==113){
    					for (ii=0;ii<3;ii++){
    					         array1[i][ii]=0;
                                                     break; 
    					}
    			}
    ARE NOT THE SAME. You put the break inside the for(ii) loop.

    ps. I think you want this output
    0 0 0
    0 0 0
    303 196 0

    and that's what you'll get.
    Last edited by MK27; 03-02-2009 at 07:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    I've put it like you said:

    Code:
    #include "stdafx.h"
    #include<stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int array1[3][3]={{  0,113,303},
    					  {113,  0,196},
    					  {303,196,  0}};
    	int i;
    	int j;
    	int ii;  
    
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			/* check if number 113 exists */
    
    				if(array1[i][j]==113){
    				for (ii=0;ii<3;ii++) array1[i][ii]=0;
                    break;     
    					
    				
    			
    				}
    				printf("%2d ",array1[i][j]);
    			
    
    		}
    		
    		printf("\n");
    		
    	}
    
    	scanf("%d",&i);
    
    	return 0;
    }
    and now i get:

    0
    303 196 0

    (im not sure why it does give me all 3 rows now)

    i think i didnt explain it very well when i first posted the question

    where i check whether a number is equal to 113
    i want to to set the line it first finds the number to zero

    so in this 113 is in position 0,1 and 1,0

    the first time it finds 113 is in position 0,1
    so i want it to be able to set row's 0 values to zero
    but not row's one

    thx a lot for your help i really appreciate it
    Last edited by goofy26; 03-02-2009 at 08:13 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sorry, you should take the break right out, I didn't take into account the flow with the printing.

    There is a flaw in your code tho -- you start printing the row BEFORE you have checked it. So if there was a row with 113 in it with a non-zero value before it in the row (eg, 666, 113, 0) it will change the row after printing 666, so you will see 666 0 0.

    You should print the array in a completely separate block or function after you parse (then you can put that break statement back in too).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    thx for the advice

    i've put it like that:


    Code:
    #include "stdafx.h"
    #include<stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int array1[3][3]={{  0,113,303},
    					  {113,  0,196},
    					  {303,196,  0}};
    	int i;
    	int j;
    	int ii;  
    
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			/* check if number 113 exists */
    
    				if(array1[i][j]==113){
    				for (ii=0;ii<3;ii++) array1[i][ii]=0;
                    break;     
    				}
    		}
    	}
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			printf("%2d ",array1[i][j]);
    		}
    		printf("\n");
    	}
    	scanf("%d",&i);
    
    	return 0;
    }
    and i get the result you mentioned earlier:
    0 0 0
    0 0 0
    303 196 0

    is there a way to get it not to set the whole second row to zero

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    i've added one more line and now i get the result i want:

    (not sure if its the right way to do it though)

    Code:
    #include "stdafx.h"
    #include<stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int array1[3][3]={{  0,113,303},
    					  {113,  0,196},
    					  {303,196,  0}};
    	int i;
    	int j;
    	int ii;  
    
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			/* check if number 113 exists */
    				if(array1[i][j]==113){
    				for (ii=0;ii<3;ii++) array1[i][ii]=0;
    				array1[j][i]=0;
                    break;  
    				
    				}
    				
    		}
    		break;
    	}
    	for(i=0;i<=2;i++){
    		
    		for (j=0;j<=2;j++){
    			printf("%2d ",array1[i][j]);
    		}
    		printf("\n");
    	}
    	scanf("%d",&i);
    
    	return 0;
    }

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just a note, that you did say this in the Original Post:

    Quote Originally Posted by goofy26 View Post
    well i have a 2d symmetric array where i search for a value and if found i want to give the value of zero to the whole row
    If you want to do something different to each row, you would have to make different rules for each row by using more if statements and/or conditions:
    Code:
    for (j=0;j<=2;j++){
    				if((i==1) && (array1[i][j]==113)){
    				          for (ii=0;ii<3;ii++) array1[i][ii]=0;
                                              break;     
    				}
                                    else if((i==2) && (array1[i][j]==113)){
                                              [only change two numbers]
                                     }
                                     ...etc.

    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    ok, ill keep that in mind as i continue to change this further

    thanks so much for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  2. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM