-4

I've made a code which creates a matrix and sort it in ascending form. (row)

I want to do following things in the ex.'s that I've given with my code, but I can't find out how.

ex. If I enter "r" as the input; a matrix entered like this;

3 4 5
9 2 8
6 4 8

Should en up in that form;

3 4 5
2 8 9
4 6 8

and the output in the screen should be;

4
8
6

As we take the medians of the rows.

and, if I enter the input value as 'c' (which is for column sorting and median)

3 4 5
9 2 8
6 4 8

should re-order itself as;

3 2 5
6 4 8
9 4 8

and the median value should show up itself as;

6 4 8

Here is my full code;

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

0

Trying to perform all the actions in main() will make this way too complex.

Rather, break the code into functions, one function per activity.

For instance:

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]
Air
  • 7,120
  • 2
  • 47
  • 78
user3629249
  • 15,593
  • 1
  • 16
  • 17
  • you found the median statically, the author is expecting it in generalised form because n value can change over time might not be the same 3. – Manjunath N Dec 20 '14 at 22:27
-1

I am Posting you for the row median.

Declare the below variable

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;
}
Manjunath N
  • 1,195
  • 10
  • 20
-1

I am posting you the complete solution according to your requirement as far as possible, almost complete

you can comment if something went wrong with my code.

#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");
}
Manjunath N
  • 1,195
  • 10
  • 20