swapping odd and even numbers inside an Array in C - c

Input array is for example Array[10] = {12,23,0,-7,138,22,7,99,10,-2}
I want to print out the array with even numbers on one end and odd numbers on the other, something like this: Array[10] = {12,0,-2,10,22,138,-7,99,7,23}
int main()
{
int N = 0, i = 0, j = N-1, index = 0;
printf("Enter N (0>N<10): ");
scanf("%d", &N);
int Array[N];
for(i=0;i<N;i++){
printf("Enter the value number %d :", i+1);
scanf("%d", &Array[i]);
}
printf("Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array[i]);
}
printf("}");
// swaping odd and even numbers
int Array2[N];
while (i < j){
if(Array[i]%2 == 0){
Array2[i] = Array[index];
i++;
}
else{
Array2[j] = Array[index];
j++;
}
index++;
}
// view the new array
printf("\n Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array2[i]);
}
printf("}");
return 0;
}
This doesn't seem to work. Any help would be appreciated.
Note: I know the Array[N] part is not how it's supposed to be done, it's just to simplify things.

In your else statement:
else{
Array2[j] = Array[index];
j++;
}
You need j--; not j++.

In your else statement:
if(Array[i]%2 == 0){
don't you want to inspect the item you are sorting (Array[index]) ?

At the beginning of the program in this declaration
int N = 0, i = 0, j = N-1, index = 0;
j is set to -1 because N is initialized by 0.
So this loop
while (i < j){
//...
will be iterate never independing on whether it makes any sense.:)
As for the loop then it does not swap even and odd numbers.
If I have understood your approach correctly you need something like the folloing. You can modify the demonstrative program such a way that the values of elements of the array will be enetered by the user.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
int i, j;
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
i = 0; j = N;
while ( i != j )
{
if ( a[i] % 2 == 0 )
{
++i;
}
else if ( a[--j] % 2 == 0 )
{
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
return 0;
}
The program output is
Array[10] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
Array[10] = { 12, -2, 0, 10, 138, 22, 7, 99, -7, 23 };

Dry run it, and you will understand the concept
#include <stdio.h>
int main()
{
int arr[10];
int i,temp;
printf("Enter elements into arry\n");
for(i=0;i<=9;i++)
{
printf("Element #%d-->",i);
scanf("%d",&arr[i]);
}
for(i=0;i<=9;i=i+2)
{
if((arr[i]%2)!=0)
{
if((arr[i+1]%2)!=0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)==0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
else
{
if((arr[i+1]%2)==0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)!=0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
for(i=0;i<=9;i++)
{
printf("Element #%d-->%d\n",i,arr[i]);
}
return 0;
}

Related

Finding Median of an Array

I am trying to write a C program to find the median of an array, but the task requires to not sort the array. The current code I have works, but fails when there is a repeated number. I am struggling to find a way to account for this case. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int median_finder(int size, int* data) {
int n1, n2;
int count = 0;
for (int t = 0; t < size; t ++) {
int piv = data[t];
int higher = 0;
int lower = 0;
int median;
if (size % 2 != 0) {
for (int j = 0; j < size; j++) {
if (piv < data[j]) {
higher++;
} else if (piv > data[j]) {
lower++;
}
}
if (higher != 0 && lower == higher) {
printf("MEDIAN: %d\n", piv);
return 0;
}
} else {
//int num = 0;
for (int j = 0; j < size; j++) {
if (piv < data[j]) {
higher++;
} else if (piv > data[j]) {
lower++;
}
}
if (higher != 0 && (lower == size/2 || higher == size/2)) {
count++;
if (count == 1) {
n1 = piv;
} if (count == 2) {
n2 = piv;
}
}
} if (count == 2) {
if (n1 > n2) {
median = n2;
} else {
median = n1;
}
printf("Median: %d\n", median);
return 0;
}
}
}
int main(int argc, char** argv) {
int size = atoi(argv[1]);
argv++;
argv++;
int data[size];
for (int i = 0; i < size; i++) {
data[i] = atoi(argv[i]);
}
median_finder(size, data);
}
The median for an unsorted array with possible duplicate values a of length n is the element with the value a[i] where half of the remaining elements (n-1)/2 (rounded down) are between less than (lt) or less than and equal (lt + eq) to a[i]:
#include <assert.h>
#include <stdio.h>
int median(size_t n, int *a) {
assert(n > 0);
for(size_t i = 0; i < n; i++) {
size_t lt = 0;
size_t eq = 0;
for(size_t j = 0; j < n; j++) {
if(i == j) continue;
if(a[j] < a[i]) lt++;
else if(a[j] == a[i]) eq++;
}
if((n-1)/2 >= lt && (n-1)/2 <= lt + eq)
return a[i];
}
assert(!"BUG");
}
// tap-like
void test(size_t test, int got, int expected) {
printf("%sok %zu\n", got == expected ? "" : "not ", test);
if(got != expected) {
printf(" --\n"
" got: %d\n"
" expected: %d\n"
" ...\n", got, expected);
}
}
int main(void) {
struct {
size_t n;
int *a;
} tests[] = {
{1, (int []) {0}},
{2, (int []) {0, 1}},
{3, (int []) {-1, 0, 1}},
{4, (int []) {-1, 0, 0, 1}},
};
for(int i = 0; i < sizeof tests / sizeof *tests; i++) {
test(i+1, median(tests[i].n, tests[i].a), 0);
}
}

Havel-Hakimi Algorithm in C

I'm trying to write havel-hakimi theorem in C. But I have a problem with the while loop. The program doesn't sort array again in the while loop and that's why output prints the wrong answer. Could show me what's my fault please?
# include <stdio.h>
int main(){
int j,i,vertex_number,temp1,temp2,a=0,b=0;
printf("Vertex Number:");
scanf("%d",&vertex_number);
int graph[vertex_number];
for(i=0;i<vertex_number;i++){
scanf("%d",&graph[i]);
}
while(1){
//SORTING ARRAY
for(i=0;i<vertex_number;i++){
for(j=i+1;j<vertex_number;j++){
if(graph[i]<graph[j]){
temp1=graph[i];
graph[i]=graph[j];
graph[j]=temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]==0){
a++;
}
}
if(a==vertex_number){
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]<0){
b++;
}
}
if(b>0){
printf("graph not exist.");
return 0;
}
temp2=graph[0];
for(i=0;i<temp2;i++){
graph[i]=graph[i+1];
}
vertex_number--;
for(i=0;i<temp2;i++){
graph[i]-=1;
}
printf("-------------\n");
for(i=0;i<vertex_number;i++){
printf("%d\n",graph[i]);
}
}
}
Your have 2 issues, the exist if graph[i]-=1; is negative and the removing loop doing should be done for vertex_number and not temp2
# include <stdio.h>
int main(void) {
int i, j, vertex_number, temp1, temp2;
printf("Vertex Number:");
scanf("%d", &vertex_number);
int graph[vertex_number];
for (i = 0; i < vertex_number; i++){
scanf("%d", &graph[i]);
}
while (1) {
//SORTING ARRAY
for (i = 0; i < vertex_number; i++) {
for (j = i+1; j < vertex_number; j++) {
if (graph[i] < graph[j]) {
temp1 = graph[i];
graph[i] = graph[j];
graph[j] = temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
if (graph[0] == 0) {
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for (i = 0; i < vertex_number; i++) {
if (graph[i] < 0){
printf("graph not exist.");
return 0;
}
}
temp2 = graph[0];
vertex_number--;
for (i = 0; i < vertex_number; i++) { // HERE was your issue
graph[i] = graph[i + 1];
}
for (i = 0; i < temp2; i++) {
graph[i]-=1;
if (graph[i] < 0) {
printf("graph not exist.");
return 0;
}
}
printf("-------------\n");
for (i = 0; i < vertex_number; i++) {
printf("%d\n",graph[i]);
}
}
}
You don't need a, if the first is null all the other are null or negative
You don't need b neither just stop on first occurence
Not an answer, but a cleaned-up version that works follows.
The key is that it literally removes the first element from the array by advancing the pointer and reducing the size of the array. That way, we're always working with elements 0..s-1 or 0..n-1.
// Destroys the contents of the provided array.
int havel_hakimi(unsigned *degrees, size_t n) {
while (1) {
// Yuck
for (size_t i=0; i<n; ++i) {
for (size_t j=i+1; j<n; ++j) {
if (degrees[i] < degrees[j]) {
int temp = degrees[i];
degrees[i] = degrees[j];
degrees[j] = temp;
}
}
}
if (degrees[0] == 0)
return 1; // Has a simple graph.
// Remove first element.
unsigned s = degrees[0];
++degrees;
--n;
if (s > n)
return 0; // Invalid input!
if (degrees[s-1] == 0)
return 0; // Doesn't have a simple graph.
for (size_t i=s; i--; )
--degrees[i];
}
}
Tested using the following:
#include <stdio.h>
int main(void) {
{
unsigned degrees[] = { 6, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 1
}
{
unsigned degrees[] = { 6, 5, 5, 4, 3, 2, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 0
}
return 0;
}

Find maximum value from 2 dimensional array & addition of all the values before the maximum value & multiply the all values after the maximum value

Here is my code
#include<stdio.h>
void main() {
int a[4][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 15, 6, 5 },
{ 4, 3, 2, 1 } };
int max = a[0][0];
int mIndexF, mIndexE, addition = 0, multiplication = 1, i, j, status = 0, k,
l;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
mIndexF = i;
mIndexE = j;
}
}
}
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
if ((a[k][l] < max) && (status == 0)) {
addition += a[k][l];
} else {
status++;
if (a[k][l] != max) {
multiplication *= a[k][l];
}
}
}
}
printf("Addition is %d\n", addition);
printf("Multiplication is %d", multiplication);
return 0;
}
I want to find the maximum value. Also want to print addition of the values which are in before of the maximum value and want to print multiply value of the values which are in after the maximum value.
The following should do the trick:
#define MAX_INT (((unsigned int)(-1))>>1)
#define MIN_INT (~(MAX_INT))
void minmax(int a[4][4])
{
int i, j, maxi=0, maxj=0, max=MIN_INT, sum=0, mul=1;
// this is for find out maximum value
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (max < a[i][j]) {
max = a[i][j];
maxi = i;
maxj = j;
}
}
}
// this is to add and multiply
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (i< maxi || (i==maxi && j<maxj)) // this is "before"
sum += a[i][j];
else if (i==maxi && j==maxj) // this is "same"
; //..nothing to do
else mul *= a[i][j];
}
}
printf("i,j=%d,%d; sum= %d, mul= %d\n", maxi, maxj, sum, mul);
}
EDIT: Added definitions of MAX_INT and MIN_INT
Your code seems to be correct: just initialize
max=INT_MIN using include<limits.h>
Your second loops seems to slight inappropriate you could use:
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(status==0)
add+=disp[i][j];
else if(status==1)
mul*=disp[i][j];
if(i==loc_i && j==loc_j)
status=1;
}
Afterwards just subtract
add-=disp[mIndexF][mIndexE];
#include<stdio.h>
void main(){
int a[4][4]={
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,2,3,3}
};
int max = a[0][0],mIndexF,mIndexE,addition = 0,multiplication = 1,i,j,status=0,k,l;
// this is for find out maximum value
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(max<a[i][j]){
max = a[i][j];
mIndexF=i;
mIndexE=j;
}
}
}
printf("The maximum value is %d\n", max);
for(k=0;k<4;k++){
for(l=0;l<4;l++){
if((a[k][l]<max) &&(status==0)){
addition+=a[k][l];
}else{
status++;
if(a[k][l]!=max){
multiplication*=a[k][l];
}
}
}
}
printf("Addition is %d\n",addition);
printf("Multiplication is %d",multiplication);
return 0;
}

How to display the elements of a 5*5 matrix in the following way?

I want to display the output of 5*5 matrix in the following way:
Input:
1 2 3 4 5
6 7 8 9 0
1 3 5 7 9
2 4 6 8 0
1 4 3 7 0
Output:
1 2 3 4 5 0 9 0 0 7 3 4 1 2 1 6 7 8 9 7 8 6 4 3 5
I have written the below program, but I can't get output like the above. What is the mistake?
#include<stdio.h>
int main()
{
int i, j, m, n, a[5][5];
scanf("%d%d", & m, & n);
if (m >= 1 && m <= 5 && n >= 1 && n <= 5)
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", a[i][j]);
}
}
if (m == 5 && n == 5)
{
for (i = 0; i == 0; i++)
{
for (j = 0; j < n; j++)
{
printf("%d", a[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = n - 1; j == n - 1; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 1; i == n - 1; i++)
{
for (j = n - 2; j >= 0; j--)
{
printf("%d", a[i][j]);
}
}
for (i = n - 2; i >= n - 4; i--)
{
for (j = 0; j == 0; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 4; i == n - 4; i++)
{
for (j = n - 4; j <= n - 2; j++)
{
printf("%d", a[i][j]);
}
}
for (i = n - 3; i == n - 3; i++)
{
for (j = n - 2; j >= n - 4; j--)
{
printf("%d", a[i][j]);
}
}
}
}
return 0;
}
use
if(m>=1 && m<=5 && n>=1 && n<=5)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
you will get output displayed on screen and use
printf("%d\t",a[i][j]);
to display output clearly
make this changes to the code, you are not looping through the matrix properly,
#include<stdio.h>
int main()
{
int i,j,m,n,a[5][5];
scanf("%d%d",&m,&n);
if(m>=1 && m<=5 && n>=1 && n<=5)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
if(m==5 && n==5)
{
for(i=0;i==0;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=1;i<n;i++)
{
for(j=n-1;j==n-1;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-1;i==n-1;i++)
{
for(j=n-2;j>=0;j--)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-2;i>=n-4;i--)
{
for(j=0;j==0;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-4;i==n-4;i++)
{
for(j=n-4;j<=n-2;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-3;i<=n-2;i++)
{
for(j=n-2;j==n-2;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-2;i==n-2;i++)
{
for(j=n-3;j>=n-4;j--)
{
printf("%d\t",a[i][j]);
}
}
for(i=n-3;i==n-3;i++)
{
for(j=n-4;j<=n-3;j++)
{
printf("%d\t",a[i][j]);
}
}
}
}
return 0;
}
this loop is for 5*5 only as you coded.
5*5 sample
#include<stdio.h>
int main(){
int i,j,m,n,a[5][5];
//scanf("%d %d",&m,&n);
//if(m>=1 && m<=5 && n>=1 && n<=5){
m = n = 5;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", &a[i][j]);
}
}
int side_size = m;
int row = 0, col = 0;//start point
int side = side_size -1;
while(side > 1){
for(i = 0; i<side ;++i)
printf("%d ", a[row][col++]);//MOVE RIGHT
for(i = 0; i<side ;++i)
printf("%d ", a[row++][col]);//MOVE DOWN
for(i = 0; i<side ;++i)
printf("%d ", a[row][col--]);//MOVE LEFT
for(i = 0; i<side ;++i)
printf("%d ", a[row--][col]);//MOVE UP
++row;++col;
side -= 2;
}
printf("%d\n", a[row][col]);
return 0;
}
I would use go forward and turn right approach
int max_x = 4;
int min_x = 0;
int max_y = 4;
int min_y = 0;
int x = 0;
int y = 0;
int direction = 0; // 0-RIGHT, 1-DOWN, 2-LEFT, 3-UP
int count = 0;
int go_forward(){
switch(direction){
case 0:
if( x+1 > max_x ){ min_y++; return 1; }
else x++;
break;
case 1:
if( y+1 > max_y ){ max_x--; return 1; }
else y++;
break;
case 2:
if( x-1 < min_x ){ max_y--; return 1; }
else x--;
break;
case 3:
if( y-1 < min_y ){ min_x++; return 1; }
y--;
break;
}
return 0;
}
int turn_right(){
direction = (direction+1)%4; // 0-RIGHT, 1-DOWN, 2-LEFT, 3-UP
}
int main(){
/* int matrix[5][5]={ { 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}}; //TEST */
//get matrix from input
int cango;
do{
printf("%d ", matrix[y][x]);
count++;
cango = go_forward();
if( cango!=0 ){
turn_right();
cango = go_forward();
}
}while(cango == 0 && count < 25);
return 0;
}

Error in a "for" loop, I think memory problems

I'm trying to create an algorithm that loops through the loop 'for' ten times, the ultimate goal is to print out a 50x50 matrix with the sum of the values ​​greater than or equal to 49, 0, and 1 represented as '*', but for some strange reason the loop only does 3 iterations instead of 10. ..
#include <stdio.h>
void printsimulaciodeu(int matlefa[50][50]) {
int ii, jj;
for (ii = 0; ii < 50; ii++) {
for (jj = 0; jj < 50; jj++) {
if(matlefa[ii][jj] == 0) {
printf(" 0 ");
}
else if (matlefa[ii][jj] >= 49) {
printf(" %d ", matlefa[ii][jj]-48);
}
else {
printf(" * ");
}
}
printf("\n");
}
}
int main(){
int tabla[50][50]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
int its;
int i, j;
int disparos_pos[10] = {1, 5, 14, 22, 37, 48, 43, 40, 28, 19};
char disparos_dir[10]= {'i','i','d','i','d','d','i','d','d','i'};
int mataux[50][50];
for (i = 0; i < 50; i++) {
for (j = 0; j < 50; j++) {
mataux[i][j] = tabla[i][j];
}
}
int matsuma[50][50];
for (its = 0; its < 10; its++) {
int pos = disparos_pos[its];
int dir = disparos_dir[its];
i = 0;
j = pos;
int cont = 49;
while (i < 49) {
if (mataux[i+1][j] == 0) {
mataux[i+1][j] = cont;
++i;
}
else {
if (dir == 'i') {
if (mataux[i+1][j] != 0) {
if (mataux[i][j-1] == 0) {
mataux[i][j-1] = cont;
--j;
}
else {
dir = 'd';
}
}
}
if (dir == 'd') {
if (mataux[i+1][j] != 0) {
if (mataux[i][j+1] == 0) {
mataux[i][j+1] = cont;
++j;
}
else {
dir = 'i';
}
}
}
}
}
if (its == 0) {
int m, n;
for (m = 0; m < 50; m++) {
for (n = 0; n < 50; n++) {
matsuma[m][n] = mataux[m][n];
}
}
}
if (its > 0) {
int p, q;
for (p = 0; p < 50; p++) {
for (q = 0; q < 50; q++) {
if (mataux[p][q] == 49) {
if (matsuma[p][q] >= 49) {
matsuma[p][q] = matsuma[p][q]+1;
}
}
}
}
}
}
printsimulaciodeu(matsuma);
}
I thought that one possibility is that the memory for the process is complete and therefore not iterate correctly, is that possible?
please help, I need it to work well for college work.
After a bit of debugging, I figured out the actual problem: Your while loop will run for INFINITE time in 3rd iteration of for loop.
Explaination
The while loop runs OK until i=33
at i=33 (j = 21, dir = 'i'), the none of the assignment statements (mataux[blah][blah] = blah balh) are executed, as conditons turn out to be false. Ultimate result is that value of 'dir' is toggled between 'i' and 'd', and the loop will run for forever!!
Details
mataux[i+1][j] = mataux[34][21] = 1 != 0 ---> first IF skipped
mataux[i][j-1] = mataux[33][20] = 49 != 0 ---> third IF in first ELSE of WHILE skipped
ELSE --> dir = 'd' (100)
mataux[i][j+1] = mataux[33][22] = 1 != 0 ---> third IF in last IF block skipped
ELSE --> dir = "i" (105)
Hope this helps!

Resources