promotion image of download ymail app
Promoted

程式設計2-D Array問題

declare a 2D integer array data[3][5], and design the following functions:

1. init_array(int data[][5], int size)

data[i][j] = i*j;

2. print_array(int data[][5], int size)

print the array

3. array_double(int data[][5], int size)

double all of the elements

4. array_total(int data[][5], int size)

count and print the total of all elements of the array data[][]

The main program calls the following functions:

init_array()

print_array()

array_double()

print_array()

array_total()

Update:

謝謝你囉!!!

1 Answer

Rating
  • 8 years ago
    Favorite Answer

    呃,僅供參考,題目裡2維陣列是data[3][5],我不知道那個size是拿來做什麼用的

    public class Test0514{

    static void init_array(int data[][],int size){

    for (int i=0;i < 3;i++){

    for (int j=0;j < 5;j++){

    data[i][j] = i*j;

    }

    }

    }

    static void print_array(int data[][],int size){

    for (int i=0;i < 3;i++){

    for (int j=0;j < 5;j++){

    System.out.println("data["+i+"]["+j+"]="+data[i][j]);

    }

    }

    }

    static void array_double(int data[][],int size){

    for (int i=0;i < 3;i++){

    for (int j=0;j < 5;j++){

    data[i][j] = (int)Math.pow(data[i][j],2);

    }

    }

    }

    static void array_total(int data[][],int size){

    int total = 0;

    for (int i=0;i < 3;i++){

    for (int j=0;j < 5;j++){

    total += data[i][j];

    }

    }

    System.out.println("TOTAL:"+total);

    }

    public static void main(String[] args){

    int[][] data = new int[3][5];

    init_array(data,data.length);

    print_array(data,data.length);

    array_double(data,data.length);

    print_array(data,data.length);

    array_total(data,data.length);

    }

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.