urgently need your help. ;( (array)

hi. ;)

how to do this?
-
ask the user to input entries of 10x10 array. sort each column into increasing order. print out the array with sorted columns? thanks! ;)))
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;

#define MAT_DIM 2

class buble_sort
{
  public:
    void fill_arr();
    void print_arr();
    void sort_arr();
  private:
    int arr[MAT_DIM][MAT_DIM];
};

void buble_sort::fill_arr()
{
  for(int i = 0; i < MAT_DIM; i++)  
    for(int j = 0; j < MAT_DIM; j++)
    {
      cout << "Enter value for element[" << i << "][" << j << "]:";
      cin >> arr[i][j];
    }
}

void buble_sort::print_arr()
{
  for(int i = 0; i < MAT_DIM; i++)
  {
    for(int j = 0; j < MAT_DIM; j++)
      cout << " " << arr[i][j];
    cout << endl;
  }
}

void buble_sort::sort_arr()
{
  int temp;
  for(int col = 0; col < MAT_DIM; col++)
    for(int i = 0; i < MAT_DIM - 1; i++)
    {
      int continue_sort = 0;
      for(int j = 0; j < MAT_DIM - i - 1; j++)
      {
        if(arr[j][col] > arr[j+1][col])
        {
          temp = arr[j][col];
          arr[j][col] = arr[j+1][col];
          arr[j+1][col] = temp;
          continue_sort++;
        }
      }
      if(!continue_sort)
        break;
    }
}

int main()
{
  buble_sort bs_obj;
  bs_obj.fill_arr();
  cout << "Unsorted Matrix:\n";
  bs_obj.print_arr();
  bs_obj.sort_arr();
  cout << "Sorted Matrix:\n";
  bs_obj.print_arr();
}
Last edited on
omg. thank you!!! gonna run this. ;))) I have another problem...
ask the user to enter the names and population of 3 cities in metro manila. find the average of all the population that have been entered and print the names of all cities that have their population larger than the computed average. (here's the other problem. thank you in advanced. ;))) )
why don't you have go first? at least an attempt.. Before copying and pasting someone else's code and learning nothing from the process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class cities 
{
  public:
    void get_cities();
    void print_cities();
    void print_cities_abv_avg_pop();
    
  private:
    typedef struct
    {
      string name;
      unsigned int population;
    }a_city;    

    vector<a_city> cities_v;
    unsigned int avg_population;
};

void cities::get_cities()
{
  a_city city;
  string str;
  int no_of_cities = 0, cum_population = 0;
  
  // Get the city names and its population from the user.
  while(1)
  {
    cout << "Enter city name. Enter an empty line to finish. ";
    getline(cin, str);
    if(str.empty())
      break;
    city.name = str;
    cout << "Enter city population: ";
    cin >> city.population;
    cin.ignore(); // Flush out the newline from the above cin call.
    cities_v.push_back(city);
    no_of_cities++;
    cum_population += city.population;
  }
  avg_population = cum_population / no_of_cities;
}

void cities::print_cities()
{
  for(vector<a_city>::iterator it = cities_v.begin() ; it != cities_v.end(); ++it)
  {
    cout << "\nCity: " << (*it).name;
    cout << " Population: " << (*it).population;
  }
  cout << "\nAverage population: " << avg_population;
}

void cities::print_cities_abv_avg_pop()
{
  cout << "\nList of cities having population more than average population of " << avg_population << " are: ";
  for (vector<a_city>::iterator it = cities_v.begin() ; it != cities_v.end(); ++it)
  {
    // Check if the current city has population more than the average population.
    if((*it).population > avg_population)
    {
      cout << "\nCity: " << (*it).name;
      cout << " Population: " << (*it).population;
    }
  }
}

int main()
{
  cities cities_obj;
  cities_obj.get_cities();
  cities_obj.print_cities();
  cout << "\n";
  cities_obj.print_cities_abv_avg_pop();
  cout << "\n";
  return 0;
}
Topic archived. No new replies allowed.