Conway's Game of Life, counting neighbors [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have an error somewhere in my code, I think I am entering an infinite loop.
Basically I get a matrix and two indexes, i,j, and i need to count how many neighbors around [i][j] have a value of 1 or 2.
this is my code:
int number_of_living_neighbors(matrix mat,int i, int j, int n,int m)
{
int counter=0,row_index=0,column_index=0;
for(row_index=i-1;row_index<=i+1;row_index++)
{
for(column_index=j-1;column_index=j+1;column_index++)
{
if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m))
{
if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2))
counter++;
if((row_index==i)&&(column_index==j)&&(mat[i][j]==1))
counter--;
}
}
}
printf("The number of living neighbors is %d", counter);
return counter;
}
it doesnt print anything.
mat is the matrix i get, i,j are the pointers, n is number of rows, m is number of columns.

for(column_index = j-1 ; column_index = j+1; column_index++)
Your condition contains an assignment, not a comparison. You probably want to use <= instead of =:
for(column_index = j-1 ; column_index <= j+1; column_index++)

Related

Frequency of values of an array [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to create a program that will count the frequency of 10 numbers that i will choose but when i am trying to run it it doesn't even run the printf and closes.Any ideas? thanks
#include <stdio.h>
int main()
{
int i,j,A[10]={0},C[10]={0};
for(i=0;i<10;i++)
{
scanf("%d /n",&A[i]);
}
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
getch();
}
The line
if(A[i]==j)
looks wrong - i was the counter for a previous loop and is now 10 (so beyond the bounds of your array). Did you mean
if(A[j]==j)
// ^
instead?
Changing this makes the program run for me. I don't think it does what you want yet. The break statement causes execution to halt the first time you find any match.
Hopefully this is enough hints to allow you to investigate how to count frequency of the numbers then print them all out yourself.
You need to again loop over the A array in order to check the value of each element:
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
}
If you know that the values in A will range from 0-9 then you could completely remove the inner loop and just do C[A[i]]++;
After this your C array will contain a count of each number the user input. e.g. C[5] will contain the number of 5s found, so you can output this as you see fit

Comparing array elements and trying to add dissimilar elements to other array not working? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i am trying to compare array elements and idea is to collect all dissimilar element to other temp[] array. I think it is going somewhere wrong ...unable to understand...please help me
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
int my_arr[SIZE] = {10,20,45,63,89,20,15,12,89,24,12,10,89,25,64,39,37,64,95,
27,23,58,97,23,18,56,94,76,32,11
};
int main()
{
int i,j,temp_arr[100];
for(i=0;i<SIZE;i++)
{
for(j=0+i; j<SIZE; j++)
{
if(*(my_arr+i)!=*(my_arr+j))
*(my_arr+i) = temp_arr[i];
}
}
return 0;
}
Here:
*(my_arr+i) = temp_arr[i];
temp_arr[j] is not initialized and you are assigning it to my_arr[i]. You description sounds like you want:
temp_arr[i] = *(my_arr+i);
But then you will end up having holes in the temp_arr. So perhaps you need another index for counting items in the temp_arr.
Something like:
int tmp_cnt = 0;
for(i=0;i<SIZE;i++) {
for(j=0+i; j<SIZE; j++) {
if(*(my_arr+i)!=*(my_arr+j))
temp_arr[tmp_cnt++] = *(my_arr+i);
}
}

Nested whiles in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
int count(int *a, int *b, int n) {
int i=0,j=0,roz=0;
while(i<n) {
while(j<n) {
if(a[i]==b[j])
roz++;
j++;
}
i++;
}
return roz;
}
n is the size of a, or b array (both are of the same size). The nested while loop seems to work only once, for i = 0. The next thing that seems to be happening is magically changing n into 1 (it is 5 at the beginning), so that the nested while doesn't loop the second time. Why is that so?
The interesting thing is that echoing n right before the return displays the right value, which is 5...
EDIT. For loops work properly here, but the question is still present.
You never reset the value of j to 0.
You should reset j to 0 between two nested loops.
while (i < n)
{
j = 0;
while (j < n)
{
if (a[i] == b[j])
roz++;
}
}
You could compute the intersection of two arrays in a more efficient way (there is a O(N*log N) solution).
The value of n is not changed here. The reason nested loop is not executing for i = 1 or later is that you are not resetting the value of j. Say, n is 5. When i = 0 nested loop executes properly and the value of j is 5 after it finishes. When i = 1 then j is still 5 and so it never enters the nested loop.
while(i<n)
{
j = 0; // reset j here to solve the problem
while(j<n) {}
Well by looking at the logic,it seems that you are conuting the number of common elements in both the arrays.
the mistake you are doing is you are not resetting the value of j to 0 in the first while loop.

Displaying the frequencies of randomly generated integers [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
This code is meant to list the number from 1-9 and display next to each number the frequency that it appears in the 10,000 random numbers that have been generated.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int counts[10];
int i;
int random;
srand(time(0));
for (i = 0; i < 10; i++)
counts[i] = 0;
for (i = 0; i<= 10000; i++)
{
random = (int)(10.0*rand()/(RAND_MAX+1.0));
counts[random]++;
}
for (i = 0; i<10; i++);
printf("%d occurs %f%%\n", i, i/100.0);
return(0);
}
However when I run the program I get a list from 1-9 which I want but next to it lists 1-9 but in decimal instead of the frequency. this is what i get when i run it:
1 occurs 0.010000%
2 occurs 0.020000%
3 occurs 0.030000%
4 occurs 0.040000%
5 occurs 0.050000%
6 occurs 0.060000%
7 occurs 0.070000%
8 occurs 0.080000%
9 occurs 0.090000%
What do i need to change in order to get the frequency of each number,
also when I run the code from terminal using emacs it lists 1-9 but when I run the code in eclipse it only displays 10 occurs 0.100000% in the console. Is there any reason why this happens?
It looks like you're storing the frequencies of each number correctly, you're just not displaying them. Your output doesn't refer to the counts array at all.
In this line:
printf("%d occurs %f%%\n", i, i/100.0);
i/100.0 should be counts[i]/10000.

Can anyone explain this program? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
int array[2][2] = {0, 1, 2, 3};
int i;
int sum = 0;
for (i =0; i < 4; ++i)
{
int x, y;
x = i % 2;
if (x)
{
y = 0;
}
else
{
y = 1;
}
sum += array[x][y];
}
printf("%d\n", sum);
It's short enough that you could walk through it yourself (since this is homework) and run each line yourself on paper. If there is any line that you can't figure out, ask a more specific question. Just use pencil, make a box to show the values of x, y, i, sum and all 4 elements of the array. Then walk through changing the values in those boxes as you examine lines of code and you will see exactly what's happening. One thing you should know is that "if (x)" will treat x as true when it is 1.

Resources