C语言中如何实现二维数组元素的查找示例?

2026-05-20 01:161阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计886个文字,预计阅读时间需要4分钟。

C语言中如何实现二维数组元素的查找示例?

C语言实现二维数组查找的示例及题目描述:

题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排列,每一列也都按照从上到下递增的顺序排列。请实现一个函数,在该二维数组中查找一个指定的值,并返回其所在的位置。

函数原型:cint* findElementIn2DArray(int** array, int rows, int cols, int target, int* resultRow, int* resultCol);

函数参数:- `array`:指向二维数组的指针- `rows`:二维数组的行数- `cols`:二维数组的列数- `target`:要查找的目标值- `resultRow`:查找结果的行索引,输出参数- `resultCol`:查找结果的列索引,输出参数

函数返回值:- 查找到目标值时返回1,并设置`resultRow`和`resultCol`为对应的行和列索引- 没有查找到目标值时返回0

示例代码:c#include #include

int* findElementIn2DArray(int** array, int rows, int cols, int target, int* resultRow, int* resultCol) { int* result=(int*)malloc(sizeof(int) * 2); if (result==NULL) { return NULL; } result[0]=-1; // 默认未找到 result[1]=-1;

int i=0, j=cols - 1; // 从左下角开始查找 while (i =0) { if (array[i][j]==target) { result[0]=i; result[1]=j; break; } else if (array[i][j] > target) { j--; } else { i++; } } return result;}

int main() { int rows=4, cols=5; int target=3; int array=(int)malloc(rows * sizeof(int*)); for (int i=0; i

int* result=findElementIn2DArray(array, rows, cols, target, &resultRow, &resultCol); if (result[0] !=-1) { printf(Found %d at row %d, col %d\n, target, result[0], result[1]); } else { printf(Element %d not found\n, target); }

// 释放内存 for (int i=0; i

return 0;}

C语言中如何实现二维数组元素的查找示例?

C语言二维数组中的查找的实例

题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数

思路描述:一个数字的下方和右方是比它本身大的区域,而左方和上方时比它本身小的区域。选取右上角的数字进行比较,当该数大于指定的数时,舍去该列,当该数小于指定的数时,舍去该行,当相等时,则表示找到

C语言实现:

#include<stdio.h> #include<stdlib.h> typedef unsigned int boolean; #define MAX 4 #define TRUE 1 #define FALSE -1 void showAry(int ary[MAX][MAX]); boolean find(int ary[MAX][MAX], int rows, int cols, int number); void showAry(int ary[MAX][MAX]) { int i = 0, j = 0; for(; i < MAX; i++) { j = 0; for(; j < MAX; j++) { printf("%d ", ary[i][j]); } } } boolean find(int ary[MAX][MAX], int rows, int cols, int number) { int i = 0, j = cols - 1, n = 0; boolean result = FALSE; if(ary == NULL || rows <= 0 || cols <= 0) { return result; } while(i < rows && j >= 0) { n = ary[i][j]; if(number == n) { printf("\nary[%d, %d] = %d\n", i, j, n); result = TRUE; break; }else if(number < n) { j -= 1; }else if(number > n) { i += 1; } } return result; } //1 2 8 9 2 4 9 12 4 7 10 13 6 8 11 15 int main() { int ary[MAX][MAX]; int i = 0, j = 0; for(; i < MAX; i++) { j = 0; for(; j < MAX; j++) { scanf("%d", &ary[i][j]); } } showAry(ary); find(ary, MAX, MAX, 7); }

以上就是讲解C语言二维数组中的查找的实例,希望能帮助需要同类型问题的朋友,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

本文共计886个文字,预计阅读时间需要4分钟。

C语言中如何实现二维数组元素的查找示例?

C语言实现二维数组查找的示例及题目描述:

题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排列,每一列也都按照从上到下递增的顺序排列。请实现一个函数,在该二维数组中查找一个指定的值,并返回其所在的位置。

函数原型:cint* findElementIn2DArray(int** array, int rows, int cols, int target, int* resultRow, int* resultCol);

函数参数:- `array`:指向二维数组的指针- `rows`:二维数组的行数- `cols`:二维数组的列数- `target`:要查找的目标值- `resultRow`:查找结果的行索引,输出参数- `resultCol`:查找结果的列索引,输出参数

函数返回值:- 查找到目标值时返回1,并设置`resultRow`和`resultCol`为对应的行和列索引- 没有查找到目标值时返回0

示例代码:c#include #include

int* findElementIn2DArray(int** array, int rows, int cols, int target, int* resultRow, int* resultCol) { int* result=(int*)malloc(sizeof(int) * 2); if (result==NULL) { return NULL; } result[0]=-1; // 默认未找到 result[1]=-1;

int i=0, j=cols - 1; // 从左下角开始查找 while (i =0) { if (array[i][j]==target) { result[0]=i; result[1]=j; break; } else if (array[i][j] > target) { j--; } else { i++; } } return result;}

int main() { int rows=4, cols=5; int target=3; int array=(int)malloc(rows * sizeof(int*)); for (int i=0; i

int* result=findElementIn2DArray(array, rows, cols, target, &resultRow, &resultCol); if (result[0] !=-1) { printf(Found %d at row %d, col %d\n, target, result[0], result[1]); } else { printf(Element %d not found\n, target); }

// 释放内存 for (int i=0; i

return 0;}

C语言中如何实现二维数组元素的查找示例?

C语言二维数组中的查找的实例

题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数

思路描述:一个数字的下方和右方是比它本身大的区域,而左方和上方时比它本身小的区域。选取右上角的数字进行比较,当该数大于指定的数时,舍去该列,当该数小于指定的数时,舍去该行,当相等时,则表示找到

C语言实现:

#include<stdio.h> #include<stdlib.h> typedef unsigned int boolean; #define MAX 4 #define TRUE 1 #define FALSE -1 void showAry(int ary[MAX][MAX]); boolean find(int ary[MAX][MAX], int rows, int cols, int number); void showAry(int ary[MAX][MAX]) { int i = 0, j = 0; for(; i < MAX; i++) { j = 0; for(; j < MAX; j++) { printf("%d ", ary[i][j]); } } } boolean find(int ary[MAX][MAX], int rows, int cols, int number) { int i = 0, j = cols - 1, n = 0; boolean result = FALSE; if(ary == NULL || rows <= 0 || cols <= 0) { return result; } while(i < rows && j >= 0) { n = ary[i][j]; if(number == n) { printf("\nary[%d, %d] = %d\n", i, j, n); result = TRUE; break; }else if(number < n) { j -= 1; }else if(number > n) { i += 1; } } return result; } //1 2 8 9 2 4 9 12 4 7 10 13 6 8 11 15 int main() { int ary[MAX][MAX]; int i = 0, j = 0; for(; i < MAX; i++) { j = 0; for(; j < MAX; j++) { scanf("%d", &ary[i][j]); } } showAry(ary); find(ary, MAX, MAX, 7); }

以上就是讲解C语言二维数组中的查找的实例,希望能帮助需要同类型问题的朋友,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!