Segmentation fault error with my minesweeper code in C - c

So I have been trying to solve this problem where I have to write a code that counts the number of mines that are around a certain point on a imaginary minefield. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int row;
int col;
int count;
int mineCount = 0;
int i;
int j;
// allocating memory
scanf("%d %d", &row, &col);
char **m = malloc(sizeof(char* ) * row);
for(i = 0; i < row; i ++)
{
m[i] = malloc(sizeof(char) * (col + 1)); //empty minefield
}
//planting mines
for(count = 0; count < row; count ++)
{
scanf("%s", m[count]);
}
// counting mines
for(i = 0; i < row; i ++)
{
for(j = 0; j < (col + 1); j ++)
{
if(m[i][j] != 42)
{
if(m[i-1][j] == 42)
mineCount += 1;
if(m[i+1][j] == 42)
mineCount += 1;
if(m[i][j+1] == 42)
mineCount += 1;
if(m[i][j-1] == 42)
mineCount += 1;
if(mineCount > 0)
m[i][j] = mineCount;
}
}
}
//printing out the minefield
for(i = 0; i < row; i ++)
{
for(j = 0; j < col; j ++)
printf("%c", m[i][j]);
printf("\n");
}
return 0;
}
I put '5 5' for the first input, and for the minefield, my input would be something like this:
*....
..*..
...*..
.*...
....*
at the end though, I get this 'segmentation fault (Core dumped)' error. I have been searching around for answers and found out that this happens when I try to access something that I have no access to. Any help would be appreciated. Thanks.

if(m[i-1][j] == 42)
if(m[i][j-1] == 42)
For these statements you need to have checks if i and j are not 0, otherwise you are accessing invalid memory

Third input of ...*.. // 6 char long will make
//planting mines
for(count = 0; count < row; count ++)
{
scanf("%s", m[count]);
}
to buffer overflow and write an extra . to out-of-bound memory.
Also, I don't see any free, assuming you must have implemented that.

Have a look at if(m[i+1][j]). If i=row-1 this will be equal to row. But as you only allocated row values and the addressing starts at 0 row-1 is the highest value, which you are able to address.
And also at if (m[i-1][j]) and if(m[i][j-1]) when i or j is zero respectively.

Related

Removing duplicates in a C array

I am writing a program which determines the intersection of 2 integer arrays (size of 10 elements). I think I got every other parts covered except for sorting out duplicates. Does anyone know a way of checking duplicates without making a function or using an external C library?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '\0';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '\0')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
In the code above i was trying one method although it didn't work and instead made the program stuck after inputting all the elements. I am open to other methods as long it doesn't require an external library to run. Thanks
As i see it now , in the third loop where you checking your duplicates i thing that you have to increese k not i :
for (int k = j; j < count; k++), also you must decrise the size of j in your code under the count--;.So your code for checking duplicates seems right but , you want the intersection of this two arrays you made , so you dont have to check for duplicates because in the array intersection[SIZE] you will put only one number from the two arrays, so you will not have duplicates .You should check for duplicates if you wanted to make the union of this two arrays .I make some changings to your code acording what you want to create and this code here find the intersection from two arrays.Try this and delete the duplicate check because that makes your code to run to infinity . One last thing your intersection check must be replace whith this :
//Intersection check
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);

2d array and for loops

I did this code to try the basic operations on bidimensional arrays (2d arrays), but it doesn't print
on screen the letter 'A'.
Can someone help me figure this out?
Thanks
#include <stdio.h>
#define ROWS 2
#define COLS 3
int main()
{
int x;
int y;
int i;
int j;
int Field[ROWS][COLS];
printf("Enter two values: ");
scanf("%d %d", &x, &y);
Field[y][x] = 1;
for(i = ROWS - 1; i > 0; i--){
for(j = 0; j < COLS; j++){
if(Field[i][j] == 1){
printf("A");
}
}
}
return 0;
}
In the outer loop
for(i = ROWS - 1; i > 0; i--){
the range of indices is (in the descending order) [ROWS-1, 1], So the index 0 is not processed in this loop.
It is better such a loop to write the following way
for ( i = ROWS; i != 0; i-- ){
for(j = 0; j < COLS; j++){
if(Field[i-1][j] == 1){
printf("A");
}
}
}
In this case the variable i can have even an unsigned integer type as for example size_t and the code will be valid even when ROWS is equal to 0.
Otherwise the expression ROWS - 1 can yield the maximum value of the unsigned type when ROWS is equal to 0.
as mentioned i>0 should be i>=0 other wise you won't traverse the array completely.
also note that even after you you corrected that statement , it's better to check input x and y , because if they are greater or equal to ROWS and COLS , than by passing boundaries of your array here Field[y][x] = 1; your program will lead to undefined behavior.
so I suggest this
scanf("%d %d", &x, &y);
if (x < ROWS && y < COLS)
Field[y][x] = 1;
else
return 0;
//rest of your code with i>=0 in this loop for(i = ROWS - 1; i >= 0; i--)
This: for(i = ROWS - 1; i > 0; i--){ fails to traverse the necessary array location, and does not clearly convey to future maintainers of your software what exactly the intent is.
However, to do what you have described in your problem description, unless there is a compelling reason to set up a decrementing index (--), a normal incrementing (++) set of for loops is adequate and more idiomatic :)
for(i = 0; i < ROWS; i++)
{
for(j = 0; j < COLS; j++)
{
if(Field[i][j] == 1)
{
printf("A");
}
}
}
This code adds some protections to out of bounds arrays and prints 'A'. Should be a good starting point for you.
#include <stdio.h>
#define ROWS 2
#define COLS 3
int main()
{
int x,y,i,j = 0;
int Field[ROWS][COLS]; // 2 Rows 3 Column array
printf("\nEnter row value less than %i: ",ROWS);
scanf("%d", &y);
printf("\nEnter column value less than %i: ",COLS);
scanf("%d", &x);
if(x >= COLS || y>= ROWS)
{
printf("\nOut of bounds inputs!");
return 0;
}
Field[y][x] = 1;
for(i = ROWS - 1; i > 0; i--)
{
for(j = 0; j < COLS; j++)
{
if(Field[i][j] == 1)
{
printf("A");
}
}
}
return 0;
}

counting negative integers in a matrix

i am having error while running this code
negativenoinmatrix.c:10:16: error: subscripted value is neither array nor pointer nor vector
if(z[i][j]<0)
i want to calculate the number of negative integers in a matrix
#include <stdio.h>
int negnumbers(int *z, int n, int m)
{
int count = 0;
int i = 0;
int j = m - 1;
while (j >= 0 && i < n)
{
if (z[i][j] < 0)
{
count += (j + 1);
i += 1;
}
else
j -= -1;
}
return count;
}
int main()
{
int n = 3, m = 4;
int a[n][m];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
printf("%d ", a[i][j]);
printf("\n");
}
int val = negnumbers((int *) a, 3, 4);
printf("%d", val);
}
The function needs to accept a pointer to an array, not a pointer to a single item. Change it to
int negnumbers(int n, int m, int z[n][m])
...
int val = negnumbers(3, 4, a);
(Where int z[n][m], as per the rule of "array adjustment", will get changed by the compiler internally to a pointer to the first element, int (*z)[m].)
When you pass a 2-d array to a function, at least the 2nd dimension must be specified. Change to this:
int negnumbers(int z[][4],int n,int m)
You can then use this more straightforward approach to counting the negative numbers:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (z[i][j] < 0)
count++;
}
}
You are calling a pointer z, and also creating a dynamic matrix out of it. So you need to allocate some memory for it which can be done with:
malloc(z[i][j])
Then after you're done, make sure you deallocate the memory now or else you'll have a memory leak, which you can read more about at Memory Leaks wikipedia.
This is done by calling free(...)
Hope this solves the not an array or pointer error!

array counting sort segmentation fault

Hello i am trying to use counting sort to sort numbers that i read from a file. this is my code:
void CountingSort(int array[], int k, int n)
{
int i, j;
int B[100], C[1000];
for (i = 0; i <= k; i++)
{
C[i] = 0;
}
for (j = 1; j <= n; j++)
{
C[array[j]] = C[array[j]] + 1;
}
for (i = 1; i <= k; i++)
{
C[i] = C[i] + C[i-1];
}
for (j = 1; j <= n; j++)
{
B[C[array[j]]] = array[j];
C[array[j]] = C[array[j]] - 1;
}
printf("The Sorted array is : ");
for (i = 1; i <= n; i++)
{
printf("%d ", B[i]);
}
}
void max(int array[],int *k,int n){
int i;
printf("n je %d\n",n);
for (i = 0; i < n; i++)
{
if (array[i] > *k) {
*k = array[i];
}
}
}
int main(int brArg,char *arg[])
{
FILE *ulaz;
ulaz = fopen(arg[1], "r");
int array[100];
int i=0,j,k=0,n,x,z;
while(fscanf(ulaz, "%d", &array[i])!=EOF)i++;
fclose(ulaz);
n=i;
max(array,&k,n);
printf("Max je %d\n",k);
CountingSort(array,k,n);
return 0;
}
i have no errors but when i start my program i get Segmentation fault error. pls help! (dont read this bot is asking me to write some more details but i have none so i just write some random words so i can post my question and hopefully get an answer)
The problem is that your implementation of the counting sort is incorrect: it uses arrays as if they were one-based, while in C they are zero-based.
After carefully going through your loops and fixing all situations where you use a for loop that goes 1..k, inclusive, instead of the correct 0..k-1, the code starts to work fine:
int i, j;
int B[100], C[1000];
for (i = 0; i <= k; i++){
C[i] = 0;
}
for (j = 0; j < n; j++){
C[array[j]]++;
}
for (i = 1; i <= k; i++){
C[i] += C[i-1];
}
for (j = 0; j < n; j++) {
B[--C[array[j]]] = array[j];
}
printf("The Sorted array is : ");
for (i = 0; i < n; i++) {
printf("%d ", B[i]);
}
Demo.
Note: I modified some of the operations to use C-style compound assignments and increments/decrements, e.g. C[array[j]]++ in place of C[array[j]] = C[array[j]] + 1 etc.
The problem most likely is here
int B[100], C[1000]; // C has space for numbers up to 999
...
for (i = 1; i <= k; i++)
C[i] = C[i] + C[i-1]; // adding up till C[k] == sum(array)
for (j = 0; j < n; j++)
B[C[array[j]]] = array[j]; // B has space up to 99, but C[k] is sum(array)
so you're reserving space for C for a highest value of 999 but in B you're assuming that the sum of all input values is less than 100...
the resolution of your problem is to first probe the input array and get the maximum and the sum of all input values (and minimum if the range may be negative) and allocate space accordingly
edit: you probably meant j < n and not j <= n
Adding to dasblinkenlight's spot-on answer:
Is your input data guaranteed to be in the range [0, 999]? If it isn't, it's obvious that segmentation faults can and will occur. Assume that the maximum value of array is 1000. C is declared as
int C[1000];
which means that C's valid indices are 0, 1, 2, ... 999. But, at some point, you will have the following:
C[array[j]] = ... /* whatever */
where array[j] > 999 so you will be attempting an out-of-bounds memory access. The solution is simple: probe array for its maximum value and use dynamic memory allocation via malloc:
/* assuming k is the maximum value */
int * C = malloc((k + 1) * sizeof(int));
Note: an alternative to this, which would also nullify the need for an initialization loop to make all elements of C equal to 0, would be to use calloc, which dynamically allocates memory set to 0.
// allocate C with elements set to 0
int * C = calloc(k + 1, sizeof(int);
Another important factor is the range of your running indices: you seem to have forgotten that arrays in C are indexed starting from 0. To traverse an array of length K, you would do:
for (i = 0; i < K; ++i)
{
processArray(array[i]);
}
instead of
for (i = 1; i <= K; ++i)
{
processArray(array[i]);
}

Printing a multi dimensional array

I've written a little thing which asks the user for some input (rows and cols), which should then set everything in an array to a dot (".") and print it out, but this crashes my application.
void main()
{
int i,j, m, n;
printf("The number of lines (m): ");
scanf("%d", m );
printf("\nThe number of columns (n): ");
scanf("%d", n);
//create my array
char mineGrid[n][m];
//set all fields in to safe (.)
for (j = 0; j <= n; j++)
{
for (i = 0; i <= m; i++)
{
mineGrid[j][i] = ".";
}
}
//print a grid of dots
for (j = 0; j <= n; j++)
{
for (i = 0; i <= m; i++)
{
printf("%s", mineGrid[j][i]);
}
}
}
Any idea why this is crashing?
On cause of major trouble here is that you have a lot of loop that look like
for (j = 0; j <= n; j++)
/* ^ */
/* | */
/* Look! */
which will run j from 0 to n, but you have declared your array as
char mineGrid[n][m];
which means that space has been allocated for rows numbered 0 to n-1.
All you index loops are wrong in that way. The idomatic way to write those loops is
for (j = 0; j < n; ++j)
where I have fixed the range and also changed the increment from post- to pre- which is an old micro-optimization that generally does not make any difference in c these days (because compilers are smart enough to fix it), but can if you switch to c++ and use a non-trivial class in that way. So I keep it in my list of little things to "fix".
That's because you're putting a string in the array instead of char.
do it like this:
void main()
{
int i,j, m, n;
m = 5;
n = 6;
//create my array
char mineGrid[n][m];
//set all fields in to safe (.)
for (j = 0; j <= n; j++)
{
for (i = 0; i <= m; i++)
{
mineGrid[j][i] = '.';
}
}
//print a grid of dots
for (j = 0; j <= n; j++)
{
for (i = 0; i <= m; i++)
{
printf("%c", mineGrid[j][i]);
}
printf("\n");
}
}
you created n X m elements but used n+1 X m+1 elements in array. use like bellow
a
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
{
mineGrid[j][i] = '.';
}
}
That's because for an array of size N, the valid array indexes 0 to N-1. But you are accessing N th element which is not a valid array index and accessing it invokes undefined behavior.
for (j = 0; j <= n; j++)
{
for (i = 0; i <= m; i++) // Array out of bounds in either condition check
With that said, you have issues with your input as well.
scanf("%d", m ); // Missing & operator before m.

Resources