I have been looking around for a solution and I seem to be fixing everything that was wrong with theirs, however mine still doesn't work.
The following code is displaying vectors from a class called Creature, sorting the vectors and then displaying the sorted vectors.
// displaying the unsorted vector
for (int i = 0; i < MAX_NO_CREATURES; i++)
{
if (creatures[i].getAlive() != 0) // checking to see if a creature exists
{
for (unsigned int j = 0; j < creatures[i].getAvaFood().size(); j++)
{
cout << creatures[i].getAvaFood()[j] << " ";
}
}
}
//sorting the vector
for (int i = 0; i < MAX_NO_CREATURES; i++)
{
if (creatures[i].getAlive() != 0) // checking to see if a creature exists
{
creatures[i].bubbleSort(creatures[i].getAvaFood()); // calling a sorting function from creatures[i] passing in the relevant vector (its own.. wait what?)
}
}
cout << endl;
// displaying the sorted vector
for (int i = 0; i < MAX_NO_CREATURES; i++)
{
if (creatures[i].getAlive() != 0) // checking to see if a creature exists
{
for (unsigned int x = 0; x < creatures[i].getAvaFood().size(); x++)
{
cout << creatures[i].getAvaFood()[x] << " ";
}
}
}
When I click retry on the error window visual basic takes me to this line in the creature class. Specifically the IF statement.
void Creature::bubbleSort(vector<int> &v)
{
bool swapped = true;
int temp;
while (swapped)
{
swapped = false;
for (unsigned int i = 0; i < v.size(); i++)
{
if (v[i] > v[i + 1])
{
temp = v[i];
v[i] = v[i + 1];
v[i + 1] = temp;
swapped = true;
}
}
}
}
for (unsigned int i = 0; i < v.size(); i++)
{
if (v[i] > v[i + 1])
The for allows i to be all values [0, v.size) which means you will test v[v.size] which is invalid.
Change your for loop to
for (unsigned int i = 0; i < v.size() - 1; i++)
You will need to add a check that v.size() is not zero.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am writing a C++ code for a Sudoku solver.
The code must work for a 9x9, 16x16 and 25x25 grid. My code only works for a 9x9 grid. I am not sure why. May someone please help me. I think I need to make the 16x16 and 25x25 codes work faster somehow. How do I do this?
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
vector<int> tokenize(string s, string del);
void readAPuzzle(vector<vector<int>> &grid);
void printGrid(vector<vector<int>> grid);
bool isValid(int i, int j, vector<vector<int>> grid);
bool isValid(vector<vector<int>> grid);
bool search(vector<vector<int>> &grid);
int getFreeCellList(vector<vector<int>> grid, vector<pair<int, int>> &freeCellList);
int main()
{
// Read a Sudoku puzzle
vector<vector<int>> puzzle;
readAPuzzle(puzzle);
if (!isValid(puzzle))
cout << "Invalid input" << endl;
else if (search(puzzle)){
printGrid(puzzle);
}
else
cout << "No solution" << endl;
return 0;
}
vector<int> tokenize(string s, string del)
{
vector<int> row;
int start = 0;
int end = s.find(del);
while (end != -1) {
row.push_back(stoi( s.substr(start, end - start)));
start = end + del.size();
end = s.find(del, start);
}
row.push_back(stoi( s.substr(start, end - start)));
return row;
}
void readAPuzzle(vector<vector<int>> &grid){
string line;
getline(cin, line);
vector<int> firstRow = tokenize(line, " ");
grid.push_back(firstRow);
for(int i = 0; i < firstRow.size()-1; i++){
getline(cin, line);
vector<int> row = tokenize(line, " ");
grid.push_back(row);
}
}
/** Obtain a list of free cells from the puzzle */
int getFreeCellList(vector<vector<int>> grid, vector<pair<int, int>> &freeCellList)
{
// 81 is the maximum number of free cells
int numberOfFreeCells = 0;
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid.size(); j++)
if (grid[i][j] == 0)
{
freeCellList[numberOfFreeCells].first = i;
freeCellList[numberOfFreeCells].second = j;
numberOfFreeCells++;
}
return numberOfFreeCells;
}
/** Print the values in the grid */
void printGrid(vector<vector<int>> grid)
{
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid.size(); j++)
cout << grid[i][j] << " ";
cout << endl;
}
}
/** Search for a solution */
bool search(vector<vector<int>> &grid)
{
int k = 0; // Start from the first free cell
bool found = false; // Solution found?
const int n = grid.size();
vector<pair<int, int>> freeCellList(n*n);
int numberOfFreeCells = getFreeCellList(grid, freeCellList);
while (!found)
{
int i = freeCellList[k].first;
int j = freeCellList[k].second;
if (grid[i][j] == 0)
grid[i][j] = 1; // Start with 1
if (isValid(i, j, grid))
{
if (k + 1 == numberOfFreeCells)
{ // No more free cells
found = true; // A solution is found
}
else
{ // Move to the next free cell
k++;
}
}
else if (grid[i][j] < grid.size())
{
grid[i][j] = grid[i][j] + 1; // Check the next possible value
}
else
{ // grid[i][j] is 9, backtrack
while (grid[i][j] == grid.size())
{
grid[i][j] = 0; // Reset to free cell
if (k == 0)
{
return false; // No possible value
}
k--; // Backtrack
i = freeCellList[k].first;
j = freeCellList[k].second;
}
grid[i][j] = grid[i][j] + 1; // Check the next possible value
}
}
return true; // A solution is found
}
/** Check whether grid[i][j] is valid in the grid */
bool isValid(int i, int j, vector<vector<int>> grid)
{
// Check whether grid[i][j] is valid at the i's row
for (int column = 0; column < grid.size(); column++)
if (column != j && grid[i][column] == grid[i][j])
return false;
// Check whether grid[i][j] is valid at the j's column
for (int row = 0; row < grid.size(); row++)
if (row != i && grid[row][j] == grid[i][j])
return false;
int n = sqrt(grid.size());
// Check whether grid[i][j] is valid in the 3 by 3 box
for (int row = (i / n) * n; row < (i / n) * n + n; row++)
for (int col = (j / n) * n; col < (j / n) * n + n; col++)
if (row != i && col != j && grid[row][col] == grid[i][j])
return false;
return true; // The current value at grid[i][j] is valid
}
/** Check whether the fixed cells are valid in the grid */
bool isValid(vector<vector<int>> grid)
{
// Check for duplicate numbers
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid.size(); j++)
if (grid[i][j] != 0)
if (!isValid(i, j, grid))
return false;
// Check whether numbers are in the range
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid.size(); j++)
if ((grid[i][j] < 0) || (grid[i][j] > 9))
return false;
return true; // The fixed cells are valid
}
This is the code I have written so far.
Thank you.
In bool isValid(vector<vector<int>>) you have
if ((grid[i][j] < 0) || (grid[i][j] > 9))
return false;
ie a grid with a number > 9 is never considered valid. I cannot tell if there are other errors, but when you only allow numbers in [0,9] it cannot work for 16x16 or 25x25 sized grids.
You are passing the grid by value to some of the functions. You should pass them as const reference instead to avoid unnecessary copies.
I'm new to C++ and working on the following code (which rotate a given 2d matrix), my code works fine for one rotation. Then I add a while loop to my code to make it more general, I noticed that the while code does not go to more iteration not mater what. I tried to bring the return MatrixRotateOutPut out of the while but then it was unknown. I also tried to add return 0 after while loop but it gives me another error.
I also checked these two links (Link1, Link2), but they werenot really helpful for me.
Thanks for the help in advance.
#include<iostream>
#include<vector>
using namespace std;
int MatrixPrint2D(vector<vector<int>> Input) {
for (int i = 0; i < Input.size(); i++) {
for (int j = 0; j < Input[0].size(); j++) {
printf("%d ", Input[i][j]);
}
printf("\n");
}
}
vector<vector<int>> MatrixRotate(vector<vector<int>> Input, int Irritation) {
while (Irritation > 0) {
cout << Irritation << "\n" << endl;
vector<vector<int>> MatrixRotateOutPut(Input[0].size(), vector<int>(Input.size(), 1));
for (int i = 0; i < Input.size(); i++) {
for (int j = 0; j < Input[0].size(); j++) {
MatrixRotateOutPut[j][Input.size() - 1 - i] = Input[i][j];
}
}
vector<vector<int>> Input(MatrixRotateOutPut.size(), vector<int>(MatrixRotateOutPut[0].size(), 1));
Input = MatrixRotateOutPut;
MatrixPrint2D(MatrixRotateOutPut);
printf("\n");
Irritation--;
return MatrixRotateOutPut;
}
}
int main() {
//Define a matrix for testing:
vector<vector<int>> Matrix2(4, vector<int>(5, 1));
int R = 2;
int C = 4;
vector<vector<int>> MatrixInput(R, vector<int>(C, 1));;
for (int i = 0; i < MatrixInput.size(); i++) {
for (int j = 0; j < MatrixInput[0].size(); j++) {
int temp;
temp = i ^ (2 * j);
MatrixInput[i][j] = temp;
}
}
cout << "MatrixInput:" << endl;
MatrixPrint2D(MatrixInput);
printf("\n");
vector<vector<int>> OutPut2 = MatrixRotate(MatrixInput, 4);
return 0;
}
Edit: I am sorry for this question.
You may do the following:
Move the return outside of the loop
Move the declaration of the returned variable outside of the loop.
So:
vector<vector<int>> MatrixRotate(vector<vector<int>> Input, int Irritation) {
vector<vector<int>> MatrixRotateOutPut;
while (Irritation > 0) {
cout << Irritation << "\n" << endl;
MatrixRotateOutPut = vector<vector<int>>(Input[0].size(), vector<int>(Input.size(), 1));
for (int i = 0; i < Input.size(); i++) {
for (int j = 0; j < Input[0].size(); j++) {
MatrixRotateOutPut[j][Input.size() - 1 - i] = Input[i][j];
}
}
vector<vector<int>> Input(MatrixRotateOutPut.size(), vector<int>(MatrixRotateOutPut[0].size(), 1));
Input = MatrixRotateOutPut;
MatrixPrint2D(MatrixRotateOutPut);
printf("\n");
Irritation--;
}
return MatrixRotateOutPut;
}
I checked SO and saw two issues dealing with the same problem statement. However, neither of them contained what I'm looking for. The task is to output 1 if an array with n elements contains an element that occurs more than n/2 times, using a divide and conquer strategy.
I developed what I think is a working solution based on the fact that the base case is a sub array with one element that is (obviously) the majority element in the subarray. I then proceed to compare these majority elements across subarrays, asking if ultimately they happen more than n/2 times.
For more details, see https://classes.soe.ucsc.edu/cmps102/Fall01/solutions4.pdf (Problem 4)
I wrote two different solutions to this problem, one using a very naive O(n^2) algorithm to check if my solution was correct for all cases, and one attempting to implement the algorithm described in the link above.
Inside main, I stress test my solution against the obviously correct but naive one. However, upon running this I'm getting a SIGABRT (signal 6) error. My debugger tells me to check for malloc, object was probably modified after being freed.
Now I can't tell whether or not my solution is correct. I really don't know what's going on with bad allocation, I'm relatively new to C++.
Code goes below:
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector;
int get_majority_element(vector<int> &a, int left, int right) {
int m;
int majority_left, majority_right; // majority element in either sub array
int count_right = 0, count_left = 0; // count for above
int leftt, rightt; // endpoints
if (a.size() == 1) {
return a[0];
}
else {
m = (left + right)/2; // calculate mid point
vector<int> b_left(m);
vector<int> b_right(right - m);
// get left sub array
for (int i = 0; i < m; i++) {
b_left[i] = a[i];
}
// set new endpoints
leftt = 0;
rightt = m;
majority_left = get_majority_element(b_left, leftt, rightt);
for (int i = 0; i < right - m + 1; i++) {
b_right[i] = a[m+i];
}
leftt = m;
rightt = right - m + 1;
majority_right = get_majority_element(b_right, leftt, rightt);
// if there is a majority element, count its frequency
if (majority_left != -1) {
for (int i = 0; i < a.size(); i++) {
if (a[i] == majority_left)
count_left++;
}
}
if (majority_right != -1) {
for (int i = 0; i < a.size(); i++) {
if (a[i] == majority_right)
count_right++;
}
}
// if both elements in sub arrays are majority and they're different, there is no majority element
if (count_left == count_right && majority_left != majority_right) {
return -1;
}
// check if either sub array has a majority element that occurs more than n/2 times
else if (count_right > count_left && count_right > a.size()/2) {
return majority_right;
}
else if (count_left > count_right && count_left > a.size()/2){
return majority_left;
}
}
return -1;
}
int get_majority_fast(vector<int> &a, int left, int right){
std::reverse(a.begin(),a.end());
int current = 0;
int count;
for (int i = 0; i < a.size(); i++) {
current = a[i];
count = 0;
for (int j = 0; j < a.size(); j++) {
if (a[j] == current)
count ++;
}
if (count > a.size()/2)
return 1;
}
return -1;
}
int main() {
// std::cin >> n;
// vector<int> a(n);
// for (size_t i = 0; i < a.size(); ++i) {
// std::cin >> a[i];
// }
// std::cout << (get_majority_fast(a, 0, a.size()) != -1) << '\n';
while(true){
int one, two;
int n = rand() % 100 ;
std::cout << n << "\n";
vector<int> a;
for (int i = 0; i < n; ++i) {
a.push_back(rand() % 100);
}
one = get_majority_element(a, 0, a.size());
two = get_majority_fast(a, 0, a.size() != -1);
if (one != two) {
std::cout << "Wrong answer: " << one << ' ' << two << "\n";
break;
}
else {
std::cout << "OK\n";
}
}
}
There was effectively an out of bounds error in one of the loops. Corrected loop is below:
for (int i = m; i < right - m ; i++) {
b_right.at(m-i) = a.at(m + i);
}
I thought the weird SIGABRT error was due to something arcane. Guess I learned to use x.at()
Also, I had a couple other mistakes. Full corrected code is below:
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector;
int get_majority_element(vector<int> &a, int left, int right) {
int m;
int majority_left = -1, majority_right = -1; // majority element in either sub array
int count_right = 0, count_left = 0; // count for above
int new_left, new_right; // endpoints
if (a.size() == 1) {
return a[0];
}
else {
m = (a.size())/2; // calculate mid point
vector<int> b_left(m);
vector<int> b_right(right - m);
// get left sub array
for (int i = 0; i < m; i++) {
b_left.at(i) = a.at(i);
}
for (int i = 0; i < a.size() - m ; i++) {
b_right.at(i) = a.at(i + m);
}
// set new endpoints
new_left = 0;
new_right = m;
majority_left = get_majority_element(b_left, new_left, new_right);
new_left = m;
new_right = right - m;
majority_right = get_majority_element(b_right, new_left, new_right);
// if there is a majority element, count its frequency
if (majority_left != -1) {
for (int i = 0; i < a.size(); i++) {
if (a[i] == majority_left)
count_left++;
}
}
if (majority_right != -1) {
for (int i = 0; i < a.size(); i++) {
if (a[i] == majority_right)
count_right++;
}
}
// if both elements in sub arrays are majority and they're different, there is no majority element
if (count_left == count_right && majority_left != majority_right) {
return 0;
}
else if (count_left == count_right)
return majority_left;
// check if either sub array has a majority element that occurs more than n/2 times
else if (count_right > count_left && count_right > a.size()/2) {
return majority_right;
}
else if (count_left > count_right && count_left > a.size()/2){
return majority_left;
}
// majority element in sub arrays isn't majority in array
else
return 0;
}
}
int get_majority_fast(vector<int> &a, int left, int right){
std::reverse(a.begin(),a.end());
int current = 0;
int count;
for (int i = 0; i < a.size(); i++) {
current = a[i];
count = 0;
for (int j = 0; j < a.size(); j++) {
if (a[j] == current)
count ++;
}
if (count > a.size()/2)
return 1;
}
return -1;
}
int main() {
int n;
std::cin >> n;
vector<int> a(n);
for (size_t i = 0; i < a.size(); ++i) {
std::cin >> a[i];
}
int result;
int out;
result = get_majority_element(a, 0, a.size());
if (result != 0)
out = 1;
else
out = 0;
std::cout << out << '\n';
}
I'm sure I could make it a lot prettier but right now I don't want to hear about this for a while.
A randomly generated 4x4 2-D array is given to the user, of which one element is definitely 0. Considering 0 to be an empty location, the user will have to exchange the remaining 15 elements with 0 repeatedly until they get the array in ascending order, with 0 as the last element.
At this point, they're allowed to exchange any element with 0.
But how do I modify this code to ensure that are only able to exchange those elements with 0 that are adjacent to it (either above, below or beside it) ?
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int check_asc(int a[][4])
{
int i, j, previous = a[0][0];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if(i == 3 && j == 3)
{
if (a[i][j] == 0)
return 1;
}
else if (a[i][j] < previous)
{
return 0;
}
previous = a[i][j];
}
}
return 1;
}
void swap(int a[][4], int &xpos, int &ypos)
{
int arr, temp;
cout << "\n\nEnter number to be swapped with 0: ";
cin >> arr;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (a[i][j] == arr)
{
temp = a[xpos][ypos];
a[xpos][ypos] = a[i][j];
a[i][j] = temp;
xpos = i;
ypos = j;
return;
}
}
}
}
int check_rep(int a[][4], int assign)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (assign == a[i][j])
return 0;
}
}
return 1;
}
void main()
{
int a[4][4], assign, xpos = 0, ypos = 0, asc_result, rep_result;
srand((unsigned)time(NULL));
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
if (i == 0 && j == 0)
a[i][j] = 0;
else
{
do {
assign = rand() % 50;
rep_result = check_rep(a, assign);
} while (rep_result == 0);
a[i][j] = assign;
}
}
cout << "\n\nArrange the 4x4 matrix into ascending order. (Consider 0 as a blank space)" << endl;
for (int i = 0; i < 4; i++)
{
cout << endl;
for (int j = 0; j < 4; j++)
cout << a[i][j] << '\t';
}
do {
swap(a, xpos, ypos);
system("cls");
for (int i = 0; i < 4; i++)
{
cout << endl;
for (int j = 0; j < 4; j++)
cout << a[i][j] << '\t';
}
asc_result = check_asc(a);
} while (asc_result == 0);
cout << "\n\tYou win"<<endl;
system("pause");
}
Simple, just extend your swap function with a piece of code that will check whether the location of the element to be swapped is adjacent to the location of 0:
void swap(int a[][4], int &xpos, int &ypos)
{
...
if (a[i][j] == arr &&
((i == xpos && (j == ypos - 1 || j == ypos + 1)) ||
(j == ypos && (i == xpos - 1 || i == xpos + 1))))
{
temp = a[xpos][ypos];
a[xpos][ypos] = a[i][j];
a[i][j] = temp;
xpos = i;
ypos = j;
return;
}
An improvement would be to separate the check condition and inform the user in case when the element is not adjacent to 0.
Rough Algorithm
1) create a function find location, it will return a structure Point that has x, y integer fields, it will find the x, y location of any piece based on the pieces value, i.e. lets say 0 is entered, if it is located in the top left corner (0,0), a point (0, 0) will be returned
2) create a function that takes in 2 points, the location of the '0' and the location of the piece we wish to swap lets call it S, if S.x = 0.x and 0.y - 1 = S.y or S.y - 0.y + 1 then you know that said piece is directly above or below the 0, now of course you have ot add a few conditions for boundaries so as we dont check outside the grid. Have this function return an int 1 if the piece S is located above/below/beside, 0 if not.
3) if 1 is returned your allowed to do the flip, if 0 is returned find another piece
I have an array in c++ with unknown entries (minimum 6) i need a for loop (probably includes a few for loop) which makes 3 groups of 2. I don't care about order of groups or in the group. And tricky part is that double counting is not allowed. I tried something like this but it is obviously triple counts...
for(int i = 0; i < nArray - 1; i++)
{
for(int j = i+1; j < nArray; j++)
{
for(int k = 0; k < nArray - 1; k++)
{
for(int l = k+1; l < nArray; l++)
{
for(int m = 0; m < nArray - 1; m++)
{
for(int n = m+1; n < nArray; n++)
{
if(k!=i && k!=j && l!=i && l!=j && m!=i && m!=j && n!=i && n!=j && m!=k && m!=l && n!=k && n!=l)
{
std::cout << array[i] << "-" << array[j] << std::endl << array[k] << "-" << array[l] << std::endl << array[m] << "-" << array[n] << std::endl << std::endl;
}
}
}
}
}
}
}
Edit: for example let our array is {1,2,3,4,5,6} which has 6 entries. Output should look like:
12-34-56
12-35-46
12-36-45
13-24-56
13-25-46
13-26-45
14-23-56
14-25-36
14-26-35
15-23-46
15-24-36
15-26-34
16-23-45
16-24-35
16-25-34
But there should not be 12-43-56 or 34-12-56 since there is 12-34-56.
And for the array {1,2,3,4,5,6,7}
12-34-56
12-34-57
12-34-67
12-35-46
12-35-47
12-35-67
...
and so on.
Make it recursively. Choose two elements from the array to form a pair, remove them from the array and call the same function on reduced array. That way you'll have only two nested loops and you'll cover the whole array.
Alternatively, use additional array with information, which elements were already used instead of removing them from original array.
void GeneratePairs(std::vector<int> & values, std::vector<bool> & used, std::vector<std::pair<int, int>> & results)
{
int i = 0;
while (i < values.size() && used[i])
i++;
if (i != values.size())
{
used[i] = true;
for (int j = i + 1; j < values.size(); j++)
{
if (!used[j])
{
used[j] = true;
std::pair<int, int> tmp(values[i], values[j]);
results.push_back(tmp);
GeneratePairs(values, used, results);
results.pop_back();
used[j] = false;
}
}
used[i] = false;
}
else
{
for (int i = 0; i < results.size(); i++)
{
printf("%d,%d", results[i].first, results[i].second);
if (i < results.size() - 1)
printf("-");
}
printf("\n");
}
}
// (...)
int main(int argc, char * argv)
{
std::vector<int> input;
input.push_back(1);
input.push_back(2);
input.push_back(3);
input.push_back(4);
input.push_back(5);
input.push_back(6);
std::vector<bool> used;
for (int i = 0; i < input.size(); i++)
used.push_back(false);
std::vector<std::pair<int, int>> results;
GeneratePairs(input, used, results);
getchar();
}
I just want to mention I have figured out the simple detail which solves this problem:
for(int i=0;i<input.size();i++)
{
for(int j=i+1;j<input.size();j++)
{
for(int k=i+1;k<input.size();k++)
{
for(int l=k+1;l<input.size();l++)
{
if( j==k || j==l) continue;
for(int m=k+1;m<input.size();m++)
{
for(int n=m+1;n<input.size();n++)
{
if( m==j || m==l || n==j || n==l) continue;
std::cout << input[i] << input[j] << "-" << input[k] << input[l] << "-" << input[m] << input[n] << std::endl;
}
}
}
}
}
}