Trang Chủ   Vn+ TiVi   Fanpage Twitter  Fanpage FaceBook
Loading
                                         

Bản Tin Câu Lạc Bộ Pa Tin Im-Pod                                                                          
Diễn đàn Vn+
Tiêu Điểm

Tình Yêu - Giới Tính

1001 Bí Ẩn

Tổng hợp phần mềm

Ebook

Thủ Thuật IT

Thủ Thuật Blog

Video Clip

Lạ & Fun


You are not connected. Please login or register

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

1MA PHƯƠNG c++ Empty MA PHƯƠNG c++ Thu Aug 18, 2011 9:07 pm

El Kun

El Kun
Super Moderator
Super Moderator
MA PHƯƠNG

Code:
#include <stdio.h>
#include <conio.h>

// func declaration
void matrix( int n );

// main()
int main(void)
{
    int n;
 
    // input until it's valid.
    do
    {
    printf("\n Plz input size of matrix [ odd size & n < 20 ]: n = ");
    scanf("%d",&n);
    if ( n % 2 == 0 ) printf("\n Invalid input value .. Plz re-input ... \n");
    }
    while ( n % 2 == 0 );
 
    if ( n > 20 ) { n = 19 ; // in case of n is greater than 20
    printf("\n %d is greater than 20 & set to be default as 19 .",n ); } // end if
 
    // call matrix()
    matrix(n);
    // stop to watch
    getch();
    return 0;
}

// function matrix(int n)
void matrix( int n )
{
    int a[20][20];
    int i, j, row, col, count = 1;
    int old_row, old_col, sum = 0;
   
    // set starting value of array
    for ( i = 0 ; i < n ; i++ )
    for ( j = 0 ; j < n ; j++ )
        a[i][j] = 0;
   
    // set the 1st value to start
    row = 0; col = (n-1) / 2;
   
    while ( count < n*n + 1 )
    {
          a[row][col] = count++ ; // set value for elements
          old_row = row ; old_col = col; // save the last addresses
          // define whether going out of array
          row -= 1; if ( row == -1 ) row = n - 1;
          col += 1; if ( col == n ) col = 0;
          // in case of already having number
          if ( a[row][col] != 0 )
          {
                row = old_row + 1;
                col = old_col;
          } // end if
    } // end while
    // print result
    printf("\n");
    for ( i = 0 ; i < n ; i++ )
    {
    for ( j = 0 ; j < n ; j++ )
        printf("%4d",a[i][j]);
    printf("\n");
    } // end for
   
    // calculate sum
    for ( j = 0 ; j < n ; j++ )
        sum += a[0][j];
    printf("\n Sum of each row - column - diagonal line is : %d " , sum);
   
    return;
}

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết

 

Legend :  [ Vn-plus Founder ] [ Administrator ] [ Super Moderator ] [ Moderator ] [ Member ] [ VIP member ] [ banned ]



Free Auto Backlink Exchange ServiceFree Backlink Exchange For SeoVietnam BacklinksTravel BacklinksFree BacklinksText Backlink ExchangesText Back Link ExchangeFlorists LinksOverShopping Link ExchangeFree Automatic LinkWeb Link Exchange - Linkcsere

    Diễn Đàn Vn-Plus.Org | Thống kê | Liên hệ | Báo cáo lạm dụng  | Bạn muốn quảng cáo | Lên Đầu Trang

    Powered by: PHPBB2. Copyright © 2011  Diễn Đàn Mở VN+
    Contact: Vn-plus Founder . Yahoo: Nobita_xuka_ccy. Địa chỉ: Từ Tây - Yên Phú - Yên Mỹ - Hưng Yên
    BQT không chịu trách nhiệm bất cứ nội dung nào của thành viên đăng tải

Liên Kết Vớivn+

Free forum | ©phpBB | Free forum support | Báo cáo lạm dụng | Thảo luận mới nhất