0

I have two dimensional array that represents country and its medals from certain competition.

Output:
                Gold    Silver    Bronze
Country1         1        0        1    
Country2         1        1        0    
Country3         0        0        1    
Country4         1        0        0    
Country5         0        1        1    
Country6         0        1        1    
Country7         1        1        0  

Let's imagine that all medal types represent specific number of points, for example Gold is 4 points, Silver is 2 points and Bronze is 1 point.

The thing I'm trying to achieve is that program is looking at all rows and columns and prints the number of points each country has.

Expected Output:
                Points
Country1         5    
Country2         6    
Country3         1    
Country4         4    
Country5         3    
Country6         3    
Country7         5 

Here is my code

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

const int COUNTRIES = 7;
const int MEDALS = 3;
void print_2D_array(int mas[][MEDALS], int r, int k);
void print_task(int mas[][MEDALS], int r, int k);

  int medal_counts[COUNTRIES][MEDALS] = {
  {1, 0, 1},
  {1, 1, 0},
  {0, 0, 1},
  {1, 0, 0},
  {0, 1, 1},
  {0, 1, 1},
  {1, 1, 0}};

int main(){
  int i;
  print_2D_array(medal_counts, COUNTRIES, MEDALS);
  print_task(medal_counts, COUNTRIES, MEDALS);
  return 0;
}

void print_2D_array(int mas[][MEDALS], int r, int k){
  for(int n = 0; n < r; n++){
    for(int m =0; m < k; m++){
      cout << setw(4) << mas[n][m];
    }
    cout << endl;
  }
}

void print_task(int mas[][MEDALS], int r, int k){
  cout << setw(20) << "Gold" << setw(10) << "Silver" << setw(10) << "Bronze" << endl;
  for (int i = 0 ; i < COUNTRIES; i++){
    cout << "Country" << i + 1 << "     "; 
    for (int j = 0 ; j < MEDALS; j++){
      cout << setw(5) << mas[i][j] << "    ";
      if(j == 2)
        cout << endl;
    }  
  }
}

I was trying to solve this problem for a long time and I tried using

  for(int i = 0; i < MEDALS; i++){
    for(int j = 0; j < COUNTRIES; j++){
      cout << "arr[" << j << "][" << i << "] ";
      cout << medal_counts[j][i] << endl;  
    }
  }
    
  cout << endl;

With the output of:

arr[0][0] 1
arr[1][0] 1
arr[2][0] 0
arr[3][0] 1
arr[4][0] 0
arr[5][0] 0
arr[6][0] 1
arr[0][1] 0
arr[1][1] 1
arr[2][1] 0
arr[3][1] 0
arr[4][1] 1
arr[5][1] 1
arr[6][1] 1
arr[0][2] 1
arr[1][2] 0
arr[2][2] 1
arr[3][2] 0
arr[4][2] 1
arr[5][2] 1
arr[6][2] 0

But I still didn't figure out how to solve this problem. Are there any ideas?

2
  • 2
    for example Gold is 4 points, Silver is 2 points and Bronze is 1 point. -- Where do you actually calculate the final value using these values? This is missing from your code. Pretend you didn't write the program, and someone handed you the rules of calculating the total points. How would you do it with pencil and paper? Commented Mar 27, 2022 at 17:32
  • points = arr[row][0] * 4 + arr[row][1] * 2 + arr[row][2] Commented Mar 27, 2022 at 17:34

1 Answer 1

1

As noted in comments, you actually need to calculate the points total and output it.

  for (int i = 0; i < COUNTRIES; i++){
    int total = arr[i][0] * 4 + arr[i][1] * 2 + arr[i][2];
    cout << total << endl; 
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.