《C和指针》——6.6

题目:

  在指定的下限、上限之间使用数组方法查找质数,并将质数提取出来。

要求:

  略

解答代码:

#include <stdio.h>

#define UPLIMIT        11000
#define DOWNLIMIT 10000
#define NUM UPLIMIT-DOWNLIMIT void show_array(int *p, int n) //显示数组p[]中的n个元素
{
int i;
for(i=; i<n; i++)
{
if ((i% == ) && (i!=))
printf("\n");
printf("%6d", *(p+i));
}
printf("\n");
} void def_array(int *p) //初始化数组元素值为序号+1
{
int i;
for(i=; (i+DOWNLIMIT)<(UPLIMIT); i++)
{
*(p+i) = i++DOWNLIMIT;
}
} void DataProcess(int *p) //将数组中的非质数替换为0
{
int i, j;
for(i=; i<=(UPLIMIT/); i++)
{
for(j=; j<NUM; j++)
{
if ((*(p+j) >= ) && (*(p+j) != i) && (*(p+j) % i == ))
{
*(p+j) = ;
}
}
}
} int DataSelect(int *p) //将数组中的所有0去掉
{
int i, j;
for(i=, j=; i<NUM; i++)
{
if(*(p+i) > )
{
*(p+j) = *(p+i);
j++;
}
}
return j;
} int main(void)
{
int arrayt[NUM];
int *p = arrayt;
int n = ; def_array(p); //初始化数组元素值为索引号+1
show_array(p, NUM); //显示数组p[]中的n个元素
DataProcess(p); //将数组中的非质数替换为0
n = DataSelect(p); //将数组中的所有0去掉,n为质数的个数
printf("There are %d numbers:\n", n);
show_array(p, n); getchar();
return ;
}

注:

  下限和上限可以设置

《C和指针》章节后编程练习解答参考——6.6的更多相关文章

  1. 《C和指针》章节后编程练习解答参考——6.2

    <C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...

  2. 《C和指针》章节后编程练习解答参考——6.3

    <C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...

  3. 《C和指针》章节后编程练习解答参考——第5章

    5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...

  4. 《C和指针》章节后编程练习解答参考——6.4

    <C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...

  5. 《C和指针》章节后编程练习解答参考——6.1

    <C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...

  6. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...

  7. 《C和指针》章节后编程练习解答参考——第9章

    9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...

  8. 《C和指针》章节后编程练习解答参考——第8章

    8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...

  9. DSAPI多功能组件编程应用-参考-Win32API常数

    DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...

随机推荐

  1. Windows下安装Eric5时出现的“Sorry, please install QtHelp.”问题解决办法

    解决Windows下安装Eric5时出现的“Sorry, please install QtHelp.”问题   PyQt4在Windows中使用了DirectX作为加速,不过,PyQt4没有使用最新 ...

  2. JMeter对Oracle数据库进行压力测试

    步骤 (1)复制ORACLE的JDBC驱动JAR包文件(ojdbc14.jar)到JMeter的lib目录下. (2)运行jmeter.bat (3)建立线程组:右键测试计划->添加->T ...

  3. oracle本月、上月、去年同月第一天最后一天

    select trunc(sysdate, 'month') 本月第一天,        trunc(last_day(sysdate)) 本月最后一天,        trunc(add_month ...

  4. [rxjs] Async, handle data over time

    If I have an array, and I want to apply filter, map, forEach to it. let Observable = Rx.Observable; ...

  5. android 65 文件访问权限

    package com.itheima.createfile; import java.io.File; import java.io.FileNotFoundException; import ja ...

  6. mybatis11 sqlMapConfig.xml文件说明

    1sqlMapConfig.xml SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typ ...

  7. WinCacheGrind配合XDebug分析PHP程序性能

    http://www.nowamagic.net/librarys/veda/detail/2338

  8. Exploring Message Brokers: RabbitMQ, Kafka, ActiveMQ, and Kestrel--reference

    [This article was originally written by Yves Trudeau.] http://java.dzone.com/articles/exploring-mess ...

  9. Obj2002java

    计算球体积 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. static inner class 什么时候被加载

    一直认为在加载outer class 的同时也会加载inner class 并且完成静态变量和代码块的初始化,今天在维基百科上面看到 “The static class definitionLazyH ...