2013-07-30 19 views
-2

我有規模的2x3 {7 7 7,4 4 4}和尺寸的2x3 {4 4 4,1 1 1}的矩陣B的矩陣Aarray[c] = {5 5 2}我如何選擇一行來計算矩陣減法在C

我希望用戶選擇連續做減法和如果行減法大於數組,它會要求用戶選擇另一行。

我的問題是,如果我選擇第1行減法,{7 7 7} - {4 4 4} = {3 3 3},第三值大於2,它應該打破,並要求用戶挑選另一行,但我的代碼並沒有這樣的。

while(count!=0){ 
    printf("enter row number"); 
    scanf("%d",&i); 
    if(running[i]){ 
    exec=1; 
    for(j=0;j<column;j++){ 
     if(A[i][j]-B[i][j]>array[j]){ 
     exec=0; 
     break; 
     } 
    } 
    if(exec){ 
     printf("Row %d is executing\n",i+1); 
     running[i]=0; 
     count--; 
     break; 
    } 
    }  
} 
+0

什麼是運行? –

+0

它輸出中「第1行正在執行」 – jason

+0

解釋R,運行,算.. [R應該沒有 – 999k

回答

0

嗨請在下面找到使用do while循環代碼,

注:我在靜態初始化數組,如果你願意,你可以動態地分配使用malloc

#include <stdio.h> 

int main() 
{ 
    int rowNum,i,j; 
    char execute = 'N' ; // flag variable used to repeat the loop 


    int A[2][3] = {7,7,7,4,4,4};  // Allocate the matrices dynamically if required 
    int B[2][3] = {4,4,4,2,2,2} ; 

    int c[3] = {5,5,2}; 

    do { 
      execute = 'N';    
      printf("\nEnter row number to perform substraction:"); 
      scanf("%d",&rowNum); 

      if(rowNum <= 2) // checking whether entered row number is greater than 2 

      { 
      for(i = rowNum-1, j =0 ; j <3 ; j++) 
      { 
      if((A[i][j] - B[i][j]) > c[j]) 
       { 
        execute = 'Y' ; 
        printf("Condition failed,Enter valid row number"); 
        break ; 
        } 


     } 
    } 
    else 
    printf("Crossed max number of rows\n"); 
} 

while(execute=='Y'); 


return 0 ; 
}  
+0

列的,如果我有4行,ROW1和ROW2有資格減法。如果row1成功,如何實現它要求用戶輸入其他行來執行減法 – jason

+0

添加else塊像else else {execute ='Y';繼續;} if((A [i] [j] - B [i] [j])> c [j])塊之後的語句。 – Mahesh