and sorry for the noob question.
So I've been asked to create an algorithm to fill a 2D array. They didn't say what the rules were, however, I've been told that the resulting matrix should look like this:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
I haven't been told if the matrix is necessarily square, but the size may vary.
So substantially what I'm seeing is the matrix is vertically, horizontally and diagonally symmetrical.
Now whatever I try, it ends up being super complicated, while as I look at it, I feel like it should be pretty simple...
Any trick or snippet on how you'd do it?
Thanks in advance.
You need 2 nested loops, to traverse through rows and columns. The content of the field is the minimum of the control variables and and the minimum of the difference of a control variable and the size of the array dimension, incremented by 1.
N = 5
0: min(0, N-0-1) + 1 = 1
1: min(1, N-1-1) + 1 = 2
2: min(2, N-2-1) + 1 = 3
3: min(3, N-3-1) + 1 = 2
4: min(4, N-4-1) + 1 = 1
#include <stdio.h>
#define N 5
#define MIN(a,b) (((a)<(b))?(a):(b))
int main()
{
int a[N][N];
for ( int i = 0; i < N; ++ i )
{
for ( int j = 0; j < N; ++ j)
{
int minI = MIN(i, N-i-1);
int minJ = MIN(j, N-j-1);
a[i][j] = MIN(minI, minJ) + 1;
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
See the live example
Related
I have to create a Sudoku grid with the following pattern in C:
1 2 3 4
3 4 1 2
2 3 4 1
4 1 2 3
The first number in the top left corner (here: 1) must be an editable variable for a start value. There is another variable to create the grid with by the square size, in this example the square size is 2 and the 1 2 3 4 are in one square. 3 4 1 2 are in another square and so on...
If the start value is e.g. 3, the grid looks like this:
3 4 1 2
1 2 3 4
4 1 2 3
2 3 4 1
I noticed that there is a pattern: If the row number is odd, the new start value of the next row is the second last one. If the row number is even, the new start value of the next row is the last one. I tried to do it in C, but the even rows are cloning themselves. Note that arrays and pointers are not allowed here, only loops and other control-structures.
I tried the following approach, but the even rows are cloning themselves:
#include <stdio.h>
const int intSquareSize = 2;
const int intFieldLength = intSquareSize * intSquareSize;
int intStartValue = 3;
int main() {
int a = 0;
int b = 0;
int m = 0;
for (int intRowCounter = 1; intRowCounter <= intFieldLength; intRowCounter++) {
m = intFieldLength - 1;
for (int intColumnCounter = 1; intColumnCounter <= intFieldLength; intColumnCounter++) {
a = intStartValue + (intColumnCounter - 1);
b = a;
if (a > intFieldLength) {
a = intFieldLength - m;
m--;
}
if (intRowCounter % 2 == 0 && intColumnCounter == intFieldLength) {
intStartValue = a;
} else if (intRowCounter % 2 == 1 && intColumnCounter == (intFieldLength - 1)) {
intStartValue = b;
}
printf("%d\t", a);
}
printf("\n");
}
return 0;
}
What did I wrong and how can I fix it?
If the row number is odd, the new start value of the next row is the second last one. If the row number is even, the new start value of the next row is the last one.
I don't think it is helpful to think in terms of odd and even. The involved numbers are just symbols, and one could even replace them with distinct colors (for example). Odd/even is not a significant thing here, and it would certainly not play the same role in other board sizes.
The pattern I see is this:
In the first intSquareSize rows, the values shift horizontally (compared to the previous row) by intSquareSize. For example with intSquareSize=3 the first three rows could be:
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
Notice the shift of 3 positions to the left at each next row.
Then the pattern for the next chunks of intSquareSize rows would be the same, but with one shift. So the complete 9x9 sudoku would look like this:
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
2 3 4 5 6 7 8 9 1
This is just one possible pattern you could follow. There could be others. But the following code will apply the above logic. Note I used your variables, but I prefer to use 0-based logic, so loop variables start at 0, and the value of a is also 0-based. Only at the time of printing 1 is added to that value, so it becomes 1-based:
int a = intStartValue - 1; // Move from 1-based to 0-based
for (int intBlockCounter = 0; intBlockCounter < intSquareSize; intBlockCounter++) {
for (int intRowCounter = 0; intRowCounter < intSquareSize; intRowCounter++) {
for (int intColumnCounter = 0; intColumnCounter < intFieldLength; intColumnCounter++) {
printf("%d\t", (a + 1)); // back to 1-based
a = (a + 1) % intFieldLength;
}
printf("\n");
a = (a + intSquareSize) % intFieldLength; // Shift within a block
}
a = (a + 1) % intFieldLength; // Shift between blocks
}
I am attempting to create a minesweeper game whereby the user can choose how many mines to enter, list their co ordinates in a row x column manner at whic point they will be inserted into an array.The program should act as below. However I am unsure as to how I am going to scanf multiple co ordinates into an array. The code I have acquired thus far is available below.
Welcome to minesweeper!
How many mines? 3
Enter pairs:
0 0
1 1
Game Started
2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
int number_mines;
int x;
int y;
printf("Welcome to minesweeper!\n");
printf("How many mines? ");
scanf("%d\n", &number_mines);
// TODO: Scan in the number of pairs of mines.
printf("Enter pairs:\n");
scanf("%d %d\n", &x, &y);
printf("Game Started\n");
for(int i = 0; i < 8; i++){
for(int j = 0; j< 8; j++){
if (x == i || y == j){
printf("2");
}
else{
printf("1");
}
}
}
void shellsort(int v[], int n)
{
int gap, i, j, temp;
for (gap = n/2; gap > 0; gap /= 2)
for (i = gap; i < n; i++){
for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {
temp = v[j];
v[j] = v[j+gap];
v[j+gap] = temp;
}
}
}
}
In this shellsort() function, we've j-=gap. Assuming n = 10, the gap is always 5 and j increments from 0,1,2,3,4....
It means the first 5 times this inner for loop runs, it will return a negative value to j (e.g. 0-5=-5), and thus since j will not be greater than or equal to 0, it will only run once.
It works because that is exactly what we want. We don't want to swap more than once, because if we did, we would only be swapping the same two values over again, thus causing unnecessary redundancy.
So, I was thinking why can't we just omit j-=gap from the loop as it doesn't seem to affect the functioning at all. Is there any special reason why j-=gap is included?
Am I missing something here?
It might help to take a look at insertion sort as a reference to see where this comes from. In insertion sort, we scan from left to right, swapping each element backwards until it's greater than the element that comes before it (or it gets back to the start of the array). The pseudocode for that algorithm is shown here:
for (int i = 1; i < n; i++) {
for (int j = i - 1; j > 0 && A[j + 1] > A[j]; j--) {
swap(A[j], A[j - 1]);
}
}
The outer loop ranges over all elements of the array, saying "put each one in place." The inner loop says "keep swapping the current element with the one that comes before it as long as there is an element that comes before it and that element is greater than it." Here, the use of +1, ++, -1, and -- are because we're constantly looking at the element that comes immediately before the current element.
In shellsort, we run multiple passes of this algorithm over the array, except that we don't use a step size of one. Instead, we use a step size of some amount called the gap amount. Shellsort therefore looks something like this:
for (each gap size) {
for (int i = gap; i < n; i += gap) {
for (int j = i - gap; j > 0 && A[j + gap] > A[j]; j -= gap) {
swap(A[j], A[j - 1]);
}
}
}
The idea is that each element should be continuously compared against the element that's gap elements before it. If it's less than that number, we want to swap it with the preceding element, but then need to repeatedly compare it with the new element that precedes it.
As an example, suppose we are shellsorting this array of length 6:
6 5 4 3 2 1
After the first pass of shellsort (gap = 3), the array looks like this:
3 2 1 6 5 4
Now, imagine that we do the second pass of shellsort with gap = 1. The inner loop currently says "repeatedly swap every element backwards toward the front until it comes to rest." If you remove the j -= gap step from that loop, then every element is just compared against the one directly before it. That would result in the following. In each of these snapshots, the carats refer to where the swaps are looking:
3 2 1 6 5 4 -> 2 3 1 6 5 4
^ ^
2 3 1 6 5 4 -> 2 1 3 6 5 4
^ ^
2 1 3 6 5 4
^ ^
2 1 3 6 5 4 -> 2 1 3 5 6 4
^ ^
2 1 3 5 6 4 -> 2 1 3 5 4 6
^ ^
Notice that the resulting array is not sorted. However, if we put back the j -= gap code into the mix, then the following will happen instead:
3 2 1 6 5 4 -> 2 3 1 6 5 4
^ ^
2 3 1 6 5 4 -> 2 1 3 6 5 4 -> 1 2 3 6 5 4
^ ^ ^ ^
1 2 3 6 5 4
^ ^
1 2 3 6 5 4 -> 1 2 3 5 6 4
^ ^
1 2 3 5 6 4 -> 1 2 3 5 4 6 -> 1 2 3 4 5 6
^ ^ ^ ^
As you can see, now everything gets sorted properly.
This function takes an integer array, the number of elements
in the array and tries to find a majority element in the
array. If the majority element exists, it is placed in
*result and the function returns true.
If no majority element exists, then the function returns
false. In that case, *result should not be used.
My output isn't working correctly for the program I'm writing and it is because of this findMajority function I think.
This is what the output is supposed to look like: http://pastebin.com/Q5ycXHrg
This is what my output looks like: http://pastebin.com/7P1ZTpML
This is the input:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1
1 2 3
1 1 1
1 2 1
1 2
1 1
2
1 1 1 1 2 3 4 5 6 7
Here is the function:
int findMajority(int *array, int count, int *result){
int i, counter, bcount = 0, ccount = 0, candidate, j;
if(count == 1) {
*result = *array;
return true;
}
if(count % 2 != 0 ) {
for(i = 0; i < count; i++) {
if(*(array + i) == *(array + count)) {
counter++;
}
}
if(counter > (count/2)) {
*result = *(array + count);
return true;
}
else {
*(array + count) = 0;
count--;
}
}
for(j=0; j <= count; j += 2) {
if(*(array + j) == *(array + (j + 1))) {
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}
}
if(bcount == 1) {
int k = count;
while(*(array + k) == 0) {
candidate = *(array + k);
}
}
else
findMajority((array + count), count, result);
for(j=0; j <= count; j += 2) {
if(*(array + j) == candidate) {
ccount++;
}
}
if(ccount > (count/2)) {
*result = candidate;
return true;
}
else
return false;
}
Your function has a lot of problem.
Without intialising counter you are incrementing it
check whether array[count] is the valid last element or array[count-1] is the correct one
In this code for(j=0; j <= count; j += 2){
if(*(array + j) == *(array + (j + 1))){
*(array + (count + 1)) = *(array + j);
bcount++;//how many numbers on the end of the array
}}
for count= 3 you are accessing array[4] array[5] etc.
And this is a infinite loop. you are not modifying condition variable inside the loop while(*(array + k) == 0) { candidate = *(array + k); }
I suggest you learn how to use a debugger to debug your program. For example, after wrapping your code with the following:
#include <stdio.h>
typedef enum { false, true } boolean;
// ((( your function here )))
int main(int argc, char* argv[])
{
int result = 0;
int number = 0;
int test[] = { 1, 1, 1, 1, 1, 1, 1 };
result = findMajority(&test[0], sizeof(test) / sizeof(int), &number);
printf("Result = %d, Number = %d\n", result, number);
return 0;
}
Assuming you put this into 'question.c' you could then issue the commands (assuming you have gcc and gdb):
$ gcc -g -o question question.c
$ gdb ./question
(gdb) b findMajority
Breakpoint 1 at 0x80483ea: file question.c, line 6.
(gdb) run
Starting program: ./question
Breakpoint 1, findMajority (array=0xbffff4bc, count=7, result=0xbffff4d8) at question.c:6
6 int i, counter, bcount = 0, ccount = 0, candidate, j;
You can then use the n command to step to the next line, and the p command to print variables to see what's going wrong. For example, you could find some of the problems that Toms pointer out relatively quickly:
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
40 candidate = *(array + k);
(gdb) n
39 while(*(array + k) == 0){
(gdb) n
There's your infinite loop.
(gdb) p counter
$3 = -1207959944
And there's your uninitialized counter.
Part of learning programming is figuring out strategies of determining just what went wrong. Some people like to use text-based debuggers like gdb. Some people like graphical debuggers like you could find in Eclipse CDT. Some people put printf() statements throughout their code.
Once you're really good, like Toms, you can just read it and rattle off the problems. ;-)
Given an array , we need to find the length of longest sub-sequence with alternating increasing and decreasing values.
For example , if the array is ,
7 4 8 9 3 5 2 1 then the L = 6 for 7,4,8,3,5,2 or 7,4,9,3,5,1 , etc.
It could also be the case that first we have small then big element.
What could be the most efficient solution for this ? I had a DP solution in mind. And if we were to do it using brute force how would we do it (O(n^3) ?) ?
And it's not a homework problem.
You indeed can use dynamic programming approach here. For sake of simplicity , assume we need to find only the maximal length of such sequence seq (it will be easy to tweak solution to find the sequence itself).
For each index we will store 2 values:
maximal length of alternating sequence ending at that element where last step was increasing (say, incr[i])
maximal length of alternating sequence ending at that element where last step was decreasing (say, decr[i])
also by definition we assume incr[0] = decr[0] = 1
then each incr[i] can be found recursively:
incr[i] = max(decr[j])+1, where j < i and seq[j] < seq[i]
decr[i] = max(incr[j])+1, where j < i and seq[j] > seq[i]
Required length of the sequence will be the maximum value in both arrays, complexity of this approach is O(N*N) and it requires 2N of extra memory (where N is the length of initial sequence)
simple example in c:
int seq[N]; // initial sequence
int incr[N], decr[N];
... // Init sequences, fill incr and decr with 1's as initial values
for (int i = 1; i < N; ++i){
for (int j = 0; j < i; ++j){
if (seq[j] < seq[i])
{
// handle "increasing" step - need to check previous "decreasing" value
if (decr[j]+1 > incr[i]) incr[i] = decr[j] + 1;
}
if (seq[j] > seq[i])
{
if (incr[j]+1 > decr[i]) decr[i] = incr[j] + 1;
}
}
}
... // Now all arrays are filled, iterate over them and find maximum value
How algorithm will work:
step 0 (initial values):
seq = 7 4 8 9 3 5 2 1
incr = 1 1 1 1 1 1 1 1
decr = 1 1 1 1 1 1 1 1
step 1 take value at index 1 ('4') and check previous values. 7 > 4 so we make "decreasing step from index 0 to index 1, new sequence values:
incr = 1 1 1 1 1 1 1 1
decr = 1 2 1 1 1 1 1 1
step 2. take value 8 and iterate over previous value:
7 < 8, make increasing step: incr[2] = MAX(incr[2], decr[0]+1):
incr = 1 1 2 1 1 1 1 1
decr = 1 2 1 1 1 1 1 1
4 < 8, make increasing step: incr[2] = MAX(incr[2], decr[1]+1):
incr = 1 1 3 1 1 1 1 1
decr = 1 2 1 1 1 1 1 1
etc...