Shoertening this program, help please

//Permutation program
#include <iostream>
#include <vector>
#include<time.h>
using namespace std;

void main()
{
int len; //set asside space for input
vector <int> perm; //set asside space for permutation
srand(time(NULL));
cout <<"Welcome to the Random Permutation Generator Program\n\n Please enter your length for your permutation: ";
cin >> len; //sets the length for the permutation
cout << "\n\n";
for(int i=0; i<len;i++) //sets a generic sequence for the code
{
perm.push_back(i+1);
}
for(int i=0; i<len; i++)
{
int temp = perm[i]; //stores integer i in a temporary place
int j = rand()%(i+1); //selects a random digit
perm[i] = perm[j];
perm[j] = temp;
}
cout << "Here's your Random Permutation of length " << len << ":\n";
for(int i=0; i<(len-1); i++) //displays each position except the last with a comma and a space in between
{
cout << perm[i] << ", ";
}
cout << perm[len-1] << "\n\n"; //this displays the last position
system("pause");
}
Last edited on
from start of void main()
to the end we are supposedly able to get it down to 16 lines in the {} but we have 18
Delete:
 
cout <<"Welcome to the Random Permutation Generator Program\n\n Please enter your length for your permutation: ";


&

 
system("pause");
Without changing any of your logic, I pretty much just cleaned up your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main() {
    int len;
    vector<int> perm;
    srand(static_cast<int>(time(NULL)));
    std::cout << "Welcome to the Random Permutation Generator Program\n\n Please enter your length for your permutation: ";
    std::cin >> len; std::cout << "\n\n";
    
    for( int i = 0; i < len; i++ )
        perm.push_back(i+1);
    
    for( int i=0; i<len; i++ ) {
        int temp = perm[i];
        int j = rand()%( i + 1); 
        perm[i] = perm[j]; perm[j] = temp;
    }
    
    std::cout << "Here's your Random Permutation of length " << len << ":\n";
    
    for(int i = 0; i < (len-1); i++ )
        std::cout << perm[i] << ", ";
        
    std::cout << perm[len-1] << "\n\n"; //this displays the last position
    
    // system("pause"); Do not use this. EVER
    cin.ingore();
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Permutation program
#include <iostream>
#include <vector>
#include<time.h>
using namespace std; //try not to include entire namespaces--ok for now

int main() //the standard--main should return an integer
{
	srand(time(NULL));
	int len;
	vector <int> perm;

	cout <<"Welcome to the Random Permutation Generator Program.\n\nPlease enter your length for your permutation: ";
	cin >> len;

	for(int i=0; i<len;i++) perm.push_back(i+1); //for-loops don't need brackets if they're doing 1 thing

	for(int i=0; i<len; i++) {  //randomly swap numbers--how about 'random_shuffle(perm.begin(), perm.end())' instead? 
		int j = rand()%(i+1);
		int temp = perm[i];
		perm[i] = perm[j];
		perm[j] = temp;
	}

	cout << "\n\nHere's your Random Permutation of length " << len << ":\n"; //moved the first two endline characters here--same output
	for(int i=0; i<(len-1); i++) cout << perm[i] << ", "; //as above, no brackets needed
	cout << perm[len-1] << "\n\n";

	system("pause"); //Look into cin.ignore(numeric_limits<streamsize>::max(), '\n') with cin.get()
	return 0; //the standard
}
Topic archived. No new replies allowed.