I'm making Sudoku validater program that checks whether solved sudoku is correct or not, In that program i need to compare multiple variables together to check whether they are equal or not...
I have provided a snippet of code, what i have tried, whether every su[][] has different value or not. I'm not getting expecting result...
I want to make sure that all the values in su[][] are unequal.
How can i achieve the same, what are mistakes in my snippet?
Thanks...
for(int i=0 ; i<9 ;++i){ //for checking a entire row
if(!(su[i][0]!=su[i][1]!=su[i][2]!=su[i][3]!=su[i][4]!=su[i][5]!=su[i][6]!=su[i][7]!=su[i][8])){
system("cls");
cout<<"SUDOKU'S SOLUTION IS INCORRECT!!";
exit(0);
}
}
To check for each column uniqueness like that you would have to compare each element to the other ones in a column.
e.g.:
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
for (int k = j + 1; k < 9; ++k) {
if (su[i][j] == su[i][k]) {
system("cls");
cout << "SUDOKU'S SOLUTION IS INCORRECT!!\n";
exit(0);
}
}
}
}
Since there are only 8 elements per row this cubic solution shouldn't give you much overhead.
If you had a higher number N of elements you could initialize an array of size N with 0 and transverse the column. For the i-th element in the column you add 1 to that elements position in the array. Then transverse the array. If there's a position whose value is different from 1, it means you have a duplicated value in the column.
e.g.:
for (int i = 0; i < N; ++i) {
int arr[N] = {0};
for (int j = 0; j < N; ++j)
++arr[su[i][j] - 1];
for (int i = 0; i < N; ++i) {
if (arr[i] != 1) {
system("cls");
cout << "SUDOKU'S SOLUTION IS INCORRECT!!\n";
exit(0);
}
}
}
This approach is way more faster than the first one for high values of N.
The codes above check the uniqueness for each column, you would still have to check for each row.
PS: I have not tested the codes, it may have a bug, but hope you get the idea.
Related
I am trying to make a program that sorts an array without using the sort function (that won't work with objects or structs). I have made the greater than one work, but the less than one keeps changing the greatest element in the array to a one and sorting it wrong, and when used with the greater than function, the first element is turned into a large number. Can someone please help me fix this or is it my compiler.
void min_sort(int array[], const unsigned int size){
for(int k = 0; k < size; k++) {
for(int i = 0; i < size; i++) {
if(array[i] > array[i+1]){
int temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
}
}
You are not looping correctly. Looks like you are trying bubble sort which is:
void min_sort(int array[], const unsigned int size){
for(int k = 0; k < size; k++)
for(int i = k+1; i < size; i++)
if(array[i] < array[k]){
int temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
void min_sort(int array[], const unsigned int size)
{
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-1-i;j++)
{
if(array[j]>array[j+1])
{
swap(array[j] , array[j+1]);
}
}
}
}
I see that you are trying to implement the bubble sort algorithm. I have posted the code for bubble sort here. In bubble sort you basically compare the element at an index j and the element next to it at index j+1. If array[j] is greater than array[j+1] , you swap them using the swap() function or by using the temp method. The outer loop will run size - 1 times , and the inner loop will run size - 1 - i times because the last element will already be in place.
For Example we have an array of size 4 with elements such as :
array[i] = [100,90,8,10]
The bubble sort will sort it in the following steps :
90,100,8,10
90,8,100,10
90,8,10,100
8,90,10,100
8,10,90,100
8,10,90,100
See, the use of size-1-i . You can see the nested loop runs less number of times in each iteration of the outer loop.
There is only one mistake that your 2nd loop condition should be: i < size -1.
So it should be:
for (int i = 0; i < size -1; i++)
Your attempt at bubble sort is basically correct, you just have an out of bounds issue with your inner loop. During the inner loop's last run, i == size - 1, therefore i + 1 is equal to size, thus data[i+1] is out of range. Simply change the condition of your for to be i < size - 1.
Working example: https://godbolt.org/z/e5ohWPfTz
I'm currently writing a tetris in C++ and I am at the last stage, I need to delete a row when it is full. Once a piece falls it is stored in a boolean array grid[20][10]. For example I check which row is full (or true), if it is I call a method deleteRow, where n is a number of row:
void Grid::deleteRow(int n)
{
for (j = 0; j < WIDTH; j++)
{
grid[n][j] = false;
}
}
Once the row is deleted I call a method moveRowDown:
void Grid::moveRowDown()
{
for (i = 0; i < HEIGHT; i++)
{
for (j = 0; j < WIDTH; j++)
{
grid[i+1][j]=grid[i][j];
}
}
}
So this method does not work, and all of the pieces disappear. I know I am missing the logic. Thanks for the help in advance!
They disappear because you copy 1st empty row to 2nd, then to 3rd and etc.
You need to rewrite your first loop in Grid::moveRowDown() to work from the bottom of a glass:
for (i = HEIGHT-2; i>=0; i--)
I am trying to create a function that will find the intersection of two dynamically allocated arrays comparing array 1 to array 2. For any values in array 1 that are not in array 2, those values should be deleted in array 1 so that array 1 now only holds the common values of both arrays (no repeats). I cannot use vectors, hashes, or any other thing outside of my current functions in my class:
here is my code so far:
bool IntSet::contains(int val) const
{
for (int i = 0; i < numValues; i++)
{
if (set[i] == val)
return true;
}
return false;
}
this function compares an integer parameter to values currently stored in the array...if a value is in the array it returns true and if else false;
this next function takes in a value and removes that value from the array:
void IntSet::remove(int val)
{
for (int i = 0; i < numValues; i++)
{
if (set[i] == val)
for (int j = 0; j < numValues; j++)
set[j] = set[j + 1];
}
numValues--;
}
here's where I've been having problems, this next function is supposed to iterate through one array and compare those values with the values in the other array...if one value from one array is in the other, it should just skip it, but if a value is not in the array calling the function, it should delete that value from the calling array:
void IntSet::removeDifferent(const IntSet &set2)
{
for (int i = 0; i < set2.size(); i++)
{
if (!set2.contains(set[i]))
{
remove(set[i]);
}
}
}
ive tried about 50 different variations on the removeDifferent() function and I just can't seem to figure this one out. Could someone point me in the right direction?
You're iterating i through the indexes of set2, but then you're testing set[i]. Try this:
void IntSet::removeDifferent(const IntSet &set2)
{
for (int i = 0; i < numValues; ) {
if (!set2.contains(set[i])) {
remove(set[i]);
} else {
i++;
}
}
Note that I also removed i++ from the for loop header. This is because when you remove an element, all the following elements are shifted down, so the next element takes its place in the array. If you incremented i, it would skip that element.
You also need to fix remove. It should start its inner loop from i, so it only shifts down the elements after the one being removed, and it should stop at numValues-1, so it doesn't try to access outside the array when it copies set[j+1]. And as an optimization, it can break out of the outer loop once it has found a match (I assume IntSet doesn't allow duplicates, since you only decrement numValues by 1).
void IntSet::remove(int val)
{
for (int i = 0; i < numValues; i++)
{
if (set[i] == val) {
for (int j = i; j < numValues - 1; j++) {
set[j] = set[j + 1];
}
break;
}
}
numValues--;
}
Your problem is in your remove() function:
void IntSet::remove(int val)
{
for (int i = 0; i < numValues; i++)
{
if (set[i] == val)
for (int j = 0; j < numValues; j++)
set[j] = set[j + 1];
}
numValues--;
}
You can figure out yourself why this is wrong by using a paper and pencil here. Start with a typical example: let's say you found the value you're looking for in the third element of a five-element array:
if (set[i] == val)
In this example, i would be set to 2, and numValues would be set to five. It doesn't matter what val is. Whatever it is, you found it when i is 2, and numValues is five: you found it in the third element of a five element array. Keep that in mind.
Now, you know that you are now supposed to remove the third element in this five element array. But what do you think will happen next:
for (int j = 0; j < numValues; j++)
set[j] = set[j + 1];
Well, using the aforementioned paper and pencil, if you work it out, the following will happen:
set[1] will be copied to set[0]
set[2] will be copied to set[1]
set[3] will be copied to set[2]
set[4] will be copied to set[3]
set[5] will be copied to set[4]
There are two problems here:
A) There is no set[5]. Recall that this is a five-element array, si you only have set[0] through set[4]
B) You're not supposed to copy everything in array down to one element. You have to copy only the elements after the element you want to remove.
Fix these two problems, and you will probably find that everything will work correctly.
The following function takes three word objects and checks each word's letter coordinates (in a table) against each other. The idea is to get combinations of three words from a list that don't have intersecting letter coordinates. However when you have over 600000 possible combinations this becomes very time consuming.
bool lettersIntersect(word one, word two, word three)
{
for(int i = 0; i < one.getLength(); i++)
for(int j = 0; j < two.getLength(); j++)
if(one.getLetterPosition(i).x == two.getLetterPosition(j).x && one.getLetterPosition(i).y == two.getLetterPosition(j).y)
return true;
for(int i = 0; i < two.getLength(); i++)
for(int j = 0; j < three.getLength(); j++)
if(two.getLetterPosition(i).x == three.getLetterPosition(j).x && two.getLetterPosition(i).y == three.getLetterPosition(j).y)
return true;
for(int i = 0; i < three.getLength(); i++)
for(int j = 0; j < one.getLength(); j++)
if(three.getLetterPosition(i).x == one.getLetterPosition(j).x && three.getLetterPosition(i).y == one.getLetterPosition(j).y)
return true;
return false;
}
Is there a more efficient way of doing this?
I can just give you 1 hint which striked me instantly. Don't blame me if its misleading. You can just try once at your end and see the performance.
Create map (use stl) for each word objects i.e. map_one, map_two, and map_three
Add co-ordinate value as key for each letter of a given word object to its respective map.
Then check using these maps whether there is an intersection.
Check if map in C++ contains all the keys from another map
The only thing I see possible to optimize is avoiding double checks:
for(int i = 0; i < one.getLength(); i++)
for(int j = i+1; j < two.getLength(); j++)
if(one.getLetterPosition(i).x == two.getLetterPosition(j).x && one.getLetterPosition(i).y == two.getLetterPosition(j).y)
return true;
The second for loop was changed from j = 0, to j = i+1, which makes you do the checks in half the time.
Checking between two coordinates points is a n^2 (n-square) problem, which means that the time required to do the checks is proportional to square the number of elements you can check. I thikn there's no other way to optimize this other than avoiding double checks, like I explained.
Of course, in addition to passing by references, like was suggested to you already.
On this homework problem or other learning exercise, you are intended to use a method you have been taught previously that rearranges data to make searching faster. Having rearranged the data, you should be able to find a way to scan it efficiently to find duplicates.
I've got an fstream input file that has [N] lines or items. I've written code to decide which items are triangles and which are rectangles and which are circles. I've got to isolate just the triangle items and then compare them to see if they are equal to +/- 0.1 the area of all the other triangle items. Then I have to cout the equal pairs of items as uppercase char letters.
Here's my code so far but it's not working correctly. It's outputting L&L, which is incorrect. It should say E&L because the two identical triangles in my array are on lines 5&12, Not 12&12. How do I fix this?
int ItmM = 0;
ItmN = 0;
int j = 0;
for (int i=0; i<M; i++)
{
if (btype[i] == Triangles)
{
TA[i] = (0.5 * (D[i] * E[i]));
for (int j=0; j<i; j++)
{
if (TA[i] - 0.1 < TA[j] && TA[j] < TA[i] + 0.1)
{
TA[j] = TA[i];
ItmM = i;
ItmN = j;
cout << "4. Triangular blocks that are the same size = "
<< (char)('A' + ItmM) << "&" << (char)('A' + ItmM)
<< endl;
}
}
}
}
I've edited the above code twice. It's still outputting L&L. Should be E&L, (5&12)
When you do TriangleAreaE = TrangleArea[j], and as j is always0, you are comparing every triangle with the first one. The 5th and 12th never get compared.
You should use a nested loop to compare every pair of triangles:
for (int i=0; i<M; i++)
{
if ( /*...*/ )
{
/*...*/
for (int j=0; j<i; j++)
{
// compare TriangleArea[i] with TriangleArea[j], if j is a triangle
}
}
}
You can fill in the detail, as this seems to be a homework problem.
And you should edit your existing posts instead of posting a new one.
The value of j never changes in the code you posted. You just compare the area of each triangle with the first one in the list. If I understand what you are trying to do correctly, you should be able to make it work with a nested for loop:
TriangleArea[0]= whatever
for(j=0; j<M; j++)
{
for(i=j+1;i<m;i++)
{
// rest of your code
In your code you never modify j, so you are always comparing with the first triangle (triangle A), so it's not really a suprise that it is equal to itself. What you need to do is loop over all already processed triangles and compare, so basically
for (int i=0; i<M; i++)
{
if (btype[i] == Triangles) //btype[i] declared earlier in larger code
{
TriangleArea[i] = (0.5 * (D[i] * E[i]));
for(int j = 0; j < i; ++j)
{
//Insert the comparison here
}
}
}
Of course your positioning of the cout will only print the last pair of equal sized pair, so judging from the text in your question, you might want to put the cout inside your loops.
(char)('A' + ItemNumberM) << "&" << (char)('A' + ItemNumberM)
This line prints the same item number twice. Is one of those supposed to be ItemNumberN?