C :: Sorting Elements In Lexicographical Order
Jan 30, 2013
I have a code right here that i worked on with my teacher. I can say that he did most of the work. Basically i'm sorting elements in a dictionary order.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 50 // max word size
void swap(char **p, char **q) // Q1: Why do I need a double pointer?
[Code] ....
View 2 Replies
ADVERTISEMENT
Jul 6, 2013
I am writing a function called swap that sorts the elements of an array in order from highest to lowest values where they descend and ascend. A particular thing about this function is that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for the elements in the original array and then swap sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. You can declare and initialize the original array without any user input.
For example, int arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };
Header of the function swap must be as shown below: void swap(int array[],int swapedIndexes [], int size, char mode) When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order. int swappedIndexes[5];swap(array,swappedInde… 5, 'a');
So far I have this code that randomly generates arrays of 10 elements and I found the max and min, but I am confused on what to do about the swapping part? Also, how to make the numbers ascend or descend in numerical order?? Here is what i have done so far:
#include<ctime>
#include <iostream>
using namespace std;
int min(int [],int);
void max(int [],int , int &);
void main()
{ srand(time(0));
//1-declare
[Code] .....
View 1 Replies
View Related
Nov 15, 2013
This program I am trying to write has a structure of persons with a first name last name and age for each.
What I need it to do it display the order of the persons before sorting and then display the persons after sorting by age.
So far I have written the overall program but am stuck at the most important step...which is defining the functions that will be doing all of the work.
For sort_by_age I want use bubble sorting by writing the function myself and for print_person_info I am confused on how to print data out of the struct.
Code:
#include <stdio.h>
struct person
{
char first_name[20];
char last_name[20];
int age;
};
void print_person_info(struct person clone);
void sort_by_age(int n, struct person a[]);
[Code]...
View 2 Replies
View Related
Feb 17, 2015
I want to sort a vector int in ascending order, but when I test, the output isn't correct - the vector is still unsorted. Am I calling it incorrectly?
int sorted (vector <int> a) {
int size = a.size();
sort(a.begin(), a.end());
View 2 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
May 28, 2014
// OK this program outputs an array of numbers which are read from two .txt //files which are set1 and set2.
// set1.txt has: 8 37 29 31 40 25 18 38 4 45 34 39 12 21 24 3 5 23 26 44
// set2.txt has: 46 42 25 20 19 29 49 4 32 2 10 12 39 17 33 6 3 15 45 21
// But the problem is that when you run the program, the numbers do not come out // in numerical order.
#include <iostream>
#include <fstream>
#include <string>
[Code]....
View 2 Replies
View Related
Nov 25, 2014
I need to find the Mean, median, mode, and make a histogram of a 99 value array. How ever my sorting function is not sorting the array at all how can I fix this.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <cctype>
[Code].....
View 8 Replies
View Related
Sep 25, 2014
So I try to sort an array with qsort and then save the new order, so this means if I have a list like:
4 3 7 2
[0] [1] [2] [3],
after sorting it becomes:
2 3 4 7
[3] [1] [0] [2] <== this is what I want to have!
Code:
void qsort_my(int *a, int l, int r, int *array_order) {
int j;
if( l < r ) {
j = split( a, l, r, array_order);
qsort_my( a, l, j-1, array_order);
qsort_my( a, j+1, r, array_order);
[Code]...
But my problem is that the list gets sorted, but the indexes do strange stuff. When I run with this list:
4 8 14 1 2 1 22 12 2 14
Pos: 0 1 2 3 4 5 6 7 8 9
I get:
1 1 2 2 4 8 12 14 14 22
Pos: 1 0 1 0 0 5 7 6 5 6
And with some printfs I noticed, the first two calls of split it is fine but then strange things start to happen, what I do wrong?
View 6 Replies
View Related
Jun 1, 2013
Let assume that there is an array of integer numbers in the range from 1 to N that are located in the ascending order. For example
#include <iostream>
#include <numeric>
#include <iterator>
int main() {
const size_t N = 20;
[Code] .....
How can it be converted using some one standard algorithm such a way that the resulted sequence would look like
1 3 5 7 9 11 13 15 17 19 20 18 16 14 12 10 8 6 4 2
View 19 Replies
View Related
Jul 2, 2014
make my program sort data.in this case number that i declared as char(not string, my bada)if i have
name1
number 2500
email
name 2
number 2400
email
i need to put that this way:
name 2
number 2400
email
name1
number 2500
email
i saw that can be done with qsort but when i try it it doesn't work.
Code:
typedef struct {
char nome[MAX_GERAL], email[MAX_GERAL], morada[MAX_GERAL], postal[MAX_GERAL], numero[MAX_GERAL], geral[MAX_GERAL];
int telefone, FP, SD, AM1, ALGA, CM;
}dados;
code to add info i need to sort "numero"
Code:
void adicionar(dados* contacto){
if (i<total) {
printf("
Introduza o Nome: ", i + 1);
scanf(" %[^
[code]....
View 7 Replies
View Related
Sep 7, 2014
What kind of code should i use for sorting numbers in both ascending and descending order? I don't know how to use bubble sorting either, is there another easy way to sort this out?
View 3 Replies
View Related
Apr 14, 2014
I am trying to read information in from a file, each line of which contains a name in the form 'last, first middle'. I can successfully open the file and read it into an array that is roughly 2500 string in size. The problem comes when I try to sort the array into alphabetical order. I am using a sorting algorithm that we were instructed to use, found in our book. It is shown in the code. I would add the file that needs to be opened, but it is a few thousand lines long.
The error I am getting is:
Unhandled exception at 0x00A28628 in PeopleSearch.exe: 0xC0000005: Access violation reading location 0x00CC400C.
and I know it has to do with the sorting algorithm from using break points and running the program with that section commented out. I am stumped to why it is giving this error, as it seems that it is trying to access the array outside of the bounds of the array, but it shouldn't be
Here is my code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string NameArray[2500], filename; //declare array and file to open
[Code] ....
View 2 Replies
View Related
Nov 18, 2014
I'd like to modify this code so that there is 1000 numbers instead of 100, and that they're sorted in ascending order instead of descending order.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double* sort_array(double* sort) {
for (int a=1; a<100; a++) {
[code]....
View 2 Replies
View Related
Jul 20, 2014
Here is my code below:
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<string>
using namespace std;
void main() {
fstream file;
string id,name,type,price;
[code].....
I can't separate and align 4 categories neatly, how to do this solution below(the link of example for sorting Product ID in ascending order): [URL]
By the way,here is the required .txt file(test.txt): [URL]
View 4 Replies
View Related
Oct 26, 2013
I know there is a function in algorithm class called sort() but I need to sort ignoring the case of the alphabet
Is there any function that does that?
View 2 Replies
View Related
Jul 15, 2013
I have two vectors of float that I need to sort in reverse order. reverse() seems to work for one of them, but not the other. I have toggled between sort() and reverse(). for one vector, the expected behavior is seen and the order reverses. For the other, there is no change in order.
This is very simple code, so it's hard to imagine what is going wrong.
Code:
vector<float> vec1, vec2;
vec1[0] = 14.1102; vec1[1] = 14.1145;
vec2[0] = 15.8508; vec2[1] = 26.0842;
sort( vec1.begin(), vec1.end() );
sort( vec2.begin(), vec2.end() );
[Code] ......
Printout is,
Code:
vector 1 sort
14.1102
14.1145
vector 2 sort
15.8508
26.0842
vector 1 reverse
14.1102
14.1145
vector 2 reverse
26.0842
15.8508
You can see that the order of the first vector did not change. Am I right in suspecting that the numbers are too similar for what ever method reverse() uses to determine the difference between values?
View 8 Replies
View Related
Jan 11, 2014
I wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
Code:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int main() {
int i,j,p,q,c1,c2,r1,r2,k,temp=1;
char ans1,ans2,answer;
[Code] ....
View 3 Replies
View Related
Nov 7, 2013
How do i sort records by user specified field in ascending order, without knowing how many columns the file has? its getting the data from a file.
View 6 Replies
View Related
Feb 13, 2013
I have a vector which contains vectors containing 7 integers each. I'd like to sort these vectors based on the value of the first integer (int IOT), in ascending order. I know this type of question can be found everywhere, but I'm lost as to why this doesn't compile.
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <windows.h>
using namespace std;
class orders {
public:
int IOT; // Incoming Order Time
[Code] ....
View 7 Replies
View Related
May 2, 2014
The assignment is to read in a text file of passenger names (first and last initial), seats (A1, B1, C1, D1, A2, etc.), and ticket prices. The output is specified to be a table of passengers and their seats sorted by seat order, and I also have to print the minimum and maximum ticket prices along with the owner of each ticket, along with the average of all ticket prices. My arrays are all reading in correctly, and the minimum, maximum, and average ticket prices were all easy to find. My problem is the sorting.
We were introduced to arrays a couple weeks ago and this is our first time doing any sorting. We are supposed to use a bubble sort, and while I think I gather how to use a bubble sort, it's not working in my program (so obviously I don't really know how to use it). I have tried several different things, and sometimes a few seats/names will switch around (never the way I want them to) and sometimes nothing will happen at all.
New to programming, not trying to get homework done for me, need to bubble sort airplane passengers and seats by seat order (A1, B1, C1, D1, A2, etc).
My input looks like this:
A Z A1 555.55
repeat with different initials, seats, and prices however many times the user sees fit.
I have a 1D character array for the columns(A - D), a 1D integer array for rows (1 to maximum integer), 1D float array for prices, and 2D array for name initals.
Here's the function to read in the arrays:
void read_Arrays(char seat_cols[], int seat_rows[], char name[][LAST], float price[]) {
int i, j;
for(i = 0; i < FIRST; i++) {
for(j = 0; j < LAST; j++) {
scanf(" %c", &name[i][j]);
} scanf(" %c %d %f ", &seat_cols[i], &seat_rows[i], &price[i]);
}
}
Here's the sort. The way we were shown to do it was with a 'for' loop nested within a 'do while':
{
do {
flag = 0;
for(i=0; i < ROWSIZE-1; i++) {
if(seat_rows[i] > seat_rows[i + 1]) {
[code]....
View 6 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
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
Oct 24, 2013
I am having problems sorting data... I don't know to to go about it here is my code:
<code>
#include <iostream>
#include <string>
using namespace std;
int main () {
int i;
struct person
[Code] ....
i want to sort it out so that it can output names and ages in ascending order.
View 2 Replies
View Related
May 7, 2013
I previously tried to put strings in an array, but i couldn't modify the first index in the string that is in the array. I changed my code later.
[URL]
Suggest a function that sorts the inputs vertically according to the order given by the user? I made this one
void order(string order, char index[][15]){
int counter=0,line=0,i=0,j=0;
for(i=0;i<=15-1;i++)
{for(j=0;j<=15-1;j++) {
if(index[j][i]==order[counter])
[code].....
View 1 Replies
View Related
Jan 21, 2014
So I'm trying to sort an array of 10 elements provided by the user. For Example:
Person 1: 5
Person 2: 3
Person 3: 9
etc etc...
I want it to also pair the value with the person. So it should read:
person 3: 9
Person 1: 5
Person 2: 3
Sort the values in descending order.
I can sort the array, but I cant get the Person number to pair with the value...
View 4 Replies
View Related
Sep 13, 2014
I need to make singly and doubly linked list classes that can insert elements. Once the lists are created, I need to order the linked list elements according to a certain pattern.
order2 pattern:
input: 0 1 2 3 4 5 6 7
output: 1 0 3 2 5 4 7 6
order3 pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11
output: 2 1 0 5 4 3 8 7 6 11 10 9
sequence order pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
output: 1 0 4 3 2 8 7 6 5 13 12 11 10 9
reverse pattern:
input: 0 1 2 3 4 5 6 7 8 9
output: 9 8 7 6 5 4 3 2 1 0
My instructor has given the description of the required classes in a file interfaces01.h as:
#pragma once
#include <cstdlib>
struct ISingleNode {
ISingleNode() {}
virtual ~ISingleNode() {}
virtual void setValue(int value) = 0;
virtual int getValue() = 0;
[Code] ....
However when I am compiling these files in vs 2013 I am getting the error: cannot open include file Singlenode.h,Singlelist.h.
Also I am not too sure about the sorting and reverse logic. How can I implement these logics.
View 1 Replies
View Related