C++ :: How To Generate All Permutations Of Objects Present On List
Mar 13, 2014
I've a big problem. I'd like to generate of all permutations of objects present on my list and i don't know how to do this. I've found a few examples, but i can't use them in my code...
I know how to generate all permutations of a list like: {'h','e','k','p'}
But I am needing to know how to generate all permutations like: {{'a','b'}, {'h','k','d'}, ..... } such that the first character is either an 'a' or a 'b' and the second character is one of the three in the second list, etc. There will be less than 600 000 permutations.
I am trying to generate some lists of permutations of ints but I can't make std::next_permutation work for me. The problem is I need to include permutations which don't use every number. For example take the array of numbers [1, 2]. I need an algo that will return:
where *toOrigList is a pointer to a list and *answer is an array of lists. intListType has several member functions and I will post them if you want them, but for the most part they are barely used in this function (which is not a member function). I'm having trouble outputting the permutations because the program keeps crashing. I cannot change the specification of the function. Anyway, here is what I have of the function so far:
#include "allPerms.h" #include <string> #include <cstddef> #include <iostream> using namespace std; void allPerms(intListType *toOrigList, intListType *answer) //Pre: list is not empty //Post: computes all permutations of a given list
Is there any way to generate random types I build? I know rand command returns random integers, but I want to create some classes and and a function that will generate randomly 3 objects of the types I have created.
I need to create a list of all members of a struct using the sturct definition as input. The struct is NOT part of the program, but is the input to the program.
The struct that I am using changes over time as features are added. I need the complete, fully qualified field names to then generate a table with all names with their offsets, type and length.
This information then allows me to create readers of the data that will run on different architectures, compilers and operating systems.
The struct currently has 800+ lines and uses typedef and embedded structs.
Perhaps this could be done creating LEX, YACC, Perl, SED, AWK or other language system.
I am just starting out programming and have an assignment due in the near future. the problem is I cant seem to figure it out. I started it already but got stuck cant seem to figure it out. Here is the assignment.
Create a program that will generate a list of 200 random numbers (ranging from 1-1000) and determine the medium, mode, and average of the list of numbers. Have the program display the original list ant then display the list in ascending and descending order on the screen.
I have a data structure defined up here called this:
typedef list <classSpec*> ClassSpecList;
I'm trying to add stuff into the list here based on functions that return certain values of that match the same data type. In one function, I have a list pointer object defined here and I have another statement that calls a function.
I have a standard linked list in template format (with T being the typename) with the following relevant insertion operators:
bool PushFront (const T& t); // Insert t at front of list bool PushBack (const T& t); // Insert t at back of list
I want to build a list of pointers to objects (we'll call them objects of class Box for simplicity) that is member data for a parent class (we'll call that class Warehouse). However, I want to have a constant Box....const Box INVISIBLE_BOX = Box(); that represents a nonexistent Box. However, I cannot use either
PushFront(&INVISIBLE_BOX) or PushBack(&INVISIBLE_BOX)
to add INVISIBLE_BOX to the list of boxes without getting an invalid conversion error invalid conversion from âconst warehouse::Box*â to âwarehouse:: Box*â [-fpermissive]
What can I do to make it take a constant Box? I am using g++ to compile these files.
We are considering a word C containing only lowercase. An anagram of the word C is a word formed by the letters of C in another order eventually. E.g.
'armata' is an anagram of the word 'tamara' but 'maree' is not an anagram of the word 'amare' A word is an angram of itself.
Determine the number of different anagrams that a word has. Because this number may be very high, there will be posted its decomposition into prime numbers
The input file anagrame.in contains the word C.
The output file anagrame.out will contain the decomposition into prime numbers of the number of anagrams of the word C. On each line there will be a prime factor followed by one space and then its multiplicity(power). The factors will be written in ascending order.
there is a file contains only a numbers ,we dont know how many numbers present in that file.so i want a program to display top n largest number present in that fie.(n may be 5,10,12 like that.)
I can't seem to figure out the algorithm to find the right permutation(s) of operands and operators.
We basically have a list of 6 unsigned integers. Using arithmetic operations (addition, subtraction, multiplication, division), find the arithmetic expression that evaluates to a target integer.
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code: string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND"); while (getline( input_file, line )) { std::size_t guard_found = line.find(guard_str);
This is my program, for now it is intended to compute permutations of any string entered, but the function ox will not return the final x value. ox is the function that actually computes the permutations so the return of the x value is critical.
Code: #include<stdio.h> #include<string.h> int ox(int x); int main() { int x; char input[10];
I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)
When printed out, the names are formatted as follows:
lastname, firstname: 90 lname, fname: 100 last, first: 60
However, when I print them out this is what I get:
lastname, firstname: 90 lname, fname: 100$ last, first: 60H
For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.
Code: /* - Prints a single record stored in the file pointed to by ifp. */ void print_record(FILE *ifp, int record) {
This has been bothering me for a while now, and I finally put together an example:
#include <iostream> #include <string> using namespace::std;
[Code]....
In the code above, the two classes hold pointers to each other, and that's fine but it doesn't seem right since C++ prefers to pass by reference. Yes, it can still do that (see testbox and testball) but even that seems odd to me because still you need to use pointer notation for the enclosed object. Am I the only one who feels this way, and should I just get over it? Or am I missing something that would allow an object to hold a reference?
I'm trying to generate random numbers so that I can assign people to teams. So far I have come up with this
Code:
int generateTeam(){ int i, teamNumber, c, n; for (c = 0; c <= 5; c++) { n = rand()%100 + 1; }
[code]....
}//end generateTeam I'm not sure how to make it so that I can exclude the previous random number when generating the next one. As an example, I have 22 students and I get the number 19. Now I can't have 19 again because that guy already has it.
I am a very very beginner at programming with C. Well, basically i have to generate a real number between 0 and 1 (which as the same as from 0 to 100 k and than dividing everything with 100 k).
Why I am constantly talking about 100 k? Because I would need approx 50 000 random numbers between 0 and 1. My code currently looks something like this:
Code:
int main(int argc, char** argv) { int min,max; double number; srand((unsigned)time(NULL)); number = 1 + rand()%100; printf("The number is: %lf",number); sleep(2); return (EXIT_SUCCESS); }
And If I am not mistaken, should generate numbers between 0 and 100. But I can't figure it out how to change to code in order to get enough numbers.