C++ :: Next Permutation Logical Error
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
ADVERTISEMENT
Nov 15, 2013
My project is on an online shop in which you can manage a shop(adding ,deleting ,clearing and modifying items) also you can switch to different shops(all shops are same)
Code:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
[Code] ....
The logical problem is serious because i am not able to find the error!!! There is some problem with display lines in void shop()-->if(mode=='s') and if(mode=='d'). I add only one item but in output screen i see two items(sometimes >2)
*note* to run the above display lines in the output screen....first register--->add atleast one item--->see your shop
View 12 Replies
View Related
Oct 31, 2014
I have to write a program that does the following: Monkey Business A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day during a typical week. Write a program that stores this information in a two-dimensional 3 - 7 array , where each row represents a different monkey and each column represents a different day of the week. The monkeys are represented by integers 1, 2, and 3; the weekdays are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday". The program should first prompt the user to input the data for each monkey, starting with "Sunday" for monkey #1, then monkeys #2 and #3, followed by "Monday" for monkey #1, then monkeys #2 and #3 and so on, through "Saturday". The program then creates a report that includes the following information, each properly labeled (see below):
-Average amount of food eaten per day by the whole family of monkeys.
-The least amount of food eaten during the week by any one monkey.
-The greatest amount of food eaten during the week by any one monkey.
Input Validation: Do not accept negative numbers for pounds of food eaten. When a negative value is entered, the program outputs "invalid (negative) food quantity -- re-enter" and attempts to reread the value . Prompts And Output Labels: Decimal values should be displayed using default precision, i.e. do not specify precision. Each item read should be prompted for by a string of the form "Enter the food eaten by monkey #N on DAY:" when N is 1 or 2 or 3 and DAY is "Sunday" or "Monday" or ... or "Saturday".
The output should be of the form:
Average food consumed daily: 6.23
The least daily food consumed was by Monkey #0 on Friday
The most daily food consumed was by Monkey #2 on Sunday where the specific amount of food or specific monkeys or specific days identified depend on the actual input.
However after the process of resolving the errors unable to convert to standard string and unresolved externals I now have a logical error that causes the output of the DAYNAME variable to output as what appears to be hex in the following picture:
How I can fix this and why it's casting them as this in the output.
My full code is as follows because I don't know exactly, which line of code is causing this to happen:
Code:
//
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// constants
const int MONKEYS = 3;
const int DAYS = 7;
[Code] .....
View 5 Replies
View Related
Jul 16, 2013
I've encountered a slight logical error in my code
/*
Lab06_pensionplans.cpp
Purpose :
- Create a simple financial application to calculate retirement plans
*/
#include <iostream>
#include <cstdlib>
using namespace std;
void displayMenu() {
system("cls");
[Code] ....
Look at case 2, which the user supposed to key in a new input, the problem is the value will never got into main function, I don't know what should I modify with the function.
Figured out I need to change the
void changeData(int startingAge, int numOfYears,
double lumpSumAmount, double yearlyAmount, double interestRate )
into
void changeData(int &startingAge, int &numOfYears,
double &lumpSumAmount, double &yearlyAmount, double &interestRate )
View 2 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
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
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
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
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
Dec 20, 2013
I wrote this program using an online compiler i am getting a lot of errors.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string birthmonth;
string birthday;
[Code] ....
View 6 Replies
View Related
Mar 9, 2013
I've received a segmentation fault in this part of code:
Code:
for (; str[i] != '