#import <Foundation/Foundation.h>
//定义一个整形数组array,有10个元素,给每个数组元素进行赋值([30,70]之间的随机数),并输出.
void demo1(){
int array[10];
for (int i =0 ; i < 10; i++) {
array[i] = arc4random() % (70 -30 + 1) +30;
printf("%d ", array[i]);
}
}
//编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排序,然后将这两个数组合并成一个升序数组。
void demo2(){
int array1[5];
int array2[5];
int array3[10];
printf("请输入 5个元素\n");
for (int i =0; i < 5; i++) {
scanf("%d",&array1[i]);
}
printf("请再次输入 5个元素\n");
for (int i =0; i < 5; i++) {
scanf("%d",&array2[i]);
}
for (int i =0; i < 4; i++) {
for (int j =0; j < 4 - i; j++) {
if (array1[j] > array1[j +1]) {
int temp = array1[j];
array1[j] = array1[j +1];
array1[j + 1] = temp;
}
if (array2[j] > array2[j +1]) {
int temp = array2[j];
array2[j] = array2[j +1];
array2[j + 1] = temp;
}
}
}
/*
for (int i = 0; i < 5;i++) {
printf("%d ", array1[i]);
}
printf("\n");
for (int i = 0; i < 5;i++) {
printf("%d ", array2[i]);
}
*/
for (int i =0; i < 10;i++) {
if (i <5) {
array3[i] = array1[i];
}else array3[i] = array2[i-5];
}
for (int i =0; i < 9; i++) {
for (int j =0; j < 9 - i; j++) {
if (array3[j] > array3[j +1]) {
int temp = array3[j];
array3[j] = array3[j +1];
array3[j + 1] = temp;
}
}
}
for (int i =0; i < 10;i++) {
printf("%d ", array3[i]);
}
}
//随机产生20个10~50的正整数存放到数组中,并求数组中的元素最大值、最小值、平均值及各元素之和。
void demo3(){
int array[20];
int max =0;
int min =51;
float ave =0;
float sum =0;
for (int i =0; i < 20; i++) {
array[i] = arc4random() % 41 +10;
max = max > array[i] ? max : array[i];
min = min < array[i] ? min : array[i];
sum += array[i];
}
ave = sum / 20;
// for (int i = 0; i < 20; i++) {
// printf("%d ", array[i]);
// }
printf("最大值是:%d 最小值是 :%d 平均值是:%.2f 和是:%.0f ", max,min,ave,sum);
}
//找出下列整型数组中最大和最小值及其所在位置i。
void demo4(){
int a[] = {5,-9,32,88,88,-24,14,0,21,45};
int b[] = {5,-9,32,88,88,-24,14,0,21,45};
int max =0;
int min =0;
for (int i =0; i < 9; i++) {
for (int j =0; j < 9 - i; j++) {
if (a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
// for (int i = 0; i < 10; i++) {
// printf("%d ", a[i]);
//
// }
max = a[sizeof(a)/sizeof(int)-1];//sizeof(a)/sizeof(int)字符数组的长度
min = a[0];
for (int i =0; i < 10; i++) {
if (b[i] == max) {
printf("最大值是:%d 位置是:第%d个 ", max,i+1);
}
if (b[i] == min) {
printf("最小值是:%d 位置是:第%d个", min,i+1);
}
}
/*
把str1, str2, str3合并到result数组中。
char result[50] = {0};
char str1[] = "Lanou ";
char str2[] = "23_class ";
char str3[] = " is niu best!";
结果:“Lanou 23_class is niu best!”
*/
void demo5(){
char str1[] ="Lanou ";
char str2[] ="23_class ";
char str3[] =" is niu best!";
char result[50] ={0} ;
strcat(result, str1);
strcat(result, str2);
strcat(result, str3);
// printf("%s",result);
for (int i =0; i < 50; i++) {
printf("%c", result[i]);
}
}
//下面这个程序执行后会有什么错误或者效果
void demo6(){
char string[10], str1[10];
int i;
for(i =0; i < 10; i++)
{
str1[i] = 'a';
}
strcpy(string, str1);
// printf("%s", string);
// for (int j =0; j < 10; j++) {
// printf("%d ", string[j]);
// }
//程序运行之后不会有任何提示 字符串以\0作为结束标志题目上赋值10个数值所以没有空间储存结束符 程序会一直执行占用内存
}
void demo7(){
unsignedchar str[10], i;
for(i =0; i < 256; i++)
str[i] = i;
//数组越界 i应该定义为int 类型的
}
int main(int argc,const char * argv[])
{
// demo1();
// demo2();
// demo3();
// demo4();
// demo5();
// demo6();
// demo7();
//20遍冒泡
/*
int i = 0;
printf("第%d遍\n",++i);
int array1[5];
for (int i = 0; i < 5; i++) {
array1[i] = arc4random() % (40 - 10 + 1) + 10;
}
for (int i = 0 ; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (array1[j] > array1[j + 1]) {
int temp = array1[j];
array1[j] = array1[j + 1];
array1[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%d ",array1[i]);
}
printf("\n第%d遍\n",++i);
int array2[6];
for (int i = 0; i < 6; i++) {
array2[i] = arc4random() % 41;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
if (array2[j] > array2[j + 1]) {
int temp = array2[j];
array2[j] = array2[j+1];
array2[j+1] = temp;
}
}
}
for (int i = 0; i < 6; i++) {
printf("%d ",array2[i]);
}
printf("\n第%d遍\n",++i);
int array3[9];
for (int i = 0; i < 9; i++) {
array3[i] = arc4random() % (80 - 10 + 1) + 10;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8 - i; j++) {
if (array3[j] > array3[j + 1]) {
int temp = array3[j];
array3[j] = array3[j + 1];
array3[j + 1] = temp;
}
}
}
for (int i = 0; i < 9; i++) {
printf("%d ", array3[i]);
}
printf("\n第%d遍\n",++i);
int array4[11];
for (int i = 0; i < 11; i++) {
array4[i] = arc4random() % 31 + 20;
}
for (int i = 0; i < 10; i++) {
for (int j = 0 ; j < 10 - i; j++) {
if (array4[j] > array4[j + 1]) {
int temp = array4[j];
array4[j] = array4[j + 1];
array4[j + 1] = temp;
}
}
}
for (int i = 0; i < 11; i++) {
printf("%d ", array4[i]);
}
printf("\n第%d遍\n",++i);
int array5[7];
for (int i = 0; i < 7; i++) {
array5[i] = arc4random() % 51 + 10;
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6 - i; j++) {
if (array5[j] > array5[j + 1]) {
int temp = array5[j + 1];
array5[j + 1] = array5[j];
array5[j] = temp;
}
}
}
for (int i = 0 ; i < 7; i++) {
printf("%d ",array5[i]);
}
printf("\n第%d遍\n",++i);
int array6[6];
for (int i = 0; i < 6; i++) {
array6[i] = arc4random() % 81 + 20;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i ; j++) {
if (array6[j] > array6[j + 1]) {
int temp = array6[j];
array6[j] = array6[j + 1];
array6[j + 1] = temp;
}
}
}
for (int i = 0; i < 6; i++) {
printf("%d ", array6[i]);
}
printf("\n第%d遍\n",++i);
int array7[7];
for (int i = 0; i < 7; i++) {
array7[i] = arc4random() % 71 + 10;
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6 - i; j++) {
if (array7[j] > array7[j + 1]) {
int temp = array7[j];
array7[j]= array7[j + 1];
array7[j + 1] = temp;
}
}
}
for (int i = 0; i < 7; i++) {
printf("%d ",array7[i]);
}
printf("\n第%d遍\n",++i);
int array8[8];
for (int i = 0; i < 8; i++) {
array8[i] = arc4random() % 81 + 10;
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7 - i; j++) {
if (array8[j] > array8[j + 1]) {
int temp = array8[j];
array8[j] = array8[j + 1];
array8[j + 1] = temp;
}
}
}
for (int i = 0; i < 8; i++) {
printf("%d ",array8[i]);
}
printf("\n第%d遍\n",++i);
int array9[9];
for (int i = 0; i < 9; i++) {
array9[i] = arc4random() % 91 + 10;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8 - i; j++) {
if (array9[j] > array9[j + 1]) {
int temp = array9[j];
array9[j] = array9[j + 1];
array9[j + 1] = temp;
}
}
}
for (int i = 0; i < 9; i++) {
printf("%d ",array9[i]);
}
printf("\n第%d遍\n",++i);
int array10[10];
for (int i = 0; i < 10; i++) {
array10[i] = arc4random() % 101 + 10;
}
for (int i = 0; i < 9 ; i++) {
for (int j = 0; j < 9 - i; j++) {
if (array10[j] > array10[j+1]) {
int temp = array10[j];
array10[j] =array10[j+1];
array10[j+1] = temp;
}
}
}
for (int i = 0; i < 10; i++) {
printf("%d ",array10[i]);
}
printf("\n第%d遍\n",++i);
int array11[11];
for (int i = 0; i < 11; i++) {
array11[i] = arc4random() % 101 + 10;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i ; j++) {
if (array11[j] > array11[j + 1]) {
int temp = array11[j];
array11[j] = array11[j + 1];
array11[j + 1] = temp;
}
}
}
for (int i = 0; i < 11; i++) {
printf("%d ", array11[i]);
}
printf("\n第%d遍\n",++i);
int array12[12];
for (int i = 0; i < 12; i++) {
array12[i] = arc4random() % 111 + 10;
}
for (int i = 0 ; i < 11; i++) {
for (int j = 0; j < 11 - i; j++) {
if (array12[j] > array12[j + 1]) {
int temp = array12[j];
array12[j] = array12[j + 1];
array12[j + 1] = temp;
}
}
}
for (int i = 0; i < 12; i++) {
printf("%d ", array12[i]);
}
printf("\n第%d遍\n",++i);
int array13[13];
for (i = 0; i < 13; i++) {
array13[i] = arc4random() % 121 +10;
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12 -i; j++) {
if (array13[j] > array13[j+1]) {
int temp = array13[j];
array13[j] = array13[j + 1];
array13[j + 1] = temp;
}
}
}
for (int i = 0; i < 13; i++) {
printf("%d ", array13[i]);
}
printf("\n第%d遍\n",++i);
int array14[14];
for (int i = 0; i < 14; i++) {
array14[i] = arc4random() % 131 + 10;
}
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13 - i; j++) {
if (array14[j] > array14[j + 1]) {
int temp = array14[j];
array14[j] = array14[j + 1];
array14[j + 1] = temp;
}
}
}
for (int i = 0; i < 14; i++) {
printf("%d ", array14[i]);
}
printf("\n第%d遍\n",++i);
int array15[15];
for (int i = 0; i < 15; i++) {
array15[i] = arc4random() %141 + 10;
}
for (int i = 0 ; i <14; i++) {
for (int j = 0; j < 14 - i; j++) {
if (array15[j] > array15[j+1]) {
int temp = array15[j];
array15[j]= array15[j+1];
array15[j+1] = temp;
}
}
}
for (int i = 0; i < 15;i++) {
printf("%d " , array15[i]);
}
printf("\n第%d遍\n",++i);
int array16[16];
for (int i = 0; i <16;i++) {
array16[i] = arc4random() % 151 + 10;
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15 - i; j++) {
if (array16[j]> array16[j + 1]) {
int temp = array16[j];
array16[j] = array16[j+1];
array16[j+1] = temp;
}
}
}
for (int i = 0; i < 16; i++) {
printf("%d " , array16[i]);
}
printf("\n第%d遍\n",++i);
int array17[17];
for (int i = 0; i < 17; i++) {
array17[i] = arc4random() % 161 + 10;
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16 - i ; j++) {
if (array17[j] > array17[j + 1]) {
int temp = array17[j];
array17[j] = array17[j + 1];
array17[j + 1] = temp;
}
}
}
for (int i = 0; i < 17; i++) {
printf("%d ", array17[i]);
}
printf("\n第%d遍\n",++i);
int array18[18];
for (int i = 0; i< 18; i++) {
array18[i] = arc4random() % 171 + 10;
}
for (int i = 0; i< 17; i++) {
for (int j = 0; j < 17 - i; j++) {
if (array18[j] > array18[j+1]) {
int temp = array18[j];
array18[j] = array18[j + 1];
array18[j+1] = temp;
}
}
}
for (int i = 0; i < 18; i++) {
printf("%d ", array18[i]);
}
printf("\n第%d遍\n",++i);
int array19[19];
for (int i= 0; i < 19; i++) {
array19[i] = arc4random() % 181 +10;
}
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 18 - i; j++) {
if (array19[j] > array19[j + 1]) {
int temp = array19[j];
array19[j] = array19[j + 1];
array19[ j + 1] = temp;
}
}
}
for (int i = 0; i < 19; i++) {
printf("%d ", array19[i]);
}
printf("\n第%d遍\n",++i);
int array20[20];
for (int i = 0; i < 20; i++) {
array20[i] = arc4random() % 191 +10;
}
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19 - i; j++) {
if (array20[j] > array20[j + 1]) {
int temp = array20[j];
array20[j] = array20[j + 1];
array20[j + 1] = temp;
}
}
}
for (int i = 0; i < 20; i++) {
printf("%d ", array20[i]);
}
*/
return0;
}
C语言-4
最新推荐文章于 2021-11-26 16:10:40 发布