[SO-I] 二维数组中的查找

x86/Debian linux/gcc


1 题目

一个二维整型数组被存在array.dat文件中,从第一行开始,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成几个函数,读取array.dat中的数据到程序中,任意给程序输入一个整数n,并判断此整数是否在此二维数组中。如:


Figure1. array.dat例

2 主函数

(1) main()

/*Filename:     main.c
 *Function:     Call define functions by main()
 *Author:       One fish
 *Date:         2014.11.8 Sat
 *Last Modifid: 2014.11.9 Sun
 */
#include <stdio.h>
#include <stdlib.h>
#include "cfun.h"

/*-----------Function Decalre-------------*/
void main_usage(const int argc);
void free_pp(int **pp, const int r, const int c);

/*@main()
 *@Use argc and argv arguments of main()
 *@Return 0 when OK, not 0 when except ocurr
 */
int main(int argc, char *argv[])
{
        main_usage(argc);


        myrc    rc;
        int     re;
        int     num;
        int     **pp;

        num     = 0;
        re      = FLASE;
        pp      = NULL;

        //Get row and column of argv[1]
        get_file_row_column(argv[1], &rc);

        //Allocate memory for data
        pp      = allocate_mm(rc.r, rc.c);

        //Read data of argv[1] into momory allocate just now
        if (pp) {
                re      = read_data_in(argv[1], pp, rc.r, rc.c);
        }

        //Find the number argv[2]
        if (TRUE == re) {
                num     = atoi(argv[2]);
                re      = find_num_in_array(pp, rc.r, rc.c, num);
                if (TRUE == re) printf("^_^\n");
                else printf("Not ^_^\n");
        }

        //Free memory by pp pointed to
        if (pp) {
                free_pp(pp, rc.r, rc.c);
        }

        return 0;
}


/*@main_usage:  Give tips for users
 *@arg: argc is the number of the command-line
 *@exit(1) when users use the program incorrectly
 */
void main_usage(const int argc)
{
        if (3 != argc) {
                printf("Usage: ./execute arrary.txt number\n");
                exit(1);
        }
}


/*@free_pp:     free allocate memory in order
 *@arg:         pp is the address of the memory,
                r, c is the row and column of the allocate memory
 */
void free_pp(int **pp, const int r, const int c)
{
        int i;
        printf("\n----In fre_pp() function----\n");
        for ( i = 0; i < r; i++) {
                printf("Now free pp[%d]: %p\n", i, pp[i]);
                free(pp[i]);
        }
        free(pp);
}

(2) cfun.h

/*@Filename:    cfun.h
 *@Function:    Decalre functions, varibles, macros
 *@Author:      One fish
 *@Date:        2014.11.8 Sat
 *@Last modified:       2014.11.9 Sun
*/

/*-----------Macro definition------------*/
#define TRUE    1
#define FLASE   0


/*Golbal varibles definiton*/
typedef struct rc {
        unsigned int r;
        unsigned int c;
}myrc;


/*----------Function decalration---------*/
 //int find_c_in_array(int arry[][ARRAY_SIZE], int c);



/*@Function:    Get the rows and columns of the path(file)
 *@arg:         path is the special file, rc is the pointer of the type of struct myrc
 *@revl:        none when sucess, otherwise exit(1)
 */
void get_file_row_column(const char *path, myrc *rc);



/*@Function:    allocate memory dynamicly accordding to the row, column of one file
 *@arg:         r is the number of the second class pointer, c is the number of the first class pointer
                r is the row of one file, c is the column of one file
 *@rel:         Address of the memory allocated when success, NULL when fail
 */
int **allocate_mm(const int r, const int c);



/*@Function:    Read data fromm path to pp
  @arg:         path is the location of file, pp is the address of the memory
                r and c is row and column of memory pointed by pp, respectlly
  @rel:         TURE when read success, oterwise FLASE. When pp is NULL exit(1)
*/
int read_data_in(const char *path, int **pp, const int r, const int c);



/*@find_c_in_array:     if num if pp memory, find it
  @arg:                 pp is the memory address of the array
                        r, c is the row and column of file, respectlly
                        num is the number will be find
  @rel:                 TRUE if find num in pp, FLASE if not find
*/
int find_c_in_array(int **pp, const int r, const int c, const int num);

3 子函数

(1) 获取目标文件的行和列数

/*@Filename:    get_file_row_column.c
 *@Function:    Get the special file's rows and columns
 *@Author:      One fish
 *@Date:        2014.11.4 Thu
 *@Last modified:       2014.11.9 Sun
 */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "cfun.h"

/*Macro definition*/
#define LINE_SIZE       165


/*@Function:    Get the rows and columns of the path(file)
 *@arg:         path is the special file, rc is the pointer of the type of struct myrc
 *@revl:        none when sucess, otherwise exit(1)
 */
void get_file_row_column(const char *path, myrc *rc)
{
        FILE *fp;
        char buf[LINE_SIZE];

        fp      = NULL;
        rc->r   = 0;
        rc->c   = 0;

        if ( !(fp = fopen(path, "r")) ) {
                perror("fopen()");
                exit(1);
        }

        //Get columns, fp will point to the end of first line
        while ( '\n' != fgetc(fp) ) rc->c++;
        rc->c   = (rc->c + 1) / 2;

        //Get rows
        while ( fgets(buf, LINE_SIZE - 1, fp) ) rc->r++;

        //Add the first line
        rc->r++;

        printf("\n----In get_file_row_column() function----\n");
        printf("The %s file has %d rows, %d columns\n", path, rc->r, rc->c);

        fclose(fp);
}

(2) 动态分配二维内存

/*Filename:     allocate_mm.c
 *Function:     allocate memory dynamicly accordding to the row, column of one file
 *Author:       One fish
 *Date:         2014.11.8 Sat
 *Last Modified:2014.11.9 Sun
 */
#include <stdio.h>
#include <stdlib.h>


/*@Function:    allocate memory dynamicly accordding to the row, column of one file
 *@arg:         r is the number of the second class pointer, c is the number of the first class pointer
                r is the row of one file, c is the column of one file
 *@rel:         Address of the memory allocated when success, NULL when fail
 */
int **allocate_mm(const int r, const int c)
{
        int     i, j;
        int     **pp = NULL;
        void    show_memory_adds(int **, const int, const int);

        pp      = (int **)malloc(r * sizeof(int));
        if ( !pp ) {
                printf("Allocat memory for pp failed\n");
                return NULL;
        }


        for (i = 0; i < r; ++i) {
                pp[i]   = (int *)malloc(c * sizeof(int));
                if ( !pp[i] ) {
                        //Free the heap pointed by p[j]
                        for (j = i - 1; j >= 0; --j) {
                                free(pp[j]);
                        }

                        //Free the heap pointed by p
                        free(pp);
                        printf("Allocate pp[%d] failed\n", i);
                        return NULL;
                }
        }

        show_memory_adds(pp, r, c);

        return pp;
}


//Show the allocated memory information
void show_memory_adds(int ** pp, const int r, const int c)
{
        int i, j;

        printf("\n----In allocate_mm() function----\n");
        printf("----Allocate success, Allocate memory %d rows, they are:\n", r);
        for ( i = 0; i < r; i++) {
                printf("pp[%d]: ", i);
                for ( j = 0; j < c; j++) {
                        printf("pp[%d][%d]%p\t", i, j, &pp[i][j]);
                }
                printf("\n");
        }
}

(3) 读取二维数组到动态内存

/*@Filename:    read_data_in.c
 *@Function:    Read data from file into memory
 *@Author:      One fish
 *@Date:        2014.11.8 Sat
 *@Last modified:2014.11.9 Sun
 */
#include <stdio.h>
#include <stdlib.h>
#include "cfun.h"


/*@Function:    Read data fromm path to pp
  @arg:         path is the location of file, pp is the address of the memory
                r and c is row and column of memory pointed by pp, repectlly
  @rel:         TURE when read success, oterwise FLASE. When pp is NULL exit(1)
 */
int read_data_in(const char *path, int **pp, const int r, const int c)
{
        int     i, j;
        char    ch;
        int     intg;
        FILE    *fp;

        ch      = 0;
        intg    = 0;
        fp      = NULL;

        if (!pp) {
                printf("Pointer is NULL. In %s, line: %d\n", __FILE__, __LINE__);
                exit(1);
        }

        if ( !(fp = fopen(path, "r")) ) {
                perror("fopen");
                return FLASE;
        }

        printf("\n----In read_data_in() function----\ndata  in memory:\n");
        for (i = 0; i < r; i++) {
                printf("pp[%d] line: ", i);
                j       = 0;
                while ( (j < c) && (2 == fscanf(fp, "%d%c", &intg, &ch)) ) {
                        pp[i][j]        = intg;
                        j++;

                        printf("%d\t", intg);
                        if ( j % c == 0) printf("\n");
                }
        }

        fclose(fp);

        if ( i != r) {
                printf("Read data error\n");
                return FLASE;
        }

        return TRUE;
}

(3) 判断给定的数是否在二维数组中(算法)

/*Filename:     find_num_in_array.c
 *Function:     find the special number in arrar.dat
 *Author:       One filsh
 *Date:         2014.11.4 Thu
 *Last Modified:2014.11.9 Sun
 */
#include <stdio.h>
#include "cfun.h"


/*@find_c_in_array:     if num if pp memory, find it
  @arg:                 pp is the memory address of the array
                        r, c is the row and column of file, respectlly
                        num is the number will be find in pp
  @rel:                 TRUE if find num in pp, FLASE if not find
 */
int find_num_in_array(int **pp, const int r, const int c, const int num)
{
        int i, j;

        for ( i = 0, j = c - 1; i < r, j >= 0; ) {
                if (num == pp[i][j]) {
                        printf("\n----In find_num_in_array()----\n%d in palce pp[%d][%d]\n", num, i, j);
                        return TRUE;
                } else if (num < pp[i][j]) {
                        j--;
                } else if (num > pp[i][j]) {
                        i++;
                }

        }

        printf("i=%d, j=%d\n", i, j);
        if (i >= r - 1 && j <= 0) {
                printf("\n----In find_num_in_array()----\n%d not  in array\n", num);
                return FLASE;
        }
}

4 Makefile

main:   main.o  get_file_row_column.o   allocate_mm.o   read_data_in.o find_num_in_array.o
        $(CC) main.o  get_file_row_column.o   allocate_mm.o read_data_in.o find_num_in_array.o $(OFLAGS) main


main.o: main.c cfun.h
        $(CC) $(CFLAGS) main.c


get_file_row_column.o:  get_file_row_column.c cfun.h
        $(CC) $(CFLAGS) get_file_row_column.c


allocate_mm.o:  allocate_mm.c
        $(CC) $(CFLAGS) allocate_mm.c


read_data_in.o: read_data_in.c cfun.h
        $(CC) $(CFLAGS) read_data_in.c


find_num_in_array.o: find_num_in_array.c cfun.h
        $(CC) $(CFLAGS) find_num_in_array.c


.PHONY:clean
clean:
        -rm *.o

CC=gcc
OFLAGS=-o
CFLAGS=-c

5 编译和执行结果

注:所有的文件都在同一目录下。


使用make命令编译整个程序:

lly@debian:~/Sword_Offer/14Ten$  make

gcc  -c  main.c

gcc  -c  get_file_row_column.c

gcc  -c  allocate_mm.c

gcc  -c  read_data_in.c

gcc  -c  find_num_in_array.c

gcc  main.o   get_file_row_column.o    allocate_mm.o  read_data_in.o  find_num_in_array.o  -o  main

lly@debian:~/Sword_Offer/14Ten$


给定命令行参数执行main:

lly@debian:~/Sword_Offer/14Ten$  ./main array.dat 7

 

----In get_file_row_column() function----

The array.dat  file has 4 rows, 4 columns

 

----In allocate_mm() function----

----Allocate success, Allocate memory 4 rows, they are:

pp[0]: pp[0][0]0x9aed020        pp[0][1]0x9aed024       pp[0][2]0x9aed028       pp[0][3]0x9aed02c

pp[1]: pp[1][0]0x9aed038        pp[1][1]0x9aed03c       pp[1][2]0x9aed040       pp[1][3]0x9aed044

pp[2]: pp[2][0]0x9aed050        pp[2][1]0x9aed054       pp[2][2]0x9aed058       pp[2][3]0x9aed05c

pp[3]: pp[3][0]0x9aed068        pp[3][1]0x9aed06c       pp[3][2]0x9aed070       pp[3][3]0x9aed074

 

----In read_data_in() function----

data  in memory:

pp[0] line: 1   2       8       9

pp[1] line: 2   4       9       12

pp[2] line: 4   7       10      13

pp[3] line: 6   9       11      15

 

----In find_num_in_array()----

7 in palce pp[2][1]

^_^

 

----In fre_pp() function----

Now free pp[0]: 0x9aed020

Now free pp[1]: 0x9aed038

Now free pp[2]: 0x9aed050

Now free pp[3]: 0x9aed068

lly@debian:~/Sword_Offer/14Ten$ ./main array.dat 5

 

----In get_file_row_column() function----

The array.dat file has 4 rows, 4 columns

 

----In allocate_mm() function----

----Allocate success, Allocate memory 4 rows, they are:

pp[0]: pp[0][0]0x9703020        pp[0][1]0x9703024       pp[0][2]0x9703028       pp[0][3]0x970302c

pp[1]: pp[1][0]0x9703038        pp[1][1]0x970303c       pp[1][2]0x9703040       pp[1][3]0x9703044

pp[2]: pp[2][0]0x9703050        pp[2][1]0x9703054       pp[2][2]0x9703058       pp[2][3]0x970305c

pp[3]: pp[3][0]0x9703068        pp[3][1]0x970306c       pp[3][2]0x9703070       pp[3][3]0x9703074

 

----In read_data_in() function----

data  in memory:

pp[0] line: 1   2       8       9

pp[1] line: 2   4       9       12

pp[2] line: 4   7       10      13

pp[3] line: 6   9       11      15

 

----In find_num_in_array()----

5 not  in array

Not ^_^

 

----In fre_pp() function----

Now free pp[0]: 0x9703020

Now free pp[1]: 0x9703038

Now free pp[2]: 0x9703050

Now free pp[3]: 0x9703068

lly@debian:~/Sword_Offer/14Ten$


[2014.11.9 --23:09]

R《Sword to Offer》 Note Over.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值