calloc 数组计算机学院数据结构课程设计题 目:数组应用班 级:网络工程14104班姓 名:肖勇 学 号:0104同组人姓名:舒向东起 迄 日 期: 2014.12.28-2015.3.1课程设计地点: 湖南文理学院指导教师:邹汉斌评阅意见:成绩评定:评阅人:日期:完成日期:2014年12月目录1. 需求分析…………………………………………………………32. 概要设计(含流程图)…………………………………………33. 详细设计(含代码分析)………………………………………34. 调试分析和测试结果……………………………………………115. 总结………………………………………………………………126. 参考文献…………………………………………………………127. 致谢………………………………………………………………121. 需求分析该程序可以根据用户要求定义任意定义四维数组四个下标的最大值,用户可以根据需要输入一定数目的元素放入四维数组中,放入数组中元素的数目由用户自己决定,但不能大于数组的存储空间,用户如果想在数组中放入更多的元素,可以增大数组的存储空间。根据实验要求,从键盘输入的元素以行优先存入数组,以列优先显示出来。
2. 概要设计(含流程图)该程序包含两个函数,一个函数是把从键盘获得的元素以行优先顺序放入四维数组中,另一函数是把数组中的元素以列优先顺序输出到显示屏。Main函数中有四维数组的定义和赋值,还有以上两个函数的调用,实现了实验要求的功能。以下是本查询的实验流程图3. 详细设计(含代码分析)3.1、定义四维数组动态数组是指在声明时没有确定数组大小的数组,即忽略方括号中的下标;当要用它时,可随时用ReDim语句(C语言中用malloc语句)重新指出数组的大小。使用动态数组的优点是可以根据用户需要,有效利用存储空间。其原理以三维数组为例说明:先遵循从外层到里层,逐层申请的原则:最外层指针是array,它是个三维指针,所指向的是array[],其为二维指针。所以给array申请内存应:array=(int***)calloc(n1,sizeof(int**));次层指针是array[],它是个二维指针,所指向的是array[][],其为一维指针。所以给array[]申请内存应:for(i=0;i<n1;i++){array[i]=(int**)calloc(n2,sizeof(int*));}最内层指针是array[][],它是个一维指针,所指向的是array[][][],其是个整型常量。
所以给array[][]申请内存应:for(i=0;i<n1;i++){for(j=0;j<n2;j++){array[i][j]=(int*)calloc(n3,sizeof(int));}}当然,你可以把它们整合在一起为:int i,j,k;int n1,n2,n3;int ***array;scanf("%d%d%d",&n1,&n2,&n3);array=(int***)calloc(n1,sizeof(int**));for(i=0;i<n1;i++){array[i]=(int**)calloc(n2,sizeof(int*));for(j=0;j<n2;j++){array[i][j]=(int*)calloc(n3,sizeof(int));for(k=0;k<n3;k++){array[i][j][k]=i+j+k+1;}}}最后不要忘了释放这些内存,这要遵循释放的时候从里层往外层,逐层释放的原则。分析过程可参考上面的解答,这里不再赘述。只给出代码吧:for(i=0;i<n1;i++){for(j=0;j<n2;j++){free(array[i][j]);//释放第三维指针}}for(i=0;i<n1;i++){free(array[i]);//释放第二维指针}free(array);//释放第一维指针四维创建过程同此。
3.2、数组赋值以四个for循环将数组中的所有空间都存入最大值32767。3.3、将元素放入数组结合我们熟悉的二维数组的赋值,四维数组的赋值同理,先从低层开始,逐个走向高层,但是要放入数组中的元素个数并不是恰好等于数组的空间大小,所以我们设计了一记数的变量,该变量初始化为0,当你想要往数组中存放n个数时,该变量每循环一次加一,当该变量增加到n时不在要求从键盘输入元素。3.4、将元素从数组中输出四个for循环中各个递增的变量在数组下标中的排序恰好相反,因为输入是以行优先顺序的,而输出是以列优先顺序的。而每个元素在一维数组中的序号是与数组的四个下标有联系的,可以根据这个联系运算出来。3.5、代码分析#include<iostream.h>#include<stdlib.h>static int m,n,p,q,r;//m,n,p,q分别表示四个维度,r表示要输入的元素个数//将从键盘输入的元素存入四维数组中int creatarray(int ****array){int x;//变量x暂存从键盘输入的元素int i,j,k,l;int c=0;cout<<"请输入您想要存入四维数组中的元素(以行优先顺序存入值且小于32767):"<<endl;//以行优先顺序存储for(i=0;i<m;i++)for(j=0;j<n;j++)for(k=0;k<p;k++)for(l=0;l<q;l++){cin>>x;array[i][j][k][l]=x;//将从键盘获得的元素值放入四维数组中++c;if(c==r){return(0);//若已达到输入个数,结束并返回}}return(0);//函数有返回值}//将四维数组中的元素按列优先顺序输出void showarray(int ****array){int i,j,k,l;cout<<"以列优先顺序输出结果为:"<<endl;//列优先输出for(i=0;i<q;i++)for(j=0;j<p;j++)for(k=0;k<n;k++)for(l=0;l<m;l++){if(array[l][k][j][i]!=32767)//若该位置存有从键盘输入的值,则输出{cout<<"序号:"<<l*(n*p*q)+k*(p*q)+j*(q)+(i+1)<<" "<<"array["<<l<<"]["<<k<<"]["<<j<<"]["<<i<<"]="<<array[l][k][j][i]<<endl;}}}void main(){int i,j,k,l;cout<<"请输入您需要创建的四维数组的四个下标的最大值分别减1后的值:(m,n,p,q>=1)"<<endl;cin>>m>>n>>p>>q;//从键盘输入四个维数cout<<"请输入您想要存入四维数组中的元素的个数:(个数<="<<m*n*p*q<<")"<<endl;cin>>r;//从键盘获得元素个数//定义四维数组int ****array= (int****)malloc((m)*sizeof(int***));//最外层四维指针for(i=0;i<m;i++){array[i]= (int***)malloc((n)*sizeof(int**));//次外层三维指针for(j=0;j<n;j++){array[i][j]=(int**)malloc((p)*sizeof(int*));//次内层二维指针for(k=0;k<p;k++){array[i][j][k]=(int*)malloc((q)*sizeof(int));//最内层一维指针for(l=0;l<q;l++){array[i][j][k][l]=m+n+p+q+1;}}}}//检查是否分配内存成功if(!array){cout<<"内存分配失败!";}//将数组中全部元素赋值为32767,便于以列优先输出时检查for(i=0;i<m;i++)for(j=0;j<n;j++)for(k=0;k<p;k++)for(l=0;l<q;l++){array[i][j][k][l]=32767;}//行优先创建四维数组creatarray(array);//列优先输出四维数组showarray(array);//释放内存for(i=0;i<m;i++){for(j=0;j<n;j++){for(k=0;k<p;k++){free(array[i][j][k]);//释放四维指针}}}for(i=0;i<m;i++){for(j=0;j<n;j++){free(array[i][j]);//释放三维指针}}for(i=0;i<m;i++){free(array[i]);//释放二位指针}free(array);//释放一维指针}4. 调试分析和测试结果5. 总结(心得和体会)通过这次的实践,我学到了许多东西,首先,我们要在学习过程中要注重基础,认真学习课本上的知识;其次,在写程序的过程中要善于思考,在写之前要策划好怎么写,这样写的时候才能思路明确,写的时候不会不知所措;最后,要学会善于变通,勇于尝试,敢于创新。
同时细心也是程序能成功出来的必须条件。要多练习基础,才不会在细微的地方出错。calloc 数组6. 参考文献(1)严蔚敏,吴伟民·数据结构·(c语言版)·清华大学出版社,2007:90-115(2)严蔚敏,吴伟民·数据结构题集·(c语言版)·清华大学出版社,2004:31-45(3)谭浩强·c语言程序设计·(第二版)·清华大学出版社,2008:1-2997. 致谢在此,向给予我们细心指导的老师,给予我们帮助的同学,和积极配合的组友致谢。their own conditions to develop the correct road, the maximum to avoid investment risk, gain profit.(three) vigorously promote the brand. To establish brand awareness, awareness of the use of brand, brand value, brand acquisition performance, enhance the competitive strength. Concentrated manpower, careful planning, packaging and publicity of a number of unique, market influence and coverage of the brand, the implementation of key breakthroughs, to enhance the competitive strength, walking business road the competition of alienation and characteristics, the pursuit of stability and development of the market.(four) to promote the integration of resources. To further broaden their horizons, effective integration of resources within the group, the city resources, other industries and regional resources, mutual trust, mutual benefit, seeking win-win principle, in the framework of national policies and regulations, strict inspection and argumentation, legal consultation, examination and approval procedures, strict regulation of economic activities, attract injection the social investment to the industry group, to achieve leveraging the development, ensure that the value of state-owned assets.(five) to strengthen the construction management personnel. Strengthen the management of education and training of cadres and workers of the existing business, firmly establish the concept of the market, enhance the sense of crisis to adapt to market competition, the sense of urgency, improve the ability to respond to market competition, improve management and operation of the market. At the same time, according to the need of industrial development, vigorously the introduction of high-quality management management personnel, and strive to build a high-quality professional management team, hard work, and promote the entire workforce knowledge structure, age structure, structure optimization and upgrading ability, enhance core competitiveness, adapt to the need of market competition.(six) seriously study the policy for policy. Serious research about social support the development of cultural undertakings in the country and the XX policy, especially the policy of industrial development, financial investment policy, financial policy and tax policy, and actively seek policy, projects and funds, enterprise and industry group mission to promote leapfrog development.their own conditions to develop the correct road, the maximum to avoid investment risk, gain profit.(three) vigorously promote the brand. To establish brand awareness, awareness of the use of brand, brand value, brand acquisition performance, enhance the competitive strength. Concentrated manpower, careful planning, packaging and publicity of a number of unique, market influence and coverage of the brand, the implementation of key breakthroughs, to enhance the competitive strength, walking business road the competition of alienation and characteristics, the pursuit of stability and development of the market.(four) to promote the integration of resources. To further broaden their horizons, effective integration of resources within the group, the city resources, other industries and regional resources, mutual trust, mutual benefit, seeking win-win principle, in the framework of national policies and regulations, strict inspection and argumentation, legal consultation, examination and approval procedures, strict regulation of economic activities, attract injection the social investment to the industry group, to achieve leveraging the development, ensure that the value of state-owned assets.(five) to strengthen the construction management personnel. Strengthen the management of education and training of cadres and workers of the existing business, firmly establish the concept of the market, enhance the sense of crisis to adapt to market competition, the sense of urgency, improve the ability to respond to market competition, improve management and operation of the market. At the same time, according to the need of industrial development, vigorously the introduction of high-quality management management personnel, and strive to build a high-quality professional management team, hard work, and promote the entire workforce knowledge structure, age structure, structure optimization and upgrading ability, enhance core competitiveness, adapt to the need of market competition.(six) seriously study the policy for policy. Serious research about social support the development of cultural undertakings in the country and the XX policy, especially the policy of industrial development, financial investment policy, financial policy and tax policy, and actively seek policy, projects and funds, enterprise and industry group mission to promote leapfrog development.their own conditions to develop the correct road, the maximum to avoid investment risk, gain profit.(three) vigorously promote the brand. To establish brand awareness, awareness of the use of brand, brand value, brand acquisition performance, enhance the competitive strength. Concentrated manpower, careful planning, packaging and publicity of a number of unique, market influence and coverage of the brand, the implementation of key breakthroughs, to enhance the competitive strength, walking business road the competition of alienation and characteristics, the pursuit of stability and development of the market.(four) to promote the integration of resources. To further broaden their horizons, effective integration of resources within the group, the city resources, other industries and regional resources, mutual trust, mutual benefit, seeking win-win principle, in the framework of national policies and regulations, strict inspection and argumentation, legal consultation, examination and approval procedures, strict regulation of economic activities, attract injection the social investment to the industry group, to achieve leveraging the development, ensure that the value of state-owned assets.(five) to strengthen the construction management personnel. Strengthen the management of education and training of cadres and workers of the existing business, firmly establish the concept of the market, enhance the sense of crisis to adapt to market competition, the sense of urgency, improve the ability to respond to market competition, improve management and operation of the market. At the same time, according to the need of industrial development, vigorously the introduction of high-quality management management personnel, and strive to build a high-quality professional management team, hard work, and promote the entire workforce knowledge structure, age structure, structure optimization and upgrading ability, enhance core competitiveness, adapt to the need of market competition.(six) seriously study the policy for policy. Serious research about social support the development of cultural undertakings in the country and the XX policy, especially the policy of industrial development, financial investment policy, financial policy and tax policy, and actively seek policy, projects and funds, enterprise and industry group mission to promote leapfrog development.四维数组将数组中的元素以列优先书序显示到显示屏从键盘获取元素以行优先放入四维数组中从键盘输入元素以列优先顺序显示四维数组中的元素
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-30656-1.html
我是导弹手
没有酒味也不像饮料