Jump to content

2d array question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
goofy26

goofy26

    Newbie

  • Members
  • Pip
  • 1 posts
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 to do until now:

#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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The elements of the row are:
array[i][0]
array[i][1]
array[i][2]
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Do as shown by WingedPanther,

Or, in place of


			if(array1[i][j]==113){

				/*if yes make it zero*/

				array1[i][j]=0;


				/*also, make the values for row i =0*/

			

			}


You can


			if(array1[i][j]==113){

				/*if yes make the whole row zero*/

			        memset(&array[i][0], 0, 3 * sizeof(int) );

			}



Of course you need to #include necessary header file.