c语言作业编程问题

作者&投稿:冀发 (若有异议请与网页底部的电邮联系)
c语言作业问题~

#include #include void main(){float x,y;scanf("%f,%f",&x,&y);if(abs(x*x+y*y-1)<0.001)printf("Y");else printf("N");}


#includeint main(){void p(int a,int b);int a=5,b=8;p(a,b);printf("main():%d,%d
",a,b);p(a+b,b-a);return 0;}void p(int a,int b){a++;b--;printf("p() :%d%d
",a,b);}

下面是我给的答案,有问题的联系我,再讨论。从12点多一直做到现在,别忘了给我选成推荐答案哈。累傻了我了都

1、 杨辉三角形的每一项数据正好是组合 (即s(n!/m!/(n-m)!)的值,其中n是行数(从0行开始);m是列数(从0列开始)。 请使用上述算法得到杨辉三角形每一个位置的值并按下图打印。 要求用函数f计算一个正整数的阶乘(用递归函数来实现),通过主函数调用f完成计算。

答: 下面是我的源代码,程序输出的部分在最后的注释里面

/*
* yanghui.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: sohu2000000@hotmail.com
*/

#include <cstdio>
#include <iostream>

using namespace std;

int fac(int base){
if(base==1){
return 1;
}
return fac(base-1)*base;
}

int triDisplay(int** a, int row, int col){
if(row != col){
perror("must : row=col");
return (-1);
}
int sz=row;

printf("\n");
for(int i=0;i<sz;++i){
*((int*)a+i*sz+0)=1;
*((int*)a+i*sz+i)=1;
}

for(int n=2;n<sz;++n)
for(int m=1;m<n;++m){
*((int*)a+n*sz+m) = fac(n)/fac(m)/fac(n-m);
}

for(int n=0;n<sz;++n){
for(int m=0;m<=n;++m){
printf("%5d", *((int*)a+n*sz+m) );
}
printf("\n");
}
return 0;

}

int
main(void){
int data[10][10];
int size=10;

if(triDisplay((int**)data,size,size)<0){
perror("bad mtria");
exit(-2);
}
return 0;
}

/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

*/

2. 编写一个函数,要求对n个学生的成绩进行排序,要求用数组名作函数参数。在数组a中存放了10个学生某门课程的成绩,调用上述函数,实现对10个学生的成绩排序。

答: 程序的源代码如下,程序的输出在最后面的注释里面

/*
* DcSort.cc
*
* Created on: 2010-6-14
* Author: LiuFeng
* Email: sohu2000000@hotmail.com
*
* g++ -g -O3 DcSort.cc -Wall -o dsort
*/

#include <cstdio>
#include <cstring>
#include <cstdlib>

typedef struct
{
char _sname[30];
double _sscore;
} stu;

int
Partition(stu * stus, int l, int h)
{
stu pivot = stus[l];

while(l<h){
while(l<h && ((stus[h]._sscore) >= (pivot._sscore))) h--;
if(l<h) stus[l++]=stus[h];
while(l<h && ((stus[l]._sscore) <= (pivot._sscore))) l++;
if(l<h) stus[h--]=stus[l];
}
stus[l]=pivot;

return l;
}

void DcSort(stu* stus, int low, int high){
int pivotpos;
if(low<high){
pivotpos=Partition(stus,low,high);

DcSort(stus,low,pivotpos-1);
DcSort(stus,pivotpos+1,high);
}
}

int main(void){
stu s[10]={
{"zhao",60},
{"qian",40},
{"sun",80},
{"li",90},
{"zhou",70},
{"wu",50},
{"zheng",80},
{"jiang",90},
{"shen",100},
{"han",80},
};

for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}

DcSort(s,0,10);

printf("\n===================after sorted ==================\n");

for(int i=0; i<10;++i){
printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);
}

return 0;
}

/*

输出结果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000

===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

三、程序运行结果示例:

第一题:
/*
[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc
[Administrator@ /<7>06/14]$ ./tri.exe

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

*/

第二题:

/*

输出结果:
[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort
[Administrator@ ~/<1>preInterView/SortAndFind]$ clear
[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe
student: name: zhao, score: 60.000000
student: name: qian, score: 40.000000
student: name: sun, score: 80.000000
student: name: li, score: 90.000000
student: name: zhou, score: 70.000000
student: name: wu, score: 50.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: shen, score: 100.000000
student: name: han, score: 80.000000

===================after sorted ==================
student: name: qian, score: 40.000000
student: name: wu, score: 50.000000
student: name: zhao, score: 60.000000
student: name: zhou, score: 70.000000
student: name: han, score: 80.000000
student: name: sun, score: 80.000000
student: name: zheng, score: 80.000000
student: name: jiang, score: 90.000000
student: name: li, score: 90.000000
student: name: shen, score: 100.000000
[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

二、实验内容:按题目要求完成程序的改错、调试、填空和编写。

1、以下程序中,main函数通过调用fun()函数统计整数序列中的负数的个数以及平均值。
本题约定平均值由函数返回,负数的个数由参数返回。程序有若干错误,请先阅读程序,找出其中的错误行,
并写出出错的原因,最后上机调试该程序验证自己的预测。

#1 double aver(int a[], int n, int *p) // p所谓返回的参数之一,应该使用二级指针,或者指针的引用;
#2 { int i,sum=0 ; // 这里的*p指向了函数内部的临时变量,且该变量在函数结束时,
// 会同时被系统从栈上释放掉, 那么你在main函数中读取p指向的地址,得到的
// 就是一个不可以预期的值了,也就是“野指针”
// 使用二级指针或者指针的引用,是可以将内存在函数体内外带入带出的,但是
// 注意要在main中先申请好
#3 *p=0 ;
#4 for(i=0;i<n;i++)
#5 { sum=sum+a[i] ;
#6 if(a[i]<0) *p++;
#7 }
#8 return sum/n;
#9 }
#10 #include "stdio.h"
#11 main()
#12 { int count,x[]={0,12,33,-9,-5,27,80,0,54,63};
#13 double av;
#14 av=aver(x,10,count);
#15 printf("count: %d\naverage: %.2f\n",count,av);
#16 }

2、设有如下结构定义,且struct link为链表的结点类型,由该结构类型创建的链表中的a结点已被指针p指向(见下图),请完成下面的操作:

struct link
{ int score;
struct link *next;
}*p,*q;

(1) 写出删除b结点(包括释放其存储空间)的语句序列(允许借助于q指针),但要求链表的连续结构不能破坏,不能移动p指针。

答:

void
deleteNode(link* p, link* q, int b)
{
q = p;
int len=0;

while(q->next)++len;

if(len < b) {perror("You input a overflow position"); exit(-1);}

q=p;
while(--b){ // move to b
q=q->next;
}

while(q->next) { // overwrite the prenode,one by one
q->score=q->>next->score;
}

free(q) // q point to the last node;
}

(2) 阅读下面程序说明,按注释提示,在划线处补充细节,使程序达到预期功能。 [程序说明]以下程序中,函数create的功能是创建一个结点类型为struct link的学生成绩链表,main函数中,首先调用create函数创建一个包含N个结点的成绩链表,然后调用问题(1)的算法,将链表的第2个结点删除掉,要求输出结点删除前、后链表的内容,以验证问题(1)算法的正确性。

#include <stdio.h>
#define N 5
#define L sizeof(struct link)
struct link
{ int score; /* 成绩 */
(1);/* 定义结点的指针域next */
};

struct link *create(void)
{
struct link *head,*p;
int i;
head=NULL;
printf("Input %d records\n",N);
for(i=1;i<=N;i++)
{
p=(2) ; /* 创建一个动态结点 */
scanf("%d",&p->score);
(3); /* 新结点进栈 */
head=p;
}
return (4) ; /* 返回所创建链表的头指针 */
}

void printlist(struct link *head) /* 输出链表 */
{ struct link *p;
p=head;
while(p!=NULL)
{ printf("%d,",p->score);
(5) ; /* 使p后移一个结点 */
}
printf("\n");
}

int
main( void)
{ struct link *base,*new;
(6) ; /* 调用create函数,创建链表 */
printlist(base); /* 输出原始链表 */
/* 借助于new指针删除第2个结点 */

(8) ; /* 输出结点删除以后的链表内容 */
printf("ok!\n");
}

(2)答:
【 (1) link * next; 】
【 (2) p= (link*)malloc(sizeof(struct link)); 】
【 (3) p->next = head 】
【 (4) head 】
【 (5) p=p->next 】
【 (6) base = create(); 】
【 (7) deleteNode(base, new, int b); // 利用我们上一问中,写好的函数 】
【 (8) printlist(base);】


C语言大作业,这题该怎么写?
图片看不清,只看到学生信息录入。学生信息包含多个类型数据,比如学生学号、成绩、班级、年级、学科、成绩等等。所以学生应该定义为结构体。学生数量可以用常量表示。之后定义结构数组用于循环接收学生信息的录入。之后学生信息的输出,成绩分析,求平均之类,都只是对结构数组的循环遍历。

编程高手帮我解决个小问题吧,谢谢啦、是作业题,用c++语言编写
第一题:include <iostream> using namespace std;int main (){ int a[10][10];int n,i;a[0][0]=1;for (n=1;n<10;n++){ for (i=0;i<=n;i++){ if ((i==0)||(i==n))a[n][0]=a[n][n]=1;else { a[n][i]=a[n-1][i-1]+a[n-1][i];} } } for (...

c语言作业输入一个实数 x ,计算 y=x2 ,输出 x,y。求问为什么错了,十分...
因为C语言中x的二次方用x*x表示或者用函数pow(x,2)表示 (需要包含头文件#include<math.h>)所以y=x^2在C语言中不是表示x的二次方,而是x按位异或2,因此结果不对.另外,题目要求输入一个实数,所以x,y要被定义为double型,输入输出格式符%d要改成%lf 改正后的程序如下(见图,改动的地方见注释)...

c语言编程问题。。。这是一个作业。。实在不会写,求大神帮忙。。。_百...
include<stdio.h>struct Poker{char num;char color;};int comparenum(char a,char b){a=(a=='A'?'9'+5:a);b=(b=='A'?'9'+5:b);a=(a=='K'?'9'+4:a);b=(b=='K'?'9'+4:b);a=(a=='Q'?'9'+3:a);b=(b=='Q'?'9'+3:b);a=(a=='...

c语言编程求助~ 编程作业: 1.输入10名学生5门课程的成绩,分别用函数求...
include <stdio.h> include <math.h> include define N 10 int maxi,maxj;void aver(int a[][5]){ int i,j;printf("Average:");for(i=0;i<N;i++)for(j=0;j<5;j++)a[N][j]+=a[i][j];for(i=0;i<5;i++)printf("%6.1f",(float)a[N][i]\/10);printf("\\n")...

关于python语言的编程作业,有没有人能帮我看看这题?
首先是5个循环用于输入五个字符串的 然后遍历字符串并统计小写字符数量 根据字符数量再进行相应输出即可 如果代码不想自己写的话 我可以有偿代劳

大一C语言作业:“编写程序输入两个整数,输出它们的商和余数?”应该怎么...
识字,可以阅读C语言教材;认识英文字母,标点符合,阿拉伯数字,并可以在电脑上打出来。1、数学基础。C语言中很多方面涉及数学模型,所以数学好对C的学习帮助很大。2、英文基础。流畅的英文阅读能力,可以辅助C语言学习。3、基本的硬件知识。C语言是一门直接面向底层,可以操作硬件的计算机编程语言,如果可以...

C语言 大一新生C语言作业,程序不知道哪里出了问题,有人能帮忙看下么...
[b];if(score[a][b]<min)max=score[a][b];} printf("%c科目的最高成绩为%f\\n",course[b],max);printf("%c科目的最低成绩为%f\\n",course[b],min);} } 至于scanf("%f",&score[i][j]);你这边少了一个&,这肯定是个问题 不过我是直接填的数据,没有输入,像rxprince所说 ...

编程(Fortran语言) 命题作业求解答
不太确定题主希望得到什么帮助。对题目的源代码进行了一点修改。1、用循环代替了goto语句,更符合结构化编程习惯。2、将读取的x转赋给r,使得分解二进制的运算不影响到x的值。3、增加了输出的语句,用等式直观表达十进制到二进制的结果。4、将二进制的位数增加到integer*4,即32位。供您参考。附:...

C语言作业题急于求解
1.int i;for(i=1;i<=100;i++){ if(i%3==0%%(i&0x06==6))printf("%d\\n",i);} 2.int a[4][4];int i,j;for(i=0;i<4;i++)for(j=0;j<4;j++)scanf("%d",&a[i][j]);for(i=0;i<4;i++)for(j=0;j<4;j++)if(i==0||j==0||i==3|j==3)printf("...

高密市13770688629: 大学生C语言编程作业题,请高手帮忙,急! -
撒炎升华: #include int main(void) { int money, year; double interest, rate; /**-----------*/ scanf("%d", &money ); scanf("%d", &year ); scanf("%lf", &rate ); interest=money*pow((1+rate), year )-money ; /*---------*/ printf("interest = %.2f\n", interest);return 0; }

高密市13770688629: C语言作业编程问题 -
撒炎升华: for (i=1;i<=30;i++) {a[i]=i; //定义a[i]为1~30的整数a[1]=0; //筛掉1 }for(i = 2;i <= n;i ++) //i从2开始 if(a[i] == 1) //如果i是素数 { printf("%5d",i); //输出i for(j = i;j <= n; j += i) a[j] = 0;//将i后面的数里面是i的倍数的数都标记成合数 }

高密市13770688629: C语言编程问题
撒炎升华: 这个不能确定数组大小的问题凡是刚学C的都会遇到,我们老师给的建议是用一个较大数就行了,因为那个里面是不准用变量的,所以你想用switch来判断也是不可行的

高密市13770688629: C语言编程问题
撒炎升华: 第一:设置三个变量:两个整型的,一个字符的,两个整型是用来输入两个数的,而字符型用来输入运算法; 第二:判断一下输入的字符类型,可以用if或者switch; 第三:用输出语句直接输出结果; main() {int a,b; char c; scnaf("%d%c%d",...

高密市13770688629: C语言编程问题
撒炎升华: 你好,我们是问问编程专业服务团队,很高兴能为你提供服务 首先,感谢你的支持,你的支持就是我们最大的动力! 对于这个问题,解决方法是: main() { int a=10; int b=15; int c; c=10*max(a,b); // 编译时候 这句替换为 c = 10*a>b?a:b; 即c = 100 > 15?10:15; 结果应该是10 printf("%d\n",c); getch(); } 应该这样定义宏 #define max(x,y) ((x>y)?x:y) 以上答案仅供参考,谢谢!

高密市13770688629: c语言编程问题 -
撒炎升华: #include<stdio.h> int main() { char name[20],name_a[20];//定义2个数组用于存放输入的拼音和加密后的拼音 int i=0,j=0; name[i]=getchar();//首先输入第一个拼音while(name[i]!='\n')//判断输入的拼音是不是回车键,输入回车键表示输出完成...

高密市13770688629: C语言编程问题?!?
撒炎升华: #include<stdio.h> void main() { int m; int n; int i,j; int a[100][100]; //当然你可以将数组定义的足够大 printf( "请输入一个矩阵的行数:\n" ); scanf( "%d", &m ); printf( "请输入一个举证的列数:\n" ); scanf( "%d", &n ); printf( "请...

高密市13770688629: 关于C语言编程问题 -
撒炎升华: "我在TURBO C上运行了也没有错误"按Ctrl+F9运行……另外在TC的文件夹里(默认),生成了对应的exe文件,那个程序可以用于发布.引用一下别人说的:“珍爱生命,远离TC”……

高密市13770688629: C语言的 编程问题
撒炎升华: 其实是从1000开始的,只不过是因为命令运行窗口显示有限,所以是给你的感觉是从9702开始的,打印数太多,显示不出来而已!不信你把j<10000改成j<1100 ,看看是不是从1000开始的! 运行后 去C盘 找到一个文件为 b.txt的文件,打开 里面就是结果了!

高密市13770688629: C语言的编程问题
撒炎升华: #include <stdio.h> int main() { int i, j, s; for(i = 1; i < 1000; i++) { s = 0; for(j = 1; j <= i / 2; j++) if(i % j == 0) s += j; if(s == i) { printf("%d its factors are 1", i); for(j = 2; j <= i / 2; j++) if(i % j == 0) printf(",%d", j); printf("\n"); } } return 0; }

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网