### 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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include 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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` ``````#include #include #include 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 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::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::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.