C :: Selection Sorting A 2D Array
Feb 13, 2013
Selection sorting a 2D array . Let's say i have an array like
1 2 3 4 //4 elements
1 2 // 2 elements
1 2 3 4 5 //5 elements
1 2 3 //3 elements
1 //1 element
And I want to do a selection sort it in descending order which the row with 5 elements will come first then 4 then 3 and so on. So that it would look like this
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Code:
void selectionSortDescending(int list[MAX_ROW], int size){
int temp;
int walk;
int curr;
int large; // index of the largest element
for (walk = 0; walk < size - 1; walk++)
[Code] ....
View 7 Replies
ADVERTISEMENT
Dec 7, 2013
I am trying to write a program to sort the characters in a word alphabetically. For example, if you input 'what', the computer will sort it into 'ahtw'. But, it fails to work. I didn't know why.
Code:
#include <stdio.h>
#include <string.h>
main() {
[Code].....
View 8 Replies
View Related
May 4, 2014
This program using the selection, insertion, and bubble sorts. The program needs to be able to do the following:
1. Create an array of 1000 population records when the array object is instantiated. Call it unSorted.
2.Open the file called "Population.csv" (on the portal) and invoke a function that loads the population data into the array.
3.Create a second array of 1000 elements that will be used to sort the data using the different algorithms. Name is sortedArray.
4.Write a function that will copy unSorted into sortedArray and execute that function.
5.Using a function, display the unsorted array.
6.Invoke the insertionSort () function that will sort the sortedArray using the insertion sort algorithm. Sort the population data on the rank field in ascending order. Alternatively, you can sort in descending order on population.
7.Using the display function, display sortedArray.
8.Display the number of iterations it took to do the sort using this algorithm.
9.Repeat steps 4-8 for the selection and bubble sort algorithms.
Here is my code so far:
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void loadArray (int unSorted[], int s);
void displayArray (const int num [], int size);
[Code] .....
Here is a few lines from the Population.csv file contents:
Code:
Alabama,Baldwin County,140415,389
Alabama,Blount County,51024,908
Alabama,Calhoun County,112249,477
Alabama,Colbert County,54984,858
Alabama,Cullman County,77483,653
Alabama,Dale County,49129,927
Alabama,Dallas County,46365,974
Alabama,DeKalb County,64452,753
I'm not sure how to load the data from the file into the array properly, I attempted this. I also don't know how to copy the unSorted into sortedArray.
View 14 Replies
View Related
Feb 9, 2013
I'm trying to make a selection process using roulette wheel selection. To do this I created two matrix, one random probabilities and one increasing probabilities. The idea is to choose a number according to the random probabilities. Here is the code I've written.
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main (){
float select [5], prob [10], mat [5];
int c, r, z;
cout.precision (2);
cout << "Random Number:" << endl;
[Code]...
The result I got is as follows:
Random Number:
0.0013 0.56 0.19 0.81 0.59
Increasing Probabilities:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Selected Column:
0
5
6
8
9
The evaluation doesnt seem to start from c=0 again. The selected column should be 0, 5, 1, 8, 5.
View 6 Replies
View Related
Sep 6, 2014
I need to select a random item from an array but I am going to have more than 1 such array so I created the random value selector as different function. Right now I am getting a correct value for random function but when I try to access the array value associated with that index I get 2 digits which does not make any sense.
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int randFunction(int size);
int main() {
int heapsize;
[code]....
View 3 Replies
View Related
Feb 18, 2014
How to sort the last name instead of first name. Everything works so far and sorting is done on the first character.
#include <iostream>
#include <cstring>
using namespace std;
void sort(int, char[10][40]);
int main() {
char twoD[10][40];
int input = 0;
[Code] .....
View 4 Replies
View Related
Jan 24, 2013
I have written a selection sort algorithm to go sort an array of class objects by age in ascending order, the problem is that the output being given does not match what i think the code should do. when the program runs the 3 records are added to the array and when they are sorted should be outputed in ascending order, the problem is that with my code the last 2 are sorted properly but the first element does not seem to move, it remains the same as the original unsorted value.
My code for the selection sort function and the display method are below:
void selectionSort() {
int i, minIndex, minValue;
for (i = 0; i < (arrlength - 1); i++) {
minIndex = i ;
[Code].....
View 1 Replies
View Related
Aug 29, 2013
I am writing code for a program that will take user input selection of columns and determine an array based on that.The number of columns will be user selected.The number of rows equals 3^(columns) <--exponent not XOR
- This is because each column has the possibility of having the numbers 0,1,or 2
For example, if the user were to select "4" columns, then the array would have 4 columns and 3^4=81 rows. Then I would like to populate this with all of the possible combinations of 0,1,2
i.e.
0000
0001
0002
0010
0011
0012
0020
0021
0022
0100
....
2220
2221
2222
how I would create the "For" Loop for this?
View 19 Replies
View Related
Feb 19, 2014
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.
You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.
I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861)
Also, I feel like I'm missing something in int main() to get it to sort properly.
Code:
# include <iostream>
using namespace std;
int main() {
const int SIZE1 = 3;
const int SIZE2 = 8;
int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },
[Code] ....
View 1 Replies
View Related
Apr 18, 2013
I am trying to create a code to sort an array of integer, but only between two positions of the array, not the all array.
like this:
array: 1 2 5 4 7 2 9 8
index: 0 1 2 3 4 5 6 7
i want to sort the array per exemple between the the index 2 and 5.the result is... array: 1 2 2 4 5 7 9 8
View 2 Replies
View Related
Feb 8, 2015
I tried sorting arr2 from lowest to highest value, but it only gives me 4 values and then the rest are zero.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(1);
[code].....
View 1 Replies
View Related
Nov 18, 2013
I am trying to sort an array of size 10. If I was given:
Code: int List[Size] = {29, 11,12,10,3,26,13,15,19,2};
I need the program to sort all the odd integers and put the even integers to the back of the array. Like so:
Index: 0 1 2 3 4 5 6 7 8 9
Value:29 11 3 13 15 19 12 10 26 2
And it returns the number of even integers in the List. In this case it returns 4. All I am given to start with is
Code:
int evensToRead(int* const List, const int Size){
//body
}
View 1 Replies
View Related
Oct 24, 2013
i have a matrix containing a lot of points and each point has its coordinates x and y. That is a nx2 size array. I want to sort it according to the first column ascending, with x coordinates. For points that have the same x coord i would like to sort according to y coord. Here is what i did and i cannot get a good result.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int a[5][2] = {{1,0}, {4,2}, {2,4}, {8,6},{4,8}};
int temp=0;
int i=0;
int j=0;
[Code]...
View 4 Replies
View Related
Jan 22, 2013
i need to print the names as they appear in the original file, print the info of the person with the highest distance, print the info sorted by ascending ID number, and print sorted by name alphabetically. the first two parts work fine but sorting by ID and Name dont work.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
[code]....
View 1 Replies
View Related
Jan 3, 2014
I have a question about sorting a 2D array. Lets suppose I've got the following array:
2 5 7 4 8
3 11 14 5 2
6 3 12 9 1
7 15 11 4 2
8 16 13 5 1
I would like to sort this array diagonally to make it look as : [URL] ....
View 10 Replies
View Related
Jan 9, 2014
Here is what I have, I have a 1D Array being added to a 2D Array and I need to Sort them by value value 3 in the 2D Array, while maintaining a specific amount. Here is what I have so far:
public static void CheckHS(string[] HS) {
try {
GeneralData.HighScores[10, 0] = "11";
GeneralData.HighScores[10, 1] = HS[1];
GeneralData.HighScores[10, 2] = HS[2];
GeneralData.HighScores[10, 3] = HS[3];
//Need Sort Data - Bubble Sort?
}//end Try
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
I am thinking bubble sorting but I remember reading about something faster. Unfortunately I can't find it on the web. The idea is that there will be always 10 Values and 4 Columns on the 2D Array. [The 11th Row being empty at the end of it.
View 14 Replies
View Related
Jan 3, 2014
How to sort a 2D array diagonally? Lets suppose I've got the following array:
2 5 7 4 8
3 11 14 5 2
6 3 12 9 1
7 15 11 4 2
8 16 13 5 1
I want to create a function that sort them diagonally like this: [URL] .....
View 7 Replies
View Related
Jun 17, 2013
I need to sort the elements in a 2d array by their index (starting from 1) for example:
Code:
1 5 3
4 7 8
4 10 2
10 is the biggest element and its index is 32, after 10 comes 8 with index 23 etc etc...
Looking for examples for two orders ... By descending and ascending order...
View 5 Replies
View Related
Feb 2, 2015
I have an algorithm and I want to make it as efficient as possible. Basically it just involves putting numbers in order. I have two options, but which one would be more efficient:
1. Using a doubly linked list. Every time a user wants to add a new number, the algorithm will start searching the correct place for the number from the beginning of the list. This is efficient per se, but once there are about a million numbers and a number has to be put in at the end of the list, the algorithm must go through all the 999 999 numbers before it.
2. Using a container to store all the numbers first, then sorting the numbers. In this case, adding all the numbers is fast, but the actual sorting will take a long time.
Which option would be more efficient? I was thinking of using maybe merge sort or quick sort in option 2. Yes, I'm aware I could just use vector and sort, but that's not my goal here.
View 4 Replies
View Related
Nov 7, 2014
I wish to sort a string array alphabetically and im not sure if i am doing it right. for example:
string a[5]; //initial array
string b[5]; //sorted array
int index = 0; //index of b array
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
int c = 0;
[Code] ....
View 2 Replies
View Related
Dec 29, 2013
I need to sort an array of n given elements using recursion. What am i doing wrong here?
#include <iostream>
using namespace std;
int i=0, j=1, v[100], n;
[Code].....
View 2 Replies
View Related
Nov 25, 2013
Why this wont run? I'm getting the cant convert int* to const char error but i do not know why?
I'm trying to create a program that creates an array of 100 ints between 0 and 250 and then sorts them using a separate function, I've gotten this far
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <cstring>
using namespace std;
void quicksort (int *items, int len);
[Code] ....
View 3 Replies
View Related
Jan 4, 2014
This code prints 10 20 40 50 30.
#include “stdafx.h”
#include
#include
using namespace std;
int main() {
int anarray[5] = {40,10,50,30,20};
for (int iii=0 ; iii <= 4 ; iii++)
[Code] .....
View 1 Replies
View Related
Apr 3, 2014
I have two arrays of characters that I want to combine and sort according to an internal variable (init) using a forward-iterating linked list. The two arrays must stay separated, as one of the arrays (the enemies) is contained within the object (encounter), the other is passed in via pointers (the players). The array inside the object will be destroyed later (when the encounter is over and the enemies are hopefully dead) while the one that is passed in must survive to be passed into other objects at a later time (the next encounter). My thought is to sort each array by linked list separately first, then iterate through and combine the two lists, But how to do this and no support IRL.
// DECLARATION OF CLASSES //
class character{
public:
character(); // Constructor
[Code]....
View 1 Replies
View Related
Oct 25, 2014
I should sort an array of mixed float and integer numbers by merge method, and using the pointers to sort that mix array. how to use pointers to sort those different type of data.
View 19 Replies
View Related
Mar 26, 2014
Were supposed to be able to sort an array which is user input, but the first number they input is supposed to be the length of the array, and everything after that is the actual array.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int size;
int list[80];
bool isSorted (const int list[], int size);
[code]....
I'm stuck on how I'm supposed to use the first digit of input for the array size.
View 6 Replies
View Related