C/C++ :: Circular Permutation Through Function
Jan 17, 2015
I need to create such a function that the content of the first is put into the second, the content of the second into the third and the content of the third into the first.
For example, output should be like this
3
2
1
But the code below prints out:
1
2
2
Where am I making a mistake?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
[Code]....
View 2 Replies
ADVERTISEMENT
Feb 16, 2014
I need to create a function with the following prototype:
bool isPermutation( const unsigned a[], unsigned elements );
unsigned a[] = {3, 0, 2, 1};
bool P1 = isPermutation( a, 4 );
would set P1 to true because the set of subscripts is {0, 1, 2, 3}, and the values are just a reordering of those values. On the other hand,
unsigned a[] = {3, 0, 2, 3};
bool P2 = isPermutation( a, 4 );
would set P2 to false because the set of subscripts is {0, 1, 2, 3}, but there’s no value that’s equal to 1.
I'm not exactly sure how to do this. I thought about it a couple different ways. I first thought about taking the range (max/min) and then checking to see if the numbers in between are equal from each other, distance-wise.
I then thought that I should just make this basically a sort function (I used bubble-sort), and then to just check if the numbers are equi-distant from each other.
This is my basic bubble-sort. Perhaps it is wrong, but I'm not certain...Perhaps I am making this function harder than it has to be.
unsigned temp = 0;
for (unsigned i = 0; i < elements; i++){
for (unsigned k = 0; k < elements-1; k++){
if (a[k] > a[k+1]){
temp = a[k+1];
a[k+1] = a[k];
a[k] = temp;
}}}
Should I do a sort like this, and then do something where I subtract a[i+1] - a[i], and see if that equals '1'?. I would think that would mean they would have to be equidistant. Even if this is correct, I feel like it could be more efficient.
View 4 Replies
View Related
Mar 21, 2014
Write a function that computes and returns the score for a permutation, i.e. the number of reversals required to make arr[0] == 1.
HAVE TO USE FOLLOWING FORMAT:
Code:
// POST: Returns the number of reversals needed to make arr[0] == 1
// if the reversal game were played on arr
// Note: If arr[0] == 1 initially, then score(arr, n) returns 0 AND this is what i could muster;
[code]....
View 2 Replies
View Related
Nov 30, 2014
I have a function that I would like to add to tail. It is not correct.
template<class T>
void DoublyLinkedCircularList<T> :: addToTail(T element) {
DoublyLinkedNode<T>* head;
DoublyLinkedNode<T>* temp;
temp->element = element;
[Code] .....
How do I fix it?
View 14 Replies
View Related
Nov 25, 2014
I'm having some issues with my code. For the produce function i am getting an error saying 'no instance of overload function produce() matches the argument list' and also for the lines buffer[head].data = message; buffer[head].time = current_time i get an error saying 'expression must have pointer to object type.
In the code i'm not sure if i passed the variables to the function correctly. I have attached the code .....
code produce.txt
View 14 Replies
View Related
Nov 7, 2013
I got a permutation question, I got two different std::list:
list<string> slist;
slist.push_back("str111");
slist.push_back("str222");
slist.push_back("str333");
list<int> ilist;
ilist.push_back(100);
ilist.push_back(200);
I need the permutation for both two lists, the result should be like this:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
template <typename value_t>
void dump_list(const list<value_t>& lst) {
[Code] ....
See, there are two do while loop, if I need a permutation with more than two lists, there'll be more and more do-while loops, that's make code looks ugly, I wonder if stl has some tricky way that can do this with just one next_permutation.
View 5 Replies
View Related
Jan 29, 2015
I need to be able to find every possible permutation using all possible values of a char. But I have to make it be able to form permutations from a length of 1 to variable N. For example, if N=3, I need it to be able to come up with
0x00
0x01
.......
0x00 0x00
0x01 0x01
.......
0xff 0xff 0xfe
0xff 0xff 0xff
How could I do this. (I would like to avoid recursion, since N might be as large as 50 or 60 and using recursion would most likely cause a stack overflow)
View 3 Replies
View Related
May 11, 2013
I wrote this code to solve a problem in which the user inputs a permutation of size 'N' and the next permutation of the same elements has to be generated.
I went about this in this way: given 3 2 5 4 1, starting from the right, the first no. has to be searched which has a no. greater to it on its right. Here it is 2. Now an array is generated containing all no.s on its right and greater than it. For 2, it is: 5,4. Out of these the smallest member is searched for and switched with 2. So, 4 is switched with 2 to get the Next Permutation: 3 4 5 2 1.
The code I wrote does not show any error but does not return the correct value when run and gives the same value instead. If I enter '3 2 5 4 1' it returns the same value as the answer.
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int N,M,i,n,c,swap,flag,count,small,m;
int Array[100],Key[100];
[Code] .....
View 2 Replies
View Related
Aug 10, 2013
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main () {
int myints[] = {1,2,3};
std::sort (myints,myints+3);
std::cout << "The 3! possible permutations with 3 elements:
[Code] ....
this will result
123
132
213
231
312
321
The question is : If I want to get a permutation combination with range of N
What should i do?
if N = 2
result should be 12,13,21.....such and such
Eliminate the last digit is working for 3 combination but if its going to a bigger number it does not work ...
View 5 Replies
View Related
Feb 16, 2013
I'm currently working on assignment which requires to generate a random permutation of the first N integers. For example, if N = 4, one possible answer is {3,1,2,4} without duplicates. {3,3,2,4} is not correct
to fill a[4], generate random numbers until you get one that is not already in a[0], a[1], ..., a[n-1]. for a[0] whatever random number you generate can be put in it. So here is my code. It seem to works but sometime it give duplicates.
#include <iostream>
#include <stdlib.h>
#include <time.h>
[Code].....
View 3 Replies
View Related
Mar 27, 2014
I have been given an assignment that has to do with permutations. I am suppose to read a text file that contains the permutation rules and the text to be "permutated", and then output the rules and the new text into an output file.
So far, I've gotten this:
Code:
# include <stdio.h>
void printArray(FILE* file, char* array, int maxSize)
{
int i;
for (i = 0; i<maxSize; i++)
fprintf(file, "%c", *(array + i));
[Code] ....
Here is what the input file looks like:
Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3
Moderation in temper, is always a virtue; but moderation in principle, is a species of vice.
Here is what the output file is suppose to look like:
Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3
ratMnioodetem rpein al,ywa isvirsetu a t m;eod
buon r inaticipp,lerina s cpeis of icvies - Temho.
-ainaPes
The first two lines are the permutation rules. Currently I have figured out how to read the file into an array and then print it back out into a text.
What I want to do is figure out how to read only the first two line of the input file and store that as a permutation rules, and then continue reading the rest of the input file and store that separately as the text to be "permutated". And then eventually figure out how to apply the permutation to the text.
View 3 Replies
View Related
Jan 29, 2013
I need to circular right shift a cstring in C++
Let's say I have unsigned char test[10] = "HELLO!!!"; How would I go about circularly shifting this to the right? Inline assembly instructions would be ok too
View 6 Replies
View Related
Aug 30, 2013
Any algorithm or function to rotate a displayed circle. To turn it 360 degrees like a car-tire. (It's needed to turn a turn-table in a model-railrod control program) .....
View 14 Replies
View Related
Mar 15, 2013
It's compiling but it's not working, it enters in stack overflow. It's a Doubly Linked List I'm compiling in Visual Studio. I think there's nothing wrong with this declaration, but there's just might be it:
class ListItem;
class List {
public:
ListItem *firstItemRef;
[Code] .....
View 7 Replies
View Related
Jul 30, 2014
Is there a way to make a circular progress bar in WPF without using another library or NuGet Package?
Like a tutorial on how to make it from scratch?
Right now, I'm using the existing ProgressBar UserControl but it looks horrible especially after I change the Foreground color from Green to DarkBlue.
View 6 Replies
View Related
Jan 23, 2014
I have ran into some sort of Circular dependency between two classes.
Forward declaration doesn't work as I'm allocating the memory for pointer array of Student and it requires the default constructor.
P.S I'm aware I haven't written the BIG 3, it's an incomplete code. Just want to know how to resolve this dependency.
class Student;
class Course {
char *name;
Student *s[3];
[Code].....
View 14 Replies
View Related
Oct 6, 2013
I am stuck with how to make a circular queue that are based on a struct. Have been reading about the implementation but cant really understand it fully. Here is what i got so far.
Code:
#define SIZE 10
typedef struct {
char reg;
char brand;
int modelyear;
int mileage;
[Code] .....
View 8 Replies
View Related
Nov 7, 2013
Today I faced a problem where I had circular dependency in my template arguments. I was trying to make a class hierarchy similar to:
template<class BType>
class A_base {
public:
BType* getB();
};
[Code] .....
Basically I had objects that were of type A<B<A<B<...
Basically I have a tree like structure of heterogeneous types that must facilitate two-way interactions where A's can call B's and B's can call A's. This structure is useful in many contexts the difference is the methods A and B provide are different in each of these contexts. Instead of adding the getA and getB and all the other connectivity methods in every version of A and every version of B, I wanted to create a base class that managed this automatically.
Another piece of advice was break up your code so there is a forward-only and backwards-only dependent types. This is not a complete solution because the two cannot know about the other and this does not really facilitate arbitrary two-way communication (where A calls B then B calls A back). It also makes the code more complicated in that I have two sets of objects and interfaces.
So the solution was to make the template arguments specific to the things I wanted to be flexible. The connectivity interface of A_base and B_base should be constant. Hence that cannot be in the template parameter. It was merely the traits that I wanted to make flexible so... I came up with this solution:
#include <iostream>
template<class aTraitType,class bTraitType>
class A;
template<class aTraitType,class bTraitType>
class B;
[Code] ....
Now this compiles and works great. The problem is that aObj and bObj cannot call their opposite within a trait method because print() does not know anything about the connectivity. So the solution there was to make traits an abstract base class. Then magically everything works!
#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;
[Code] .....
So this outputs the following. Clearly there is two-way communication!
Class A is not connected to B
Class B is not connected to A
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
View 6 Replies
View Related
Dec 18, 2012
I've been making a project that requires different files to have access to objects declared in other files such that circular dependencies are created. I've done some research and discovered that pointers and forward declarations should be able to fix this.
Example:
File 1 declares variable x, must edit x and y
File 2 must edit x and y, declares variable y
I know this isn't the best example, as you could probably declare x and y in the same file, but please suffice it to say that I'm unable to do that in my project.
View 10 Replies
View Related
Jan 30, 2015
I've been making a circular linked list and I'm trying to assign each list a "name" and its next "link" but when I run my code it crashes.
Soldier *Head;
Head = NULL;
string names[] = {"Arman","Bogut","Castro","Damascus","Elene"};
for (int i = 0; i < 5; ++i) {
[Code] .....
View 4 Replies
View Related
Apr 28, 2015
I have this program. I am trying to do this Circular List but i think something going wrong. The first of all is the list.The second is if my code for delete and select function are correct and the third i would like my program getting a "n" number of names and then make the circural list then print it and then when i select a number delete every node until give us the only one left.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define NUM_PER_LINE 6
typedef struct node {
char name[SIZE];
struct node * next;
[Code] .....
View 1 Replies
View Related
Oct 31, 2014
I want to write a program where one core (core 0) will fill the FIFO and other core (core 1 ) will delete the data from FIFO.
Core 0:
I create a fifo
Code:
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
And my en-queue function is in core 0 and writing to specific memory location..(which works perfectly)
Code:
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
[Code] ....
Now the problem is in the core 1. Here I am unable to read the values from the specific memory location. I am getting garbage value. Where I am doing some stupid error.. I did not understand
Code:
(front->ptr) = (unsigned int *) memory_location;
When I print the (front->ptr) it shows correct memory address but inside the De-queue function in core 1, I am getting wrong value..
Code:
int deq(int buf[n]) {
front1 = front;
printf("Val %d ", front->info); // showing wrong value
if (front1 == NULL) {
printf("
Error: Trying to display elements from empty queue");
return 0;
[Code] ...
View 8 Replies
View Related
Jun 28, 2013
/* Implementation of a circular queue of Array containg names.. */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define QSIZE 5
typedef struct{
[Code] ....
I changed my code. but whenever i typed in the ILoveBacolod it takes it as a whole, and if i deleted it deletes the string not the letter. for example:
Enter String: ILoveBacolod
Enter a command: Delete (D)
Output: LoveBacolod
Enter a command: Delete (D)
Output: oveBacolod
Enter a command: Add (A)
Enter a character: z
Output: oveBacolodz
View 2 Replies
View Related
Feb 27, 2014
When I run this in main it gives me a windows error message. I believe it has something to do with my insertAtEnd function but I've gone over it a million times....
#include<iostream>
#include<string>
#include<vector>
#include"RhymeGame.h"
using namespace std;
Game::Game() {
head = NULL;
[Code] ....
View 3 Replies
View Related
Dec 2, 2014
Should my if statement be
if (tail== NULL) or if(tail->next == tail) ?
View 4 Replies
View Related
Apr 25, 2015
I print the original list, then reverse it, then try to print it again. Here is what I get when I do that:
So that second line should print 2 1 4 5
Here is the reverse function:
void reverseCirListDeque(struct cirListDeque *q)
{
struct DLink *curr = q->Sentinel;
do{
[Code].....
View 2 Replies
View Related