生成一个一维数组,有10个元素,都用随机数填充,用指针轮询的办法实现函数查找一个数是否存在。
2015-01-08 23:11
981 查看
题目:生成一个一维数组,有10个元素,都用随机数填充,用指针轮询的办法实现函数查找一个数是否存在,具体实现代码如下:
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int find(int * pInput, int nLen, int nKey, int * pOut)
{
if (!pInput)
{
return 0;
}
if (!pOut)
{
return 0;
}
int * pCurPos = pInput;
int * pEndPos = pInput + nLen;
*pOut = 0;
while (pCurPos < pEndPos)
{
if (*pCurPos == nKey)
{
*pOut = 1;
break;
}
pCurPos++;
}
return 1;
}
int main()
{
int * p = (int *)malloc(sizeof(int)* 10);
if (!p)
{
printf("内存分配失败.\n");
return 0;
}
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
p[i] = rand() % 10;
}
printf("数组元素:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
printf("\n");
int nTemp = 0;
int nFind = 0;
printf("请输人要查询的数据:\n");
scanf("%d", &nTemp);
if (find(p, 10, nTemp, &nFind) == 0)
{
printf("查询失败.\n");
}
else
{
if (nFind == 0)
{
printf("未找到元素:%d\n", nTemp);
}
else
{
printf("已经找到元素:%d\n", nTemp);
}
}
if (p)
{
free(p);
}
system("pause");
return 0;
}
运行效果如图1所示:
图1 运行效果
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int find(int * pInput, int nLen, int nKey, int * pOut)
{
if (!pInput)
{
return 0;
}
if (!pOut)
{
return 0;
}
int * pCurPos = pInput;
int * pEndPos = pInput + nLen;
*pOut = 0;
while (pCurPos < pEndPos)
{
if (*pCurPos == nKey)
{
*pOut = 1;
break;
}
pCurPos++;
}
return 1;
}
int main()
{
int * p = (int *)malloc(sizeof(int)* 10);
if (!p)
{
printf("内存分配失败.\n");
return 0;
}
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
p[i] = rand() % 10;
}
printf("数组元素:\n");
for (i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
printf("\n");
int nTemp = 0;
int nFind = 0;
printf("请输人要查询的数据:\n");
scanf("%d", &nTemp);
if (find(p, 10, nTemp, &nFind) == 0)
{
printf("查询失败.\n");
}
else
{
if (nFind == 0)
{
printf("未找到元素:%d\n", nTemp);
}
else
{
printf("已经找到元素:%d\n", nTemp);
}
}
if (p)
{
free(p);
}
system("pause");
return 0;
}
运行效果如图1所示:
图1 运行效果
相关文章推荐
- 生成一个一维数组,有10个元素,都用随机数填充,用指针轮询的办法实现函数查找一个数是否存在。
- 生成一个一维数组,有10个元素,都用随机数填充,用指针轮询的办法实现函数查找一个数是否存在。
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- Java生成10个随机数,填充一个数组,用消息框显示数组内容,后求和输出
- 声明一个指向含有10个元素的数组的指针,其中每个元素是一个函数指针,该函数的返回值是int,参数是int*,正确的是()
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- Appium - 一个投巧的办法去验证页面是否存在某个元素
- 给你一个能生成1到5随机数的函数,用它写一个函数生成1到7的随机数。 (即,使用函数rand5()来实现函数rand7())
- 实验9 指针1 、程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依
- 实验九指针1、程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依据。
- 经典面试题:求数组的最大子序列和;实现函数 :判断一个自负喜欢是否是另一个字符串旋转所得;杨氏矩阵中查找一个数。
- 实现列表类,清空、添加、删除、查找、插入,判断元素是否存在
- 关于一个查找数组中是否存在某一个元素的问题
- 实现一个函数,传递一个有10个已经排好序的整形元素数组(数组大小实际为11)中,将一个指定的值按顺序插入,并在main函数中将插入后的数组输出。
- javascript 如何判断元素是否包含一个特定的类,hasClass函数的实现
- 编写函数实现一个字符串在另一个字符串中是否存在
- 实验9 指针1 1、程序填空:下列程序在数组中同时查找最大元素和最小元素的下标,分别存放在 main()函数的max 和 min 变量中。要求:根据运行结果分析程序和填空,并注释说明填充依据。
- (学习java)写一个完整的程序,实现随机生成20个元素的链表,快速查找中间结点的值并显示