Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. Click for Solution |
-
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
A#include<stdio.h> #include<conio.h> #include<string.h> void main() { int n,i=0,j,k,row,col; int a[4][4],b[4][4],flag[4][4]; clrscr(); printf("Enter no. of rows: "); scanf("%d",&row); printf("Enter no. of cols: "); scanf("%d",&col); for(i=0;i<row;i++) { printf("Enter line %d ",i+1); for(j=0;j<col;j++) { scanf("%d",&a[i][j]); flag[i][j]=0; } } for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(a[i][j]==0) { for(k=0;k<col;k++) { //making entire row zero b[i][k]=0; flag[i][k]=1;//mark those zeros because they will be reconsidered again by this algo. } for(k=0;k<row;k++) {//making entire column zero b[k][j]=0; flag[k][j]=1;//mark those zeros because they will be reconsidered again by this algo. } } else if(flag[i][j]==0) b[i][j]=a[i][j]; } } printf("Output\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%d ",b[i][j]); } printf("\n"); } getch(); }
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
A writing in very simple for all students in c...............#include<stdio.h> # include<stdlib.h> int main() { int arr[5][5]; int row,col; int i,j; printf("\nplease input the row and coloumns of the matrix::"); scanf("%d%d",&row,&col); printf("\nplease input the elements of the matrix::"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d",&arr[i][j]); } } fflush(stdin); printf("\nthe elements of the matrix are::\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%4d",arr[i][j]); } printf("\n"); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { static int a=-1; static int b=-1; if((i==a)||(j==b)) continue; else if(arr[i][j]==0) { a=i; b=j; int k; for(k=0;k<col;k++) { arr[a][k]=0; } for(k=0;k<row;k++) { arr[k][b]=0; } } } } printf("\n new matrix is\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%4d",arr[i][j]); } printf("\n"); } system("PAUSE"); return 0; }
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 681
Aimport java.util.*; class array { int seti,setj,setflag=1; public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter no of rows and columns"); int row=sc.nextInt(); int col=sc.nextInt(); int arr[][]=new int[row][col]; System.out.println("enter the elements"); for(int i=0;i<arr.length;i++) { for(int j=0;j<arr.length;j++) { arr[i][j]=sc.nextInt(); } } System.out.println("the elements are------>"); for(int h=0;h<arr.length;h++) { for(int k=0;k<arr.length;k++) { System.out.print(arr[h][k]+"\t"); } System.out.println(); } new array().check(arr); } public void check(int ar[][]) { for(int i=0;i<ar.length;i++) { if(setflag==0) { break; } for(int j=0;j<ar.length;j++) { if(ar[i][j]==0) { seti=i; setj=j; setflag=0; arrange(seti,setj,ar); break; } } } } void arrange(int i,int j1,int arr[][]) { for(int h=0;h<arr.length;h++) { for(int j=0;j<arr.length;j++) { if(h==i) { arr[h][j]=0; } if(j==j1) { arr[h][j]=0; } } } //after arrange System.out.println("the elements are------>"); for(int h=0;h<arr.length;h++) { for(int k=0;k<arr.length;k++) { System.out.print(arr[h][k]+"\t"); } System.out.println(); } } }<br/>
in java run it...
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 664
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 682
bint i,j; a[3][3] for(int i=0;i<3;i ) { for(j=0;j<3;j ) cin>>a[i][j]
Warning: Illegal string offset 'name' in /home/prepdo6/gpl4you/discussquessub.php on line 664
Explore- GPL4you
- Home
- Prepdoor - Online Mock Test
- About
- FAQs
- Contact
- Contact Us
© 2011 - gpl4you | All Rights Reserved.