C++ ::  Sorting Pairs Of Numbers Using AMP

Dec 15, 2014

I need numbers next to each other to be sorted (in increasing order).On the CPU I'd do something like this:

for(int i=0; i < length; i+=2) {
if(a[i]>a[i+1]) {
switch places...
}
}

But how would I do this using parallel_for_each (C++AMP) ? I need this for some algorithm that works with very long arrays and I think GPU would do this faster than CPU (even if I use all threads).

View 3 Replies


ADVERTISEMENT

C++ :: Magic Pairs - Integer Numbers

Dec 10, 2013

Alexandra has some distinct integer numbers a1,a2...an.

Count number of pairs (i,j) such that:
1≤ i ≤ n
1≤ j ≤ n
ai < aj

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1, a2, ..., an denoting these numbers.

Output

For each test case, output a single line containing number of pairs for corresponding test case.

Constraints

1 ≤ T ≤ 4
1 ≤ n ≤ 100000
0 ≤ ai ≤ 109
All the ai are distinct

Example

2
2
2 1
3
3 1 2

Output:
1
3

Explanation

Case 1: Only one such pair: (2,1)
Case 2: 3 possible pairs: (2,1), (2,3), (3,1)

as I understand the problem is just counting how many Ai's are 1 <= Ai <= N and then apply ((R*(R-1))/2), R is the count of valid Ai's

My actual code is

#include <stdio.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define getNumber getchar()
#define getString getchar()

[Code] ....

View 4 Replies View Related

C++ :: Program To Generate Pairs Of RSA Keys Using Small Prime Numbers

May 21, 2012

I have been writing a program to generate pairs of RSA keys using small prime numbers. I have been using mpz_invert() to get a value for d for the private key. On small prime numbers it calculates the correct value, but on larger primes it calculates incorrect values.

I'm assuming there must be an upper limit to the values the mpz_invert() function can handle? If so, what is this limit to avoid erroneous values?

View 1 Replies View Related

C :: Sorting Four Numbers

Mar 2, 2013

I'm trying to take a users input and break it up into four separate numbers, then take those numbers and arrange them from smallest to largest.So far I can't seem to get them working right.

Code:

# include <stdio.h>main ()
{
int inputVariables[4]; //where userinput goes after being broken up
int arrangedValues [4];// the user values arranged lowest to highest
int i;
int j;
}

[code].....

View 4 Replies View Related

C++ :: Sorting Numbers From File

Apr 6, 2013

I have been working on this program for days now and for some reason my program will not write to my third file. It will call all of the integers that I need it to call but it will not write them out to a file. How to use my loop correctly.

#include <iostream>
#include <fstream>
using namespace std;

int file1();
int file2();
int file3(int);

[Code] ....

View 10 Replies View Related

C++ :: Sorting Alphabets And Numbers

Apr 20, 2014

My program reads a string of characters. Prints all occurrences of letters and numbers, sorted in alphabetical and numerical order. Letters will be converted to uppercase.Other characters are filtered out, while number of white spaces are counted.

The problem is it crashes when i run the program. Here is my code

#include <iostream>
const int SIZE = 100;
using namespace std;
int main() {
char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
int count = 0, i = 0, j;

[Code] .....

View 5 Replies View Related

C/C++ :: Sorting Numbers Through Pointers?

May 5, 2014

I'm writing a simple program to sort numbers through use of a pointer. I've always been bad with pointers, and I tried to use a typical temp value to hold the value, but i feel like I am over doing it.

#include <iostream>
using namespace std;
void SortTests(int *, int);
int main() {
int *ptr;
int tests;

[code]....

View 2 Replies View Related

C++ :: Getting Correct Numbers After Sorting

May 10, 2014

I'm trying to make a number sorting program with other features, but the numbers are all wrong.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void readData(int list[], int size);
int main() {
int size = 50;
int scores[50] = {0};

[Code] ....

Input.DAT

376 389 450 735 600 576 612 700 450 628 778 389 667 500 475 550 687 791 829 344
549 476 400 587 535 657 789 583 340 764 422 826 566 436 565 834 533 423 837 701
847 521 746 356 582 465 493 593 425 421

View 2 Replies View Related

C++ :: Sorting Names And Numbers And Also Using Templates

May 7, 2013

For my project I have to sort 5 numbers and 5 names using a template bubble sort. I have one header for the numbers, and one for the names. This is what I have so far for my testing page:

#include "Floatheader.h"
#include "Nameheader.h"
#include <string>
int main ()
myFloat obj1;
myFloat obj2( 2.2, 5.1);

[Code] .....

I have to create a template to look like this: template<>....with a class inside the arrows. Then, I have to use bubble sort to sort the 5 names and number objects I have created. Sorting the names and numbers and also using templates?

View 1 Replies View Related

C++ :: Sorting 3 Numbers By Ascending Orders?

Jan 30, 2015

We've only covered up to Functions and how to use reference variables inside the function parameter.

One of the hw problem that was assigned was to write a void function that takes three parameters( num1, num2, num3) by reference and sorts their values into ascending order, so that num1 has the lowest, num2 the middle value, and num3 the highest value. For example, if user enters: 14, -4, 8, then the output should look like this:

-4
8
14

I've completed the program with a bunch of if/ else if statements but I was wondering if there was a more efficient way to sort the numbers. Bear in mind, we've only covered materials up to functions so I can't use any other new techniques that we haven't cover yet. Here is my code:

// This program will take three int parameters by reference and sorts their value into ascending order
//so that num1 has the lowest value, num2 has the middle value, and num3 has the highest value
#include <iostream>
using namespace std;
// declare function with reference parameter that with sort numbers
void sortNum(int &, int &, int &);
int main ()
{

[Code]....

View 2 Replies View Related

C++ :: Number Sorting Program - Getting Correct Numbers

May 10, 2014

I am a beginner at C++, the output is pretty self explanatory. I'm trying to make a number sorting program with other features, but the numbers are all wrong.

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

[Code] .....

Input.DAT
Code:
376 389 450 735 600 576 612 700 450 628 778 389 667 500 475 550 687 791 829 344
549 476 400 587 535 657 789 583 340 764 422 826 566 436 565 834 533 423 837 701
847 521 746 356 582 465 493 593 425 421

I can't for the life of me figure out why the numbers are all messed up.

View 5 Replies View Related

C++ :: Sorting Numbers In Both Ascending And Descending Order?

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

C++ :: Sorting Arrays - Int Numbers From Highest To Lowest

Oct 29, 2014

For my program i need to sort my int numbers [5] from highest to lowest but it seems that i made it go from lowest to highest ...

Code:

#include <iostream>
using namespace std;
int main() {
int numbers[5] = {3, -6, 10, 1, 130};
int counter1, counter2, tempNum;
//Highest to Lowest

[Code] ....

View 1 Replies View Related

C/C++ :: Sorting Numbers In Array From Largest To Smallest?

Mar 16, 2014

I'm trying to sort the numbers in an array from largest to smallest and print it but whenever I run this it does not sort anything at all.

#include <stdio.h>
#define SIZE 10
void Input(const int array1[]);
void Calculations(int array1[], int average);
void Output(int array1[], int average);
size_t counter1 = 0;
int average;
int main( void ) {
int array1[ SIZE ];

[code]....

View 4 Replies View Related

C/C++ :: String Of Characters - Sorting Alphabets And Numbers

Apr 20, 2014

My program reads a string of characters. Prints all occurrences of letters and numbers, sorted in alphabetical and numerical order. Letters will be converted to uppercase.Other characters are filtered out, while number of white spaces are counted.

the problem is it crushes when i run the program

Here is my code

#include <iostream>
const int SIZE = 100;
using namespace std;
int main() {
char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
int count = 0, i = 0, j;

[Code] .....

View 1 Replies View Related

C/C++ :: Sorting Program That Sorts Numbers From Least To Greatest

Jul 5, 2014

I have to write a program that sorts numbers from least to greatest. The way that it has to sort them is:

1) The program assigns the first number to be a minimum
2) If the next number is less than the minimum is now that number and they switch places. (We keep on looking if the number next to it is not smaller)
3) The program also gets the index of the minimum number
4) We keep on going until it is in order.

// Sort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void Sort(vector<int>&input);
int _tmain(int argc, _TCHAR* argv[]){
vector <int> input;

[Code] ....

View 4 Replies View Related

C :: Storing Key - Value Pairs

Mar 26, 2013

I have a project to do in C and coming form Java I miss all the included features that are missing from C! I need to be able to store key value pairs in an quick and memory efficient manner. I've looked up using hash maps but I'm very new to C so don't really understand even the basic ones. I've looked at using a multi-dimensional array as I'm more comfortable with arrays but I'm unsure if that would count as a memory efficient and quick method?

View 14 Replies View Related

C++ :: Vector Of Pairs Of References?

Jun 19, 2014

I am attempting to combine two vectors into a vector of pairs. I want to be able to alter the first and second of each pair and have those alterations reflected in the original vectors. I thought the following code might work but get compilation errors about a lack of viable overload for "=" for the line with the call to std::transform:

void f()
{
std::vector<int> a = {1,2,3,4,5};
std::vector<int> b = {6,7,8,9,0};

[Code].....

View 3 Replies View Related

C++ :: Sorting Large Numbers Of Vector Of Random Integers Returns All Zero

Jul 31, 2014

I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?

View 1 Replies View Related

C/C++ :: Non Repeating Binary Pairs Of N Values

May 19, 2014

I am working with cart algorithm and i have the following problem,i need to try all possible splits of n values in order to determine the best.

So lets say i have 3 values("low","medium",high) then the possible splits(pairs) would be:

(assuming low=0,medium=1,high=2)

0,1-2 low,medium-high
1,0-2 medium,low-high
2,1-0 high,medium-low

For 4 values(a,b,c,d) it would be:

ab,cd
ac,bd
ad,bc
abc,d
adb,c
adc,b
bcd,a

Problem is i dont know n so the solution must be recursive. The possible splits are 2^(n-1)-1. I am really stuck and most of the code is complete for cart and i really don't want to restrict it to binary values.

View 1 Replies View Related

C++ :: Searching A Vector Of Objects For Pairs

May 16, 2013

Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

In general, what do you think is the best way to do this?

View 5 Replies View Related

C++ :: Import A File Containing Words And Numbers To Linked List - Bubble Sorting

Feb 16, 2013

I have a code able to import a file containing words and numbers to a linked list, but I also need to sort this linked list alphabetically. I've done research on this involving bubble sorting, but no explanationcan achieve this objective.

Below is the code that can only put the file into linked list:

Code:
#include<iostream>
#include<conio.h>
#include"U:C++WordClass2WordClass2WordClass.cpp"
#include<fstream>
#include<vector>
#include<string>
using namespace std;

[Code] .....

View 5 Replies View Related

C++ :: Sorting User Defined Amount Of Random Numbers In Ascending Order

Jan 30, 2013

I want to implement a function into the code below that sorts the user defined amount of random numbers and then sorts them in ascending order.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;
class IntegerArray {

[Code] ....

View 5 Replies View Related

C++ :: Sorting Array Of Numbers Into Ascending Order - Bubble Sort Keeps Crashing

Oct 29, 2014

I am trying to write a program which will sort an array of numbers into ascending order, here is my code

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int array[]={1, 5, 17, 3, 75, 4, 4, 23, 5, 12, 34, 34, 805, 345, 435, 234, 6, 47, 4, 9, 0, 56, 32, 78};

[Code] .....

This compiles fine but when I run the .exe for the first time an error message comes up saying program has stopped working. If I run the program again without recompiling it seems to work as expected.

View 2 Replies View Related

C++ :: LinkedList Based Abstract Datatype For Pairs And Such

Feb 28, 2013

coming from Java, my experience with the Classes in C++ is quite limited.

Thats why I am having trouble converting the following (simple!) Java-Program.

Most examples with linked lists I found on the web describe how to implement the LinkedList class itself. But my problem is different: I want to use such a Class (I have a LinkedList class available on my system which is presumably OK).

Code: package main;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
BoxList<String> sl = new BoxList<String>(
new ArrayList<ABox<String>>());
sl.getList().add(
new BoxPair2<String>(new BoxPair2<String>(

[code].....

View 10 Replies View Related

C/C++ :: Find Minimum Difference Between Two Pairs Of Integers In A List / Array

Jan 18, 2015

I'm trying to write a simple program that finds the minimum difference between two pairs of integers in a list/array. Of no surprise, it not work the first time I ran it so naturally i chucked in a whole bunch of print statements to debug

I encountered a peculiar error that is really stumping me.

#include <iostream>
using namespace std;
int closest_pair(int arr[]) {
int i=0;
int j=0;
int size=0;
int min=0;

[Code] .....

OUTPUT:
size_main: 20
arr_main: 80
arr[0]_main: 4
size: 2
arr: 8
arr[0]: 4
0: 34
1: -12
2: 18
3: 61
19: 75
closest_pair: 0

I'm printing the size of the array in main and inside the function...and inside the function its giving me a size = 2 which is incorrect though the size printed in main is correct (size = 20). I further narrowed down the discrepancy to this statement: sizeof(arr)

View 2 Replies View Related







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