C :: Sorting 2D Array By Column

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


ADVERTISEMENT

C :: Store Sum Of EACH COLUMN In 2D Array Values In 1D Array?

Mar 29, 2013

How can I Figure out and store the sum of EACH COLUMN in the 2D array values in a 1D array? And to the same for the average?

View 1 Replies View Related

C :: Subtracting One Column In 2D Array From Another?

Oct 18, 2013

I have a 2d array where I manage donations and requests for different foods. I output a menu to the user who picks the options to either make a donation, make a request, fulfill a request, or print a status report of all the donations and requests. What I'm having trouble with is the fulfilling requests option. When the user picks fulfill requests that means that the donations are supposed to be subtracted from the requests. For example if there are 10 donations for grains and 15 requests for grains then after picking fulfill request there should be 0 donations for grains and 5 requests for grains but I don't know how to make the program do that since the values are in a 5 row by 2 column array. The five rows correspond to the five foods and the two columns correspond to donations and requests, respectively. Here's my code so far. Just disregard everything but the third case.

Code:
#include <stdio.h>int main(){
int foodbank[5][2]= {
{0,0},
{0,0},

[Code]....

View 6 Replies View Related

C++ :: Pointer With Array - Calculating Row And Column Sum

Jul 20, 2014

I have two doubts in the following code,the doubts are marked..PLs note that the following code is correct .This is a program to read 2d array using pointer ()i.e Dynamic array ,to calculate its rowsum and column sum and display this array along row sum and column sum.

#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int *Val,*Rsum,*Csum;
int MaxR,MaxC,i,j;

[Code] .....

View 2 Replies View Related

C :: Finding The Most Occurring Element In Column 1 Of 2D Array

Oct 7, 2014

This is for a 2-d array, but if i understand how to do it for a 1-d array, I can do it for a 2-d. So to keep it simpler, I will just pretend its for a 1-d array. I am working on a project where i must enter the month number of a storm and then tell the user which month had the most storms. So I have an array and the user determines the size of it by entering the number of storms. Then, using a for loop, the user enters the month number for each storm and the data is used to fill the array. This is to be used in a part of a much larger piece of code. I made a sample code below to mimic what i want to do:

Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {

/*Declare variables, i is a counting index used in for loop if necessary*/

[Code] ....

I believe this involves making some temporary array but where to start ....

View 2 Replies View Related

C :: Reading Values From File To First 2 Columns Of 2D Array / Assigning 0 To 3rd Column

Apr 4, 2014

I am attempting to read values from a file into a 2d array temp[31][2] (31 rows, 3 columns).I only want the values from the file to be read into the first two columns.I believe I am accomplishing that but when I go to print the array, I expect the first two columns to have the file data and the third column to have all zeros. The third column, however is printing such that the value is the next row/first column.

I'm not sure for instance why on the bottom loop for line 1 it doesn't print:

temp[0][0] temp[0][1] temp[0][2] 20 49 0 It instead prints: 20 49 1

Code:

#include <stdio.h> Code: #include <math.h>
FILE *inptr;
int main() {
int temp[31][2] = {0}, tempavg[31][2] = {0};
int i, j, k, sum;
inptr = fopen("ProgrammingProject14.txt", "r");

[code]....

View 8 Replies View Related

C :: Unable To Save A Column Element Into Dynamically Memory Allocated Array

Mar 3, 2014

I am trying to store each value of a column from a text file into an dynamically allocated array, which needs to be globally declared for further usage in the program.The input textfile contains the following:

34932 13854 13854 2012-01-07
172098 49418 53269 2012-01-07

I have written the following code:

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
}

[code]....

The commented printf line gives the entire values of the column, which proves that the file is correctly being read.But on compiling this program I get both compiler warnings and finally segmentation fault.

View 5 Replies View Related

C :: Sorting Array Of Integers Between Two Positions Of Array

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

C++ :: Sorting Values In Array?

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

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 View Related

C :: Sorting Array Of Size 10

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

C++ :: Sorting A Structure Array

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

C++ :: 2D Array - Sorting Diagonally

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

C# :: Sorting 2 Dimensional Array By Value

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

C/C++ :: Sorting 2D Array Diagonally?

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

C :: Sorting Elements In 2D Array By Their Index

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

C++ :: Linked List Or Sorting An Array?

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

C++ :: Sorting String Array Alphabetically

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

C++ :: Sorting Array Of N Given Elements Using Recursion?

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

C++ :: Sorting Numerical Array Error

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

C++ :: Sorting Array In Ascending Order

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

C++ :: Sorting Array By Using Linked List

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

C++ :: Sorting Array Of Mixed Float?

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

C++ :: Sorting A User Input For Array?

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

C++ :: Bubble Sorting A Classed Array

May 8, 2014

I'm really struggling with bubble sorting a classed array(an inventory). I understand bubble sorting in theory and implementing it into a static array makes sense. When I get to doing it in a classed array then I struggle. I need to sort the weight by bubble sort (then selection sort for name, and insertion sort the cost; but I want to tackle one problem at a time.

Here is the code:

#include <iostream>
#include <iomanip>
#include <conio.h>
#include "Inventory.h"
#define MAX_REC 10
using namespace std;
//Class to hold inventory variables/funcitons

[Code] ......

View 6 Replies View Related

C++ :: Sorting (array Based) Database?

Mar 26, 2013

knows if there is a ready made function in C++ to sort (numerically or alphabetically) multiple arrays based on a particular array?

i.e. Imagine you have multiple arrays or a multi-dimentional array containing data about users (name, age, etc.)

Is there a function that for example can sort it by name, and automatically shift the age so they still correspond to the correct user name?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved