指针的简单练习
2014-12-19 08:58
141 查看
1.给定一个整型数组,求该数组中数值小于 10 的元素的个数。
int counter(int* array, int length,int* countp)
{
int count=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
if(countp == NULL) return -1;
for(int i=0;i<length;i++) count+=(array[i]<10)?1:0;
//printf(“the count of the number less than 10 is %d.\n”,count);//for debug
*countp = count;
return 0;
}
//2.给定一个整型数组,计算大于该数组平均值的元素的个数。
⁃ int counter(int* array, int length,int* countp)
{
int count=0, sum=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
if(countp == NULL) return -1;
for(int j=0;j<length;j++) sum+=array[j];
for(int i=0;i<length;i++) count+=(array[i]>((double)sum/(double)length))?1:0;
//printf(“the count of the number greater than the average %lf is %d.\n”,(double)sum/(double)length,count);//for debug
*countp = count;
return 0;
}
//3.给定一个整型数组,找到数组中的最小值,并将其放到数组的首元素中,
原来首元素的内容放到最小值所在的元素中。
3.给定一个整型数组,找到数组中的最小值,并将其放到数组的首元素中,原来首元
素的内容放到最小值所在的元素中。
int exchange(int* array, int length)
{
int index=0, i=0,temp=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
for(i=1;i<length;i++) index=(array[index]<array[i])?index:i;
temp=array[0];array[0]=array[index];array[index]=temp;
return 0;
}
//4.给定一个整型数组,统计某个整数在数组中出现的次数。
⁃ int counter(int* array, int length,int n,int* countp)
{
int count=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
//if(n <= 0) return -1;//no proper illegal check
if(countp == NULL) return -1;
for(int i=0;i<10;i++) count+=(array[i]==n)?1:0;
//printf(“the count of the number %d is %d.\n”,n,count);//for debug
*countp = count;
return 0;
}
5.给定一个英文句子,单词之间用 1 个空格分开,求出第 n 个单词的偏移位置。例如
//“Professor du comes from Korea”的偏移位置是 10。
int location(char* s, int length,int n,int* location)
{
//char *s = “Professor du comes from Korea”;
char ch=‘\0’;
int index=0;
if(s == NULL) return -1;
if(length <= 0) return -1;
if(n <= 0) return -1;
if(location == NULL) return -1;
ch=s[0];
index=1;
while(ch!=‘\0’) {ch=s[index++]; if(ch == ‘ ’)break;}
//printf(“The index of the second word is %d.\n”,index);//for debug
*location = index;
return 0;
}
int counter(int* array, int length,int* countp)
{
int count=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
if(countp == NULL) return -1;
for(int i=0;i<length;i++) count+=(array[i]<10)?1:0;
//printf(“the count of the number less than 10 is %d.\n”,count);//for debug
*countp = count;
return 0;
}
//2.给定一个整型数组,计算大于该数组平均值的元素的个数。
⁃ int counter(int* array, int length,int* countp)
{
int count=0, sum=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
if(countp == NULL) return -1;
for(int j=0;j<length;j++) sum+=array[j];
for(int i=0;i<length;i++) count+=(array[i]>((double)sum/(double)length))?1:0;
//printf(“the count of the number greater than the average %lf is %d.\n”,(double)sum/(double)length,count);//for debug
*countp = count;
return 0;
}
//3.给定一个整型数组,找到数组中的最小值,并将其放到数组的首元素中,
原来首元素的内容放到最小值所在的元素中。
3.给定一个整型数组,找到数组中的最小值,并将其放到数组的首元素中,原来首元
素的内容放到最小值所在的元素中。
int exchange(int* array, int length)
{
int index=0, i=0,temp=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
for(i=1;i<length;i++) index=(array[index]<array[i])?index:i;
temp=array[0];array[0]=array[index];array[index]=temp;
return 0;
}
//4.给定一个整型数组,统计某个整数在数组中出现的次数。
⁃ int counter(int* array, int length,int n,int* countp)
{
int count=0;
if(array == NULL) return -1;
if(length <= 0) return -1;
//if(n <= 0) return -1;//no proper illegal check
if(countp == NULL) return -1;
for(int i=0;i<10;i++) count+=(array[i]==n)?1:0;
//printf(“the count of the number %d is %d.\n”,n,count);//for debug
*countp = count;
return 0;
}
5.给定一个英文句子,单词之间用 1 个空格分开,求出第 n 个单词的偏移位置。例如
//“Professor du comes from Korea”的偏移位置是 10。
int location(char* s, int length,int n,int* location)
{
//char *s = “Professor du comes from Korea”;
char ch=‘\0’;
int index=0;
if(s == NULL) return -1;
if(length <= 0) return -1;
if(n <= 0) return -1;
if(location == NULL) return -1;
ch=s[0];
index=1;
while(ch!=‘\0’) {ch=s[index++]; if(ch == ‘ ’)break;}
//printf(“The index of the second word is %d.\n”,index);//for debug
*location = index;
return 0;
}
相关文章推荐
- 结构体,文件操作,指针,简单练习
- 最简单的指针与地址练习(二维数组)0
- 最简单的指针与地址练习(二维数组)0
- Delphi指针简单入门
- 综合练习:简单日历
- 图片查看软件使用JAVA编写的,比较简单!从书上看下来的,练习使用!
- 单片机练习-RC-5红外遥控器程序及简单制造DIY PC遥控器
- 综合练习:简单日历
- 指针练习实例--(针对C新手)(转)
- 嵌入式中指针几个简单却常见的用法
- delphi指针简单入门:
- 创造性想象的一种简单练习方法
- Delphi下使用指针的简单总结
- CCNA课堂练习:CCNS的模式与RIP的简单配置
- Photoshop基础:简单练习光影特效
- wxPython 第一个简单的练习
- 指针与句柄的简单转换
- 一个简单的Tcp编程练习。
- 简单使用nHibernate,新手练习用。
- c语言-简单学生信息管理(内存链表练习)