C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

楼主: 卤水梦

如何改变数组的长度 [复制链接]

Rank: 3Rank: 3

主题
9
帖子
210
C币
225 枚
在线时间
115 小时
发表于 2011-2-11 23:07:49 |显示全部楼层
分享到:
root@yeah ~
$ cat 1.c
  1. #include <stdio.h>
  2. //ANSI C99
  3. //This Program Compiled Sucess by gcc 3.4.4
  4. //Operating System Platform is Cygwin 1.7.7-1

  5. int main (void) {

  6.         int n,i,m;

  7. //Get length of array

  8.         printf ("Enter length of array:");
  9.         scanf ("%i",&n);

  10. //Creat array

  11.         int array[n];

  12.         for(i=0,m=1;i<n;i++,m++) {
  13.                 array[i]=m;
  14.         }

  15. //display array

  16.         for(i=0;i<n;i++){
  17.                 printf ("%i ",array[i]);
  18.         }

  19.         return 0;

  20. }
复制代码
test:
root@yeah ~
$ ./1
Enter length of array:20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
root@yeah ~
$ ./1
Enter length of array:6
1 2 3 4 5 6
root@yeah ~
$
学习环境: 操作平台:Cygwin 编译器:Gcc 使用语言:ANSI C99
学习进度: Pointers

Rank: 3Rank: 3

主题
9
帖子
210
C币
225 枚
在线时间
115 小时
发表于 2011-2-11 23:08:37 |显示全部楼层
C99支持变长数组
creativewang 发表于 2010-8-9 18:05



    嗯, 果然是这样。

Rank: 3Rank: 3

主题
9
帖子
210
C币
225 枚
在线时间
115 小时
发表于 2011-2-11 23:50:14 |显示全部楼层
作为一个练习,还望大家指点。
  1. #include <stdio.h>

  2. int i,len;

  3. int main (void) {
  4.                        
  5.         void sort (int a[],int n,int m);
  6.         void disp (int a[]);

  7.         printf ("Enter length of array:\n");
  8.         scanf ("%i",&len);

  9.         int a[len];

  10.         printf ("Enter element in array:\n");

  11.         for(i=0;i<len;i++) {

  12.                 scanf ("%i",&a[i]);

  13.         }

  14.         printf ("The array before the sort:\n");

  15.         disp(a);
  16.         printf ("\nAfter sorted...\n");

  17.         printf ("Ascending sort\n");
  18.         sort(a,len,1);
  19.         disp(a);

  20.         printf ("\nDescending sort\n");
  21.         sort(a,len,0);
  22.         disp(a);

  23.         return 0;
  24. }
  25.        
  26. //

  27. void sort (int a[],int n,int d) {
  28.        
  29.         int i,j,temp;

  30.         if (d==1) {

  31.                 for(i=0;i<len;i++) {               

  32.                         for(j=i+1;j<len;j++) {

  33.                                 if(a[i]>a[j]) {

  34.                                         temp=a[i];

  35.                                         a[i]=a[j];

  36.                                         a[j]=temp;

  37.                                 }

  38.                         }

  39.                 }


  40.         }

  41.         if (d==0) {
  42.        
  43.                 for(i=0;i<len;i++) {

  44.                         for(j=i+1;j<len;j++) {

  45.                                 if(a[i]<a[j]) {

  46.                                         temp=a[j];

  47.                                         a[j]=a[i];

  48.                                         a[i]=temp;

  49.                                 }

  50.                         }

  51.                 }

  52.         }
  53.        
  54. }

  55. //
  56. void disp (int a[]) {

  57.         int i;

  58.         for(i=0;i<len;i++) {
  59.        
  60.                 printf ("%i ",a[i]);
  61.        
  62.         }

  63. }
复制代码
测试:

root@yeah ~
$ gcc _sort.c -o _sort

root@yeah ~
$ ./_sort
Enter length of array:
10
Enter element in array:
2
32
123
-9
234
21
9
0
7
8
The array before the sort:
2 32 123 -9 234 21 9 0 7 8
After sorted...
Ascending sort
-9 0 2 7 8 9 21 32 123 234
Descending sort
234 123 32 21 9 8 7 2 0 -9
root@yeah ~
$ ./_sort
Enter length of array:
5
Enter element in array:
32
1
-98
0
-89
The array before the sort:
32 1 -98 0 -89
After sorted...
Ascending sort
-98 -89 0 1 32
Descending sort
32 1 0 -89 -98
root@yeah ~
$
学习环境: 操作平台:Cygwin 编译器:Gcc 使用语言:ANSI C99
学习进度: Pointers
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-5-20 18:25

©2009-2011 cyuyan.com.cn

回顶部