如何编写单行函数进行行搜索和列式排序2d数组(限制函数应该像int search(int * A,int n,int key))

时间:2016-10-25 10:21:01

标签: c sorting search

大家好我编写了一个程序来生成随机数,然后按行和列顺序对这些数字进行排序,最后函数搜索在2d数组中查找键但是今天我的proff说你应该发送我只是单个函数这个int搜索(int * A,int n,int key)所以我可以在我的程序中测试它我不知道怎么做限制是列和行在2-5000之间,元素在0到int Max之间问题是测试n是否在增加,并假设矩阵是n乘n并且每个行和列都被排序。

我之前的节目是这个

#include <stdio.h>
#include <stdlib.h>
#define R 5000
#define C 5000

int arr[R][C];
int search(int arr[][C], int r, int c, int key) {
    if(key < arr[0][0] || key > arr[r - 1][c - 1])
        return -1;
    r = 0;
    c = c - 1;
    while(r <= c - 1 && c >= 0) {
        if(arr[r][c] < key)
            r++;
        else if(arr[r][c] > key)
            c--;
        else
            return 1;
    }
}

void sort_rows(int *arr, int n)
{
    int i;
    for(i = 1; i < n; i++)
    {
        int key = arr[i];
        int j = i - 1;
        while(j >= 0 && key < arr[j])
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}
void sort_column(int arr[][C], int r, int c)
{
    int key, k;
    int i, j;
    for(i = 0; i < r; i++)
    {
        for(j = 1; j < c; j++)
        {
            key = arr[j][i];
            k = j - 1;
            while(k >= 0 && arr[k][i] > key)
            {
                arr[k + 1][i] = arr[k][i];
                k--;
            }
            arr[k + 1][i] = key;
        }
    }
}
int main()
{
    int r, c;
    int key;
    int i, j, size_row, size_column;
    printf("Enter Number of rows and columns(2-%d,2-%d):", R, C);
    scanf("%d%d", &r, &c);
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
            arr[i][j] = rand() % (r * c) + 1;
    }
    printf("Original array:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    for(i = 0; i < r; i++)
    {
        sort_rows(arr[i], c);
    }
    sort_column(arr, r, c);
    printf("Sorted Array:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    printf("Enter element to search:");
    scanf("%d", &key);
    if(search(arr, r, c, key) == -1) printf("the %d not found", key);
    else  printf("the %d  is found", key);
}

所以我不知道如何实现这样的 任何帮助都会非常感谢你

2 个答案:

答案 0 :(得分:0)

我假设int * arr是从2D数组中转换出来的,连续分配,并且函数签名是:

  

int search(int * arr,int n,int key))

if(key < arr[0] || key > arr[n*n-1])
    return -1;
r = 0;
c = n - 1;
while(r <= c - 1 && c >= 0) {
    if(arr[r*n+c] < key)
        r++;
    else if(arr[r*n+c] > key)
        c--;
    else
        return 1;
}
return -1;

这应该有效,假设数组是正方形(列数等于行数),如果找不到元素,则此函数将返回-1,如果找到则返回1。

答案 1 :(得分:0)

Fx_Value_Input::Fx_Value_Input(int x, int y, int w, int h, const char* l)
: Fl_Value_Input(x, y, w, h, l)
{}

int Fx_Value_Input::handle(int e)
{
    int r = Fl_Value_Input::handle(e);

    if (e == FL_KEYBOARD)
    {
            if ((Fl::event_key() != FL_Enter && Fl::event_key() != FL_KP_Enter ) )
                    color(Fx::get_modified_color());
            else if ((Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter) &&             color() == Fx::get_modified_color())
                    color(FL_WHITE);
            redraw();
        }

    return r;
}

我将零改为-1只是因为程序的限制而且只是声明r和c而没有别的但是它没有工作没有编译失败它是程序内部的问题所以你能告诉我它可能是什么错了