zoukankan      html  css  js  c++  java
  • C语言-实现矩阵的转置-随机函数产生随机数并赋予数组中-190222

    //编写程序,实现矩阵的转置(行列互换)。

     1 #include <stdio.h>
     2 #include <conio.h>
     3 #include <stdlib.h>
     4 
     5 void fun (int array[3][3])
     6 {
     7     int i,j,t;
     8     for (i = 0; i < 3; i++)
     9     {
    10         for (j = 0; j < 3; j++)
    11         {
    12             if (j >= i)//控制交换的数。
    13             {
    14                 t = array[i][j];
    15                 array[i][j] = array[j][i];
    16                 array[j][i] = t;
    17             }
    18         }
    19     }
    20 }
    21 void main()
    22 {
    23   FILE *wf;
    24   int i,j;
    25   int array [3][3]={{100,200,300},{400,500,600},{700,800,900}};
    26   system("CLS");//清屏
    27   for (i=0;i<3;i++)
    28      {for (j=0;j<3;j++)
    29           printf("%7d ",array[i][j]);
    30       printf("
     ");
    31      }
    32   fun(array);//不是值传递,可以双向传递。
    33   printf("Converted array:
     ");
    34   for (i=0;i<3;i++)
    35      { for (j=0;j<3;j++)
    36           printf("%7d ",array[i][j]);
    37        printf("
     ");
    38      }   
    39 /******************************/
    40   wf=fopen("out.dat","w");//对文件读操作
    41   for (i=0;i<3;i++)
    42      { for (j=0;j<3;j++)
    43           fprintf(wf,"%7d ",array[i][j]);
    44        fprintf(wf,"
    ");
    45      }   
    46   fclose(wf);
    47 /*****************************/
    48 }

    //函数fun功能是:调用随机函数产生20个互不相同的整数放在形参a所指向的数组中。

     1 #include  <stdlib.h>
     2 #include  <stdio.h>
     3 #define   N  20
     4 void  fun( int  *a)
     5 { int  i, x, n=0;
     6   x=rand()%20;
     7 /**********found**********/
     8   while (n<N)
     9   {  for(i=0; i<n; i++ )
    10 /**********found**********/
    11          if( x==a[i] ) 
    12             break;//判断是否重复。
    13 /**********found**********/
    14      if( i==n)
    15         { a[n]=x; n++; }//赋值
    16      x=rand()%20;
    17   }
    18 }
    19 void main()
    20 { int  x[N]={0} ,i;
    21   fun( x );
    22   printf("The result :  
    ");
    23   for( i=0; i<N; i++ )
    24   { printf("%4d",x[i]);
    25     if((i+1)%5==0)printf("
    ");//一行5个元素。
    26   }
    27   printf("
    
    ");
    28 }
  • 相关阅读:
    Oracle启动关闭
    Open_stack 有虚拟机端口不通的问题
    关于Oracle归档的一些操作
    电脑无法开机 接通电源后主板有红灯闪烁的问题
    Centos7+python3.6+face-recognition
    电脑无法开机的问题-主板上有红色告警灯闪烁
    关于systemctl
    Vsftp搭建 for centos7
    外星人入侵——安装Pygame
    mysql索引原理详解
  • 原文地址:https://www.cnblogs.com/ming-4/p/10416947.html
Copyright © 2011-2022 走看看