2017-05-20 48 views
-2

私の実際の質問は、乱数の配列から最小値と最大値を見つける方法です、私はさまざまな論理を使って試してみましたが、 「NT reusltを取得し、任意のガイダンスはappreciateableだろう2次元配列から最小値と最大値を見つける

https://www.youtube.com/watch?v=yes0HMZWxUo&t=447s Put a multidimensional array into a one-dimensional array

#include<iostream> 
#include<cstdlib> 
#include<conio.h> 

void populateArray(); 
void displayArray(); 
void findMaxMinNumber(int[5][5]); 

main(){ 
    system("cls"); 
    int rows,columns = 5; 
    displayArray(); 
} 

void populateArray(){ 

    int randomNumber; 
    int minMax[5][5]; 

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

    for(int columnCount = 0; columnCount < 5; columnCount++){ 
     randomNumber = rand() % 100 + 1; 
     cout<<randomNumber; 
     minMax[rowCount][columnCount] = randomNumber; 

     if(randomNumber < 10){ 
      cout<<"  "; 
     } 
     else if(randomNumber >= 10){ 
      cout<<"  "; 
     } 
     } 
     cout<<endl; 
    } 
findMaxMinNumber(minMax); 
} 
void displayArray(){ 
    cout<<"Displaying array's data..."<<endl; 
    cout<<"------------------------------"<<endl; 
    populateArray(); 
} 
void findMaxMinNumber(int arr[5][5]){ 

    int rowMax = 0; 
    int colMax = 1; 
    int rowMin = 0; 
    int colMin = 1; 
    cout<<endl; 
    for(int i = 0; i < 5; i++){ 
     rowMax++; 
     rowMin++; 
    for(int j = 0; j < 5; j++){ 
     if(arr[i][j] < arr[rowMax][colMax]){ 
      //cout<<endl<<arr[i][j]; 
      rowMax = i; 
      colMax = j; 
     } 
     if(arr[i][j] > arr[rowMin][colMin]){ 
      rowMin = i; 
      colMin = j; 
     } 
     colMax++; 
     colMin++; 
    } 

} 
cout<<endl<<"The max is :"<<arr[rowMax][colMax]; 
cout<<endl<<"The min is :"<<arr[rowMin][colMin]; 
} 
+0

なぜこれらの4つの「インデックス」変数を​​増やしていますか?なぜ彼らはすべて0で始まるわけではないのですか?最後に、あなたのインデントを修正してください。 – LogicStuff

+0

これらの変数をインクリメントするロジックは、配列の各次のインデックス値と現在のインデックス値を一致させることで、最大値または最小値である – noman

+0

を見つけます。すでに 'i'および' j'でそれをしています。 – LogicStuff

答えて

0
void fillUpArray(int newArray[5][5]) 
{ 
    for (int i = 0; i < 5; ++i) 
    { 
     for (int j = 0; j < 5; ++j) 
     { 
      int randomNumber = rand() % 100 + 1; 
      printf("Random number[%d][%d]: %d\n", i, j, randomNumber); 
      newArray[i][j] = randomNumber; 
     } 
    } 
} 

void printMinimumMaximum(int myArray[5][5]) 
{ 
    int minimum = myArray[0][0]; 
    int maximum = myArray[0][0]; 

    for (int i = 0; i < 5; ++i) 
    { 
     for (int j = 0; j < 5; ++j) 
     { 
      if (myArray[i][j] < minimum) 
      { 
       minimum = myArray[i][j]; 
      } 
      if (myArray[i][j] > maximum) 
      { 
       maximum = myArray[i][j]; 
      } 
     } 
    } 

    printf("Minimum: %d\n", minimum); 
    printf("Maximum: %d\n", maximum); 
} 

int main() 
{ 
    int minMax[5][5]; 
    fillUpArray(minMax); 
    printMinimumMaximum(minMax); 
    return 0; 
} 
+0

私はそれを持って、それは働いた:) – noman

1
void findMaxMinNumber(int arr[5][5]){ 
    int* start = &arr[0][0]; 

    cout<<endl<<"The max is :" << *std::max_element(start, start + 5*5); 
    cout<<endl<<"The min is :" << *std::min_element(start, start + 5*5); 
} 
+0

プログラミングの初心者レベルですが、ポインタと私はstd :: beforeを使用していないし、私はそれの概念がありません。私はそれが配列を使用して見つけることを試みています – noman

+0

[minmax_element](http://en.cppreference.com/w/cpp/algorithm/minmax_element)、BTWもあります。 –

関連する問題