2016-04-05 17 views
0

Jungs können mir helfen, diesen Code zu tun, weil ich 6 Stunden verwendet habe und immer noch nicht die Antwort bekommen.Um zweidimensionales Array zu erstellen

Die Frage ist ", ein Programm schreiben, das eine zweidimensionale Anordnung entsteht (6 * 6), besiedeln das Array mit Zufallszahlen zwischen 1 und 100 a: Erhalten der Summe aller Zahlen b: Erhalten der durchschnittlichen aller Zahlen c: die Zeilensumme d Bestimmen Sie: die höchste in der Reihe e ermitteln. Bestimmen Sie die niedrigste in der Reihe

Hier ist mein Code

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

// Main Function 
int main() 
{ 
    //Initialize Variables 
    int table1[6][6]; 
    int highest ; 
    int lowest ; 
    double sumRow = 0; 

    int row = 0; 
    int col = 0; 

    //PrintArray 
    for (row = 0; row < 6; row++) 
    { 
     for (col = 0; col < 6; col++) 
     { 
      { 
       table1[row][col] = rand() % 100 + 1; 
      } 
      cout << table1[row][col] << "\t"; 
     } 
     cout << "" << endl << endl; 
    } 

    //Highest & lowest value in the row 
    for(row = 0; row <6; row ++) 
    {  
     highest = table1[row][0]; 
     lowest = table1[row][0]; 

     for (col = 0; col < 6; col++) 
     { 
      if (highest < table1[row][col]) 
       highest = table1[row][col]; 
      if (lowest > table1[row][col]) 
       lowest = table1[row][col]; 
      sumRow = sumRow + table1[row][col]; 

     } 
     cout <<" Row" << row <<" highest value :" << highest <<endl; 
     cout <<" Row" << row <<" lowest value :" << lowest << endl; 
     cout <<" Row" << row <<" average value :" << sumRow/6 <<endl; 
     cout <<" Row" << row <<" Total value :" << sumRow << endl; 
     sumRow = 0; 

     cout << endl; 
    } 
} 
+0

_ "[Ich] bekomme immer noch nicht die Antwort" _ ist keine besonders gute Problembeschreibung. Mit was genau hast du Probleme? – Michael

+0

@ItsGreg 'row = 0' und' col = 0' in Loops dabei, denke ich –

+0

Bitte [editieren] Sie Ihre Frage, um eine [mcve] und eine klare Problemaussage aufzunehmen. –

Antwort

1

Nachfolgend finden Sie eine mögliche Lösung. Bitte verwenden Sie dies als Referenz um festzustellen Probleme deines Codes.

#include <algorithm> 
#include <iostream> 
#include <ctime> 
#include <iomanip> 

using namespace std; 

int main(int argc, char* argv[]) 
{ 
    int table[6][6]; 
    int row_total[6]; 
    int row_max[6]; 
    int row_min[6]; 
    int total_total = 0; 

    srand(time(NULL)); 

    for(int row = 0 ; row < 6 ; row++) { 
    row_total[row] = 0; 
    row_max[row] = numeric_limits<int>::min(); 
    row_min[row] = numeric_limits<int>::max(); 
    for(int col = 0 ; col < 6 ; col++) { 
     table[row][col] = rand() % 100 + 1; 
     row_total[row] += table[row][col]; 
     row_max[row] = max(table[row][col],row_max[row]); 
     row_min[row] = min(table[row][col],row_min[row]); 
    } 
    total_total += row_total[row]; 
    } 

    for(int row = 0 ; row < 6 ; row++){ 
    for(int col = 0 ; col < 6 ; col++){ 
     cout << std::right << std::setw(4) << table[row][col]; 
    } 
    cout << "\tTotal: " << std::right << std::setw(4) << row_total[row]; 
    cout << "\tMax: " << std::right << std::setw(4) << row_max[row]; 
    cout << "\tMin: " << std::right << std::setw(4) << row_min[row] <<  std::endl; 
    } 

    cout << "Total of all numbers: " << total_total << std::endl; 
    cout << "Average of all numbers: " 
    << setiosflags(ios::fixed | ios::showpoint) 
    << setprecision(2) 
    << total_total/36.0 << std::endl; 
} 
Verwandte Themen