开发者

Finding the max random number

I am instructed to find the max number generated from a 2D array: arr[10][10]. Is this code correct? To me its seems to work.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int maxArray(int arr[][10], int rcap, int ccap) {
    int max = arr[10][10]; srand(time(0));
    for (int r=0; r < rcap; 开发者_高级运维r++)
        for(int c=0; c < ccap; c++)
            if(arr[r][c] > max) max = (rand()%100)+100;

    return max;
}

int main() {
    int a[10][10];
    cout << maxArray (a,10,10) <<endl;
    return 0;
}


I think you were asked to create a random 2D array and then find the max:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int maxArray(int arr[][10], int rcap, int ccap ){
    int max = 0;
    for (int r=0; r < rcap; r++)
        for(int c=0; c < ccap; c++)
            if(arr[r][c] > max) max = arr[r][c];

    return max;
}

int main() {
    int a[10][10];
    srand(time(0));
    for (int r=0; r < 10; r++)
        for(int c=0; c < 10; c++)
            a[r][c] = (rand()%100); // make a random array
    cout << maxArray (a,10,10) <<endl;
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜