All of this is in Common LISP Define a function to build a list of integers from
ID: 3744526 • Letter: A
Question
All of this is in Common LISP
Define a function to build a list of integers from 1 to n, then write a function to create a list of combinations of pairs of integers. You can use either n as the input or the list of integers as input.
This would look like ((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3)) for n = 3.
Then use this function on itself to get a list of all possible permutations.
The new resultant list from entering the previously created list would look like:
((1 1) (1 1) (1 2) (1 2) (1 3) ( 1 3) (2 1)... etc for n = 3
It would be very helpful if you wrote comment lines, explaining important lines of code, as well as variables/input labels.Help would be much appreciated!
Help would be much appreciated!
Explanation / Answer
//I write code for generating all possible pair and explaining my code.
#include <iostream>
#include <vector>
using namespace std;
// function to generate a list from the number which user enter
std::vector<int> getistOfInteger(int value)
{
//creating a vector and populating value inside it
std::vector<int> list;
for(int index = 0; index < value; ++index)
{
//filling the vector
list.push_back(index+1);
}
//return list
return list;
}
// function to generate a list of pair
std::vector<std::pair<int, int>> getPairList(int value)
{
//creating a vector which has a list of integer
std::vector<int> listOfInteger;
listOfInteger = getistOfInteger(value);
// create a vector of pair which has list in form of pair
std::vector<std::pair<int, int>> listOfIntegerPair;
//populating pair inside vector
for(int i = 1; i <= value; ++i)
{
for(int j=1; j<=value; ++j)
{
std::pair<int, int> integerPair = std::make_pair(i,j);
listOfIntegerPair.push_back(integerPair);
}
}
return listOfIntegerPair;
}
int main() {
// define a number
int number;
// lets user enter the number
cout << "Enter a number: ";
cin >> number;
// calling the function to get the combination of pairs
std::vector<std::pair<int, int>> listOfIntegerPair = getPairList(number);
// display list of pair
cout << "( ";
for(pair<int, int> pair: listOfIntegerPair)
{
cout << "(" << pair.first << ", " << pair.second <<") ";
}
cout << " )" << endl;
return 0;
}