C語言程式設計 練習題參考答案 第四章 (2) 二維數組

來源:互聯網
上載者:User

 /*  4.16  5*5矩陣中每行的絕對值最大值,與同行對角線交換*/

#include "stdio.h"

#include "math.h"
void main()
{
   int a[5][5]={{1,2,3,4,-5},{3,5,-2,4,2},{4,1,2,3,-2},
                {1,3,-2,4,6},{2,2,0,7,4}} ;
   int i,k,max,sub,temp;
   /* i 迴圈變數,控制行, k 迴圈變數,控制列,max 當前最大絕對值,sub 當前最大絕對值元素的下標
    temp 臨時用於交換的變數 */

    printf("交換之前,輸出\n");  /*交換之前,輸出*/
   for(i=0;i<=4;i++)
   {
      for(k=0;k<=4;k++)
            printf("%4d",a[i][k]);
      printf("\n");
   }
   /*交換*/
   for(i=0;i<=4;i++)
   {
      /*假設第一個元素最大*/
      max=fabs(a[i][0]);  sub=0;
      /*尋找絕對值最大的元素記下下標*/
      for(k=1;k<=4;k++)
      {
       if(fabs(a[i][k])>max)
         {
           max=fabs(a[i][k]);  sub=k;
         }
      }
      /*交換*/
      temp=a[i][i];  a[i][i]=a[i][sub];  a[i][sub]=temp;
   }
   /*交換之後,輸出*/
   printf("交換之後,輸出\n");
   for(i=0;i<=4;i++)
   {
      for(k=0;k<=4;k++)
           printf("%4d",a[i][k]);
      printf("\n");
   }

}

/*  4.17 在一個一維數組中存放任意4個數,如:5,1,8,6,產生如下矩陣
 5 5 5 5 5 5 5
 5 1 1 1 1 1 5
 5 1 8 8 8 1 5
 5 1 8 6 8 1 5
 5 1 8 8 8 1 5
 5 1 1 1 1 1 5
 5 5 5 5 5 5 5
*/
#include "stdio.h"
#include "conio.h"
void main()
{
    int FourNumbers[4], array[7][7], i , row, column;
    printf("請輸入4個整數\n");
    scanf("%d%d%d%d",&FourNumbers[0],&FourNumbers[1],&FourNumbers[2],&FourNumbers[3]);
    for(i=0;i<=3;i++)
    {
        for(row=i;row<=6-i;row++)
        {
           for(column=i;column<=6-i;column++)
               array[row][column]=FourNumbers[i];
        }
    }
    /* 輸出矩陣 */
    for(row=0;row<=6;row++)
    {
        for(column=0;column<=6;column++)
            printf("%4d",array[row][column]);
        printf("\n");
    }
    getch();
}
/* 習題4.19 對一行電文加密,每個字母轉換為字母表中迴圈右移的第三個字母, a-d, b-e, ......z-c */

#include "stdio.h"
void main()
{

    int c;
    while((c=getchar())!='\n')
    {
        if( (c>='a' && c<='z') || (c>='A' && c<='Z') )
        {
          c=c+3;
          if ((c>'Z' && c<='Z'+3) || c>'z')
            c=c-26;

        }
        putchar(c);

    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.