如何在矩阵C中找到中位数(行和列)

时间:2014-12-20 20:51:21

标签: c matrix row median

我创建了一个代码,用于创建矩阵并以升序形式对其进行排序。 (行)

我想在我的代码中提供以下内容,但我无法了解如何。

离。如果我输入“r”作为输入; 像这样输入的矩阵;

3 4 5
9 2 8
6 4 8

应该以那种形式出现;

3 4 5
2 8 9
4 6 8

并且屏幕中的输出应为;

4
8
6

当我们采取行的中位数时。

并且,如果我输入的输入值为'c'(用于列排序和中位数)

3 4 5
9 2 8
6 4 8

应该将自己重新排序为;

3 2 5
6 4 8
9 4 8

,中值应显示为;

6 4 8

这是我的完整代码;

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define n 3

int main()
{
    int arr[n][n],min,i,j,tmp,y,k,w,z=0,q=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("Enter number: ");
            scanf("%d",&arr[i][j]);
        }
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            z = j; 
            min = arr[i][j];

            for (k = j +1 ; k < n; k++)
            {
                if (arr[i][k] < min)
                {
                    min = arr[i][k];
                    z = k; 
                }
            }

            tmp=arr[i][j];
            arr[i][j]=min;
            arr[i][z]=tmp;
        }
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%d ",arr[i][j]);
        printf("\n");
    }
    getch();
}

3 个答案:

答案 0 :(得分:0)

尝试在main()中执行所有操作会使这种方式过于复杂。

相反,将代码分解为函数,每个活动一个函数。

例如:

static int matrix[3][3] = {{0}}

int main()
    inputMatrix()
    done = false
    while(!done)
        action = actionMenu()
        switch( action )
            case 0:
                ....
                break;
            case 1:
                ....
                break;
            default:
                done = true
                break;
         end switch
     end while
     return(0)


int actionMenu()
    display menu,
    done = false
    while(!done)
        display prompt 
        gets response, 
        if response is valid
            done = true
        end if
    end while
    returns validated response

void inputMatrix()
    displays criteria
    loop until 9 valid responses input
        display prompt
        get response
        validate response
    end loop:

void sortEachRow()
    --there are three rows, and three columns per row
    for row=0; row<3; row++
        swapped = true
        while swapped
            swapped = false
            for column=0; column<2; column++
                if row[column] > row[column+1]
                    swapped = true
                    exchange row[column], row[column+1]
                end if
            end for
        end while
     end for


void sortEachColumn()
    very similar to sortEachRow

void PrintRowMedian()
    print row[1][0]
    print row[1][1]
    print row[1][2]

void PrintColumnMedian()
    print row[0][1]
    print row[1][1]
    print row[2][1]

答案 1 :(得分:-1)

我发布你的行中位数。

声明以下变量

int count=0,median=0;


for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        z = j; 
        min = arr[i][j];
        count +=1; //Using count here

        for (k = j +1 ; k < n; k++)
        {
            if (arr[i][k] < min)
            {
                min = arr[i][k];
                z = k; 
            }
        }

        tmp=arr[i][j];
        arr[i][j]=min;
        arr[i][z]=tmp;
    }
/*Modifi from here*/
if(count %2 == 1)
         median = (count+1)/2;
    else
         median = (count)/2;
    printf("%d\n",arr[i][median-1]);
    count = 0;
}

答案 2 :(得分:-1)

我会尽可能根据您的要求向您发送完整的解决方案,几乎完成

如果我的代码出现问题,您可以发表评论。

#include <stdio.h>
#include <stdlib.h>

#define n 3

int arr[n][n],arr_col[n][n];
int count_row=0,median=0,count_col=0;
int min,i,j,tmp,y,k,w,z=0,q=0;
char op,op1;
void row_median();
void col_median();
void print();
void print_col();
void read();
int main() 
{
for(;;)
{
 printf("Enter 'r' for row and 'c' for column median and 'q' for quit\n");
 scanf("%c",&op);
 switch(op)
 {
   case 'r':read();
            row_median();
            printf("\n");
            break;
   case 'c':read();
            col_median();
            printf("\n");
            print_col();
            break;
   case 'q':exit(0);
            default:printf("please enter a valid choice\n");
  }
 }
return 0;
}

void read() 
{
  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
      {
        printf("Enter number: ");
        scanf("%d",&arr[i][j]);
        arr_col[i][j] = arr[i][j];
       }
    }
 }

void print()
{
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
        printf("%d ",arr[i][j]);
    printf("\n");
  }
}

void print_col()
{
 for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
        printf("%d ",arr_col[i][j]);
    printf("\n");
  }
}

void row_median()
{
 for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
        z = j; 
        min = arr[i][j];
        count_row +=1;

        for (k = j +1 ; k < n; k++)
        {
            if (arr[i][k] < min)
            {
                min = arr[i][k];
                z = k; 
            }
        }

        tmp=arr[i][j];
        arr[i][j]=min;
        arr[i][z]=tmp;
    }
    if(count_row %2 == 1)
         median = (count_row+1)/2;
    else
         median = (count_row)/2;
    printf("%d\n",arr[i][median-1]);
    count_row = 0;
  }
 print();
}

void col_median()
{
for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
        z = j; 
        min = arr_col[j][i];
        count_col +=1;

        for (k = j+1 ; k < n; k++)
        {
            if (arr_col[k][i] < min)
            {
                min = arr_col[k][i];
                z = k; 
            }
        }

        tmp=arr_col[j][i];
        arr_col[j][i]=min;
        arr_col[z][i]=tmp;
    }
    if(count_col %2 == 1)
         median = (count_col+1)/2;
    else
         median = (count_col)/2;
    printf("%d ",arr_col[median-1][i]);
    count_col = 0;
  }
 printf("\n");
}