C语言实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言实验报告”。
C语言程序设计(B)
(2010-2011-2)
实验报告2
教学班级:
学号:
姓名:
课程教师:
实验辅导教师:
一、做一个“杨辉三角” 实验前的源程序:
/*杨辉三角*/ #include void fun(int Y[][]){ int i,j,k;for(i=0;i
Y[i][0]=1;
Y[i][i]=1;} for(i=2;i
for(j=1;j
{
Y[i][j]=Y[i-1][j-1]+Y[i-1][j];
}
} } main(){ int i,j,k;int Y[12][12];fun(Y);for(i=0;i
for(k=1;k
printf(“
”);
for(j=0;j
{
printf(“%6d”,Y[i][j]);
}
printf(“n”);} } 实验错误报告:
--------------------配置: mingw2.95-CUI Debug, 编译器类型: MinGW(Old)--------------------
检查文件依赖性...正在编译 D:Program FilesC-Free Standardsamples杨辉三角.cpp...[Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:4: declaration of `Y' as
multidimensional array [Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:4: must have bounds for all
dimensions except the first [Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:8: `Y' undeclared(first use this
function)[Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:8:(Each undeclared identifier is
reported only once [Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:8: for each function it appears
in.)[Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:4: too many arguments to
function `void fun()' [Error] D:Program FilesC-Free Standardsamples杨辉三角.cpp:23: at this point in file
构建中止 杨辉三角: 7 个错误, 0 个警告
实验后的源程序:
/*杨辉三角*/ #include void fun(int Y[12][12]){ int i,j,k;for(i=0;i
Y[i][0]=1;
Y[i][i]=1;} for(i=2;i
for(j=1;j
{
Y[i][j]=Y[i-1][j-1]+Y[i-1][j];
}
} } main(){ int i,j,k;int Y[12][12];fun(Y);for(i=0;i
for(k=1;k
printf(“
”);
for(j=0;j
{
} } printf(“%6d”,Y[i][j]);} printf(“n”);
二、用函数的方法 3*3矩阵置换
实验前的源程序:
/*用函数的方法 3*3矩阵置换*/ #include “stdio.h” main(){ void zhihuan();zhihuan();} void zhihuan(){
int a[3][3],b[3][3];int i,j;printf(“请输入一个3*3的矩阵:”);printf(“n”);for(i=0;i
for(j=0;j
{
printf(“ %d ”,a[i][j]);
b[j][i]=a[i][j];
}
printf(“n”);} printf(“n转置后的矩阵排列为:n”);for(i=0 i
for(j=0;j
{
printf(“ %d ”,b[i][j]);
}
} } printf(“n”);实验错误报告:
[Error] D:Program FilesC-Free Standardtemp未命名3.cpp:30: parse error before `
实验后的源程序:
/*用函数的方法 3*3矩阵置换*/ #include “stdio.h” main(){ void zhihuan();zhihuan();} void zhihuan(){
int a[3][3],b[3][3];int i,j;printf(“请输入一个3*3的矩阵:”);printf(“n”);for(i=0;i
for(j=0;j
{
printf(“ %d ”,a[i][j]);
b[j][i]=a[i][j];
}
printf(“n”);} printf(“n转置后的矩阵排列为:n”);for(i=0;i
for(j=0;j
}
} { printf(“ %d ”,b[i][j]);} printf(“n”);
三、用函数
输入一个字符串按反序存放
实验前的源程序:
/*输入一个字符串按反序存放*/ #include #include “string.h” void fun(char str){
int length,i;
char temp;length=strlen(str);
length=strlen(str);for(i=0;i
temp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=temp;} } main(){ int i,length;char str[89],temp;printf(“ 请输入一串字符:n”);gets(str);fun(str);printf(“
字符的逆序排列为:n
”);puts(str);} 实验错误报告:
[Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:8: paing `char' to argument 1 of `strlen(const char *)' lacks a cast [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:10: paing `char' to argument 1 of `strlen(const char *)' lacks a cast [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存
放.cpp:13: invalid types `char[int]' for array subscript [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:14: invalid types `char[int]' for array subscript [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:14: invalid types `char[int]' for array subscript [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:15: invalid types `char[int]' for array subscript [Error] D:Program FilesC-Free Standardsamples输入一个字符串按反序存放.cpp:24: paing `char *' to argument 1 of `fun(char)' lacks a cast 构建中止 输入一个字符串按反序存放: 7 个错误, 0 个警告
实验后的源程序:
/*输入一个字符串按反序存放*/
#include #include “string.h” void fun(char str[]){
int length,i;
char temp;length=strlen(str);
length=strlen(str);for(i=0;i
temp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=temp;} } main(){ int i,length;char str[89],temp;printf(“ 请输入一串字符:n”);gets(str);fun(str);printf(“
字符的逆序排列为:n
”);puts(str);}
四、用指针的方法 将3*3矩阵转置
实验前的源程序:
/*用指针的方法,将一个3*3整形矩阵转置*/ #include “stdio.h” void Transfer(int(*pointer)[3]){
int t;
int i, j;
for(i=1;i
{
for(j=0;j
{
t=*(*(pointer+j)+i);
*(*(pointer+j)+i)=(*(pointer+i)+j);
*(*(pointer+i)+j)=t;
}
} }
main(){ int i,j;
int a[3][3];
printf(“请输入一个3*3的整形矩阵:n”);
for(i=0;i
{
for(j=0;j
{
scanf(“%d”,&a[i][j]);
}
}
printf(“您所输入的3*3矩阵是:n”);
for(i = 0;i
for(j=0;j
{
printf(“ %d ”,a[i][j]);
}
printf(“n”);
}
Transfer(a);
printf(“转置后的3*3矩阵为:n”);
for(i=0;i
for(j=0;j
} } { printf(“ %d ”,a[i][j]);} printf(“n”);实验错误报告:
正在编译 D:Program FilesC-Free Standardsamples用指针的方法,将一个33整形
矩阵转置.cpp...[Error] D:Program FilesC-Free Standardsamples用指针的方法,将一个33整形矩阵转置.cpp:13: aignment to `int' from `int *' lacks a cast 构建中止 用指针的方法,将一个33整形矩阵转置: 1 个错误, 0 个警告
实验后的源程序:
/*用指针的方法,将一个3*3整形矩阵转置*/ #include “stdio.h” void Transfer(int(*pointer)[3]){
int t;
int i, j;
for(i=1;i
{
for(j=0;j
{
t=*(*(pointer+j)+i);
*(*(pointer+j)+i)=*(*(pointer+i)+j);
*(*(pointer+i)+j)=t;
}
} }
main(){ int i,j;
int a[3][3];
printf(“请输入一个3*3的整形矩阵:n”);
for(i=0;i
{
for(j=0;j
{
scanf(“%d”,&a[i][j]);
}
}
printf(“您所输入的3*3矩阵是:n”);
for(i = 0;i
for(j=0;j
{
printf(“ %d ”,a[i][j]);
}
printf(“n”);
}
Transfer(a);
printf(“转置后的3*3矩阵为:n”);
for(i = 0;i
for(j=0;j
{
printf(“ %d ”,a[i][j]);
}
printf(“n”);} }
五、用指针 请输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换实验前的源程序:
实验前的源程序:
#include void main(){ int a[10],i,*m,*n;printf(“Input 10 numbers:n”);for(i=0;im)m=&a[i];i=a[10];a[10]=*m;*m=i;
} for(i=0;i
正在编译 D:Program FilesC-Free Standardtemp未命名7.cpp...[Error] D:Program FilesC-Free Standardtemp未命名7.cpp:13: ANSI C++ forbids comparison between pointer and integer 构建中止 未命名7: 1 个错误, 0 个警告
实验后的源程序:
#include void main(){
int a[10],i,*m,*n;
printf(“Input 10 numbers:n”);
for(i=0;i
{
scanf(“%d”,&a[i]);
m=n=&a[0];
}
for(i=0;i
{
if(a[i]>*m)
m=&a[i];
i=a[10];
a[10]=*m;
*m=i;
}
for(i=0;i
{
if(a[i]
i=a[9];
a[9]=*n;
*n=i;
}
for(i=0;i
printf(“ %d ”,a[i]);}
六、用指针 求一字符的长度再main中输入在字符串,并输出长度
实验前的源程序:
/*用指针 求一字符的长度 再main中输入在字符串,并输出长度*/ #include int length(char *s){
int i,n=0;
for(i=0;*(s+i)!= ;i++)
n++;
return(n);} void main(){
char *s;
char c[20];
s=c;
printf(“请输入字符串:n”);
gets(c);
printf(“ 字符串长度为%dn”,length(s));} 实验错误报告:
正在编译 D:Program FilesC-Free Standardsamples用指针 求一字符的长度 再main中输入在字符串,并输出长度.cpp...[Error] D:Program FilesC-Free Standardsamples用指针 求一字符的长度 再main中输入在字符串,并输出长度.cpp:6: stray '' in program [Error] D:Program FilesC-Free Standardsamples用指针 求一字符的长度 再main中输入在字符串,并输出长度.cpp:6: parse error before `;' 构建中止 用指针 求一字符的长度 再main中输入在字符串,并输出长度: 2 个错
误, 0 个警告
实验后的源程序:
#include int length(char *s){
int i,n=0;
for(i=0;*(s+i)!=' ';i++)
n++;
return(n);
} void main(){
char *s;
char c[20];
s=c;
printf(“请输入字符串:n”);
gets(c);
printf(“ 字符串长度为%dn”,length(s));}
七、用函数的方法 由实参传来一个字符串,统计此字符中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述结果。实验前的源程序:
/*用函数的方法 由实参传来一个字符串,统计此字符中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述结果*/ #include int alphabet,number,space,others;void count(char str[]){
int i;
for(i=0;str[i]!=' ';i++)
if((str[i]>='a'&&str[i]='A'&&str[i]
alphabet++;
else if(str[i]>='0'&&str[i]
number++;
else if(str[i]==32)
space++;
else
others++;} void main(){
void count(char[]);
char str[80];
printf(“输入字符串:n”);
gets(str);
alphbet=0;
number=0;
space=0;
others=0;
count(str);
printf(“字母的个数= %dn数字的个数= %dn空格的个数= %dn其他字符个数= %dn”,alphabet,number,space,others);} 实验错误报告:
检查文件依赖性...正在编译 D:Program FilesC-Free Standardsamples用函数的方法 由实参传来一
个字符串,.cpp...[Error] D:Program FilesC-Free Standardsamples用函数的方法 由实参传来一个字符串,.cpp:25: `alphbet' undeclared(first use this function)[Error] D:Program FilesC-Free Standardsamples用函数的方法 由实参传来一个字符串,.cpp:25:(Each undeclared identifier is reported only once [Error] D:Program FilesC-Free Standardsamples用函数的方法 由实参传来一个字符串,.cpp:25: for each function it appears in.)构建中止 用函数的方法 由实参传来一个字符串,: 3 个错误, 0 个警告
实验后的源程序:
/*用函数的方法 由实参传来一个字符串,统计此字符中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述结果*/ #include int alphabet,number,space,others;void count(char str[]){
int i;
for(i=0;str[i]!=' ';i++)
if((str[i]>='a'&&str[i]='A'&&str[i]
alphabet++;
else if(str[i]>='0'&&str[i]
number++;
else if(str[i]==32)
space++;
else
others++;} void main(){
void count(char[]);
char str[80];
printf(“输入字符串:n”);
gets(str);
alphabet=0;
number=0;
space=0;
others=0;
count(str);
printf(“ 字母的个数=%dn 数字的个数=%dn 空格的个数=%dn 其他字符个数=%dn”,alphabet,number,space,others);}
八、用函数方法 输入一个3*3矩阵求其中元素的最大值 实验前的源程序:
/*用函数方法 输入一个3*3矩阵求其中元素的最大值*/ #include void main(){ int A[3][3];
int i,j;printf(“请输入一个3*3矩阵n”);for(i=0;i
for(j=0;j
{
scanf(“%d”,&A[i][j]);
} } int max(int array[][3]);printf(“max= %dn”,max(A));} int max(int array[][3]){ int i,j,max;max=array[0][0];for(i=0;i
for(j=0;j
if(array[i][j]>max)
max=array[i][j];
return(max);} 实验错误报告:
正在编译 D:Program FilesC-Free Standardtemp未命名4.cpp...[Error] D:Program FilesC-Free Standardtemp未命名4.cpp:23: parse error before `)' 构建中止 未命名4: 1 个错误, 0 个警告
实验后的源程序:
/*用函数方法 输入一个3*3矩阵求其中元素的最大值*/ #include void main(){ int A[3][3];
int i,j;printf(“请输入一个3*3矩阵n”);for(i=0;i
for(j=0;j
{
scanf(“%d”,&A[i][j]);
} } int max(int array[][3]);printf(“max= %dn”,max(A));} int max(int array[][3]){ int i,j,max;max=array[0][0];for(i=0;i
for(j=0;j
if(array[i][j]>max)
max=array[i][j];
return(max);}
实验报告Ⅰ指导教师曾宪华实验时间: 2010 年10月日 学院通信与信息工程专业通信类班级0101105学号2011210150姓名陈环宇 实验室s313实验目的及要求:(1)掌握选择结构程序设计,学会......
实验报告一(注意:红字不用抄写,其它必须抄写。实验纸不够用写反面。)一、实验内容if语句、ifelse语句、ifelse if语句的练习二、重点难点分析题目,正确应用相应的选择语句三、实......
《C语言程序设计》实验报告实验名称 ____ C程序的运行环境和运行C程序的方法_学期日期同组人李江涛指导老师_杨雪松___________成绩___________ -------------------------......
南昌大学实验报告学生姓名: 学 号: 专业班级:√ 综合 □ 设计 □ 创新 实验日期: 实验成绩: 实验类型:□ 验证 □一.实验名称实验3 控制语句二.实验目的1.熟练掌握if 、if…else、i......
郑州轻工业学院 实 践 报 告实现内容:OJ1123最佳校友(数组)、OJ1158又是升序(指针)、OJ1180成绩统计(结构)、OJ1203做幻方(文件)学号:541507020140 学生姓名:王红旭专业班级:电子信息科......