您的位置:首页 > 编程语言 > C语言/C++

C/C++与Java多维数组,遍历与最大值获取方法!

2010-04-29 14:43 639 查看
C/C++

/*
* File:   main.cpp
* Author: Vicky
*
* Created on 2010年4月29日, 上午9:46
*/

#include <iostream>
using namespace std;

int maximum(int[], int);

int main(int argc, char** argv) {
// int sg[3][4] = {
int sg[][4] = {
{68, 77, 73, 86},
{87, 96, 78, 89},
{90, 70, 81, 86}
};

int row = (sizeof (sg) / sizeof (int)) / (sizeof (sg[0]) / sizeof (int)); // 3行
int row2 = sizeof (sg[0]) / sizeof (int); // 4列
cout << row << endl; // 3
cout << row2 << endl; // 3

int max = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < row2; j++) {
cout << sg[i][j] << "/t";
if(max < sg[i][j])
max = sg[i][j];
}
cout<<endl;
}
cout<<"the max grade is : "<<max<<endl;

cout << "the max grade is : "
<< maximum(&sg[0][0], 3 * 4) //传递第一个元素地址和元素个数
<< endl;
return (EXIT_SUCCESS);
}

int maximum(int grade[], int num) {
int max = 0;
for (int i = 0; i < num; i++)
if (grade[i] > max)
max = grade[i];

return max;
}


输出:

3
4
68 77 73 86
87 96 78 89
90 70 81 86
the max grade is : 96
the max grade is : 96

运行成功(总计时间: 329毫秒)

public class MyTest {
public static void main(String[] args) {

int[][] is = new int[][]{
{68, 77, 73, 86},
{87, 96, 78, 89},
{90, 70, 81, 86}
};
int row1 = is.length; // 3行
int row2 = is[0].length; // 4列
System.out.println(row1);
System.out.println(row2);
int max = 0;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < row2; j++) {
System.out.print(is[i][j] + "/t");
if (max < is[i][j])
max = is[i][j];
}
System.out.println();
}
System.out.println("最大值 : " + max);
}
}


3
4
68 77 73 86
87 96 78 89
90 70 81 86
最大值 : 96

以上的遍历方式,存在缺陷,应当修改为:

/*
* File:   main.cpp
* Author: Vicky
*
* Created on 2010年4月29日, 上午9:46
*/

#include <iostream>
using namespace std;

int maximum(int[], int);

int main(int argc, char** argv) {
// int sg[3][4] = {
//  int sg[][] = { 错误的!!!
int sg[][4] = {
{68, 77, 73, 86},
{87, 96, 78},
{90, 70}
};

int row = (sizeof (sg) / sizeof (int)) / (sizeof (sg[0]) / sizeof (int)); // 3行
// int row2 = sizeof (sg[0]) / sizeof (int); // 4列
cout << row << endl; // 3
// cout << row2 << endl; // 3

int max = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < sizeof (sg[i])/sizeof(int); j++) {
cout << sg[i][j] << "/t";
if(max < sg[i][j])
max = sg[i][j];
}
cout<<endl;
}
cout<<"the max grade is : "<<max<<endl;

cout << "the max grade is : "
<< maximum(&sg[0][0], 3 * 4) //传递第一个元素地址和元素个数
<< endl;
return (EXIT_SUCCESS);
}

int maximum(int grade[], int num) {
int max = 0;
for (int i = 0; i < num; i++)
if (grade[i] > max)
max = grade[i];

return max;
}




package com.lesogo.unit;

public class MyTest {

public static void main(String[] args) {		int[][] is = new int[][]{
{68, 77, 73, 86},
{87, 96, 78},
{90, 70}
};
int row1 = is.length; // 3行
System.out.println(row1);
int max = 0;
for (int i = 0; i < row1; i++) {
int len = is[i].length;
for (int j = 0; j < len; j++) {
System.out.print(is[i][j] + "/t");
if (max < is[i][j])
max = is[i][j];
}
System.out.println();
}
System.out.println("最大值 : " + max);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: