problem with nested loop in c program im so close

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

49 Replies - 1267 Views - Last Post: 22 February 2017 - 07:09 PM Rate Topic: -----

#1 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

problem with nested loop in c program im so close

Posted 17 February 2017 - 06:27 PM

this program is supposed to count through the array box[150] and open every second box from box 2 which i have done then from box 3 you open every third box if its open you close it if its close you open. you continue on for every 4th, 5th, 6th and so on until 150. My problem is with the nested loop i cant get it to work and theres no errors it just crashes when i run can someone please help ive been trying all day :(/> i dont understand why its not working the counter should be going up and i use mod to see if its every 4th, 5th box, etc.... please help here is my code:

#include<stdio.h>
#include <stdlib.h>
#define boxSize 150


int box[boxSize];
enum boxState{close, open};

int closeBox()
{
	int i;
	for (i=1; i<boxSize; i++)
	{
		box[i] = close;
	}
	return 0;
}


int openBox()
{
	int i;
	for (i=2; i<boxSize; i++)
	{
		if (i % 2 == 0)
		{
			box[i] = open;
		}
		
	}
	
	int j;
	int count;
	for (j=0; j<boxSize; j++)
    {   
		if (j % count == 0)
		{
			if (box[j] == open)
    		{
    			box[j] = close;
    		}
			else     // box must be open
			{
				box[j] = open;
    		}
    	count += 1;
		}
	}
		
}


int printBox()
{
	int i;
	for (i=0; i<boxSize; i++)
	{
      	printf("%d ", box[i]);
	} 
	
	
}

int main()
{
	closeBox();
	openBox();
	printBox();
}

This post has been edited by snoopy11: 18 February 2017 - 02:54 AM
Reason for edit:: added [ CODE ] tags please learn to use them !


Is This A Good Question/Topic? 0
  • +

Replies To: problem with nested loop in c program im so close

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,409
  • Joined: 27-December 05

Re: problem with nested loop in c program im so close

Posted 17 February 2017 - 07:56 PM

You didn't initialize count so it starts out with whatever value accidentally happens to be at the memory location occupied by count. If that happens to be 0, when you get to j % count you have a divide by zero error and it crashes. If it happens to be a value greater or equal to boxsize you never even enter that for loop.

Assign a nonzero initial value to count and you can get back to work figuring out how to accomplish whatever you're trying to do (what that is isn't entirely clear to me).

By the way, your functions openBox, closeBox and printBox don't return any values and apparently don't need to return any values, so you should make the return type void, not int.

PS: when you post code, please use code tags. Look at the instructions printed in faint type in the posting box.
Was This Post Helpful? 0
  • +
  • -

#3 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 17 February 2017 - 08:15 PM

sorry thats the old code, the code below is where i am now. The problem is i dont know how to write the loops to go through every 3rd element 4th element then 5th then 6th 7th and so on. Im able to open every other box which is the first instruction then i open every 3rd, 4th so on. I hope you understand thank you!

#include<stdio.h>
#include <stdlib.h>
#define boxSize 150


int box[boxSize];
enum boxState{close, open};

int closeBox()
{
	int i;
	for (i=0; i<boxSize; i++)
	{
		box[i] = close;
	}
	return 0;
}


int openBox()
{
	int i;
	for (i=1; i<boxSize; (i+=2))
	{
		box[i] = open;
	}
	int k;
	for (k=2;k<boxSize;k++)
	{
		for (k;k<boxSize;(k+=k))
		{
			
		}
	}
}	



int printBox()
{
	int j;
	int boxCount = 1;
	for (j=0; j<boxSize; j++)
	{
		if (j % 5 == 0)
		{
			printf("\n");
      	}
		if (box[j] == open)
		{
			printf("%d: Open           ", boxCount);
		}
		else if (box[j] == close)
		{
			printf("%d: Closed          ", boxCount);
		}
		boxCount += 1;
	}
	return 0;	
}

int main()
{
	closeBox();
	openBox();
	printBox();
}



This post has been edited by andrewsw: 18 February 2017 - 03:51 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,664
  • Joined: 19-February 09

Re: problem with nested loop in c program im so close

Posted 17 February 2017 - 08:54 PM

The inner loop is using k as the iterator as well as the outer.

You could write the second loop in function if you wanted.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,398
  • Joined: 20-March 10

Re: problem with nested loop in c program im so close

Posted 18 February 2017 - 03:41 AM

line 30

for (k;k<boxSize;(k+=k))



this statement has no effect.

So anyway from box 2 you open every other 2nd box so 4 6 8 10.....N

from box 3 you open every 3rd box so 6 9 12....N if its open already you close it
from box 4 you open every 4th box so 8 12 16....N if its open already you close it


so you identify patterns you have a step variable for you loop which starts at 2 and goes up to N in increments of one it does not go beyond N.

you have an array of boxes, array sizes start at 0 and goto N-1, where N is the size of the array if you wish to start your array at 1 then your array size must be N+1.

If you have understood the above you will see why your program crashes.

so for the first outer loop we have



for(n=n-1; n<boxSize; n++)
{



for the inner loop
for (i=0; i<boxSize; i=i+n)
        {


inside all the loops we are going to have to do some checking 2 is a special case isn't it ?

after all only in 2 do we just open up every nth box....

for all others apart from 2 we close open boxes and open closed boxes


so we need some if statements the first should check if n == 2 and if thats true
we open box[i]

else if thats not the case we else if the next statement to read if box[i] is open then close it
and else if thats not the case ie if box[i] is closed then we open it.
Was This Post Helpful? 0
  • +
  • -

#6 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 02:50 PM

View Postsnoopy11, on 18 February 2017 - 03:41 AM, said:

line 30

for (k;k<boxSize;(k+=k))



this statement has no effect.

So anyway from box 2 you open every other 2nd box so 4 6 8 10.....N

from box 3 you open every 3rd box so 6 9 12....N if its open already you close it
from box 4 you open every 4th box so 8 12 16....N if its open already you close it


so you identify patterns you have a step variable for you loop which starts at 2 and goes up to N in increments of one it does not go beyond N.

you have an array of boxes, array sizes start at 0 and goto N-1, where N is the size of the array if you wish to start your array at 1 then your array size must be N+1.

If you have understood the above you will see why your program crashes.

so for the first outer loop we have



for(n=n-1; n<boxSize; n++)
{



for the inner loop
for (i=0; i<boxSize; i=i+n)
        {


inside all the loops we are going to have to do some checking 2 is a special case isn't it ?

after all only in 2 do we just open up every nth box....

for all others apart from 2 we close open boxes and open closed boxes


so we need some if statements the first should check if n == 2 and if thats true
we open box[i]

else if thats not the case we else if the next statement to read if box[i] is open then close it
and else if thats not the case ie if box[i] is closed then we open it.




hey ive done what you have suggested but im not sure if i have coded it correctly can you please take a look. I think im close it runs fully no crash shows output. its just hard to understand how it goes from every 3rd to 4th to 5th etc.. heres my openbox function now:

void openBox()
{
	int i;
	for (i=1; i<boxSize; (i+=2))
	{
		box[i] = open;
	}
	
	int n;
	for(n=n-1; n<boxSize; n++)
	{
		for (i=0; i<boxSize; i=i+n)
		{
			if (n == 2)
			{
				box[i] = open;
			}
			else if (box[i] == open)
			{
				box[i] = close;
			}
			else if (box[i] == close)
			{
				box[i] = open;
			}
		}

	}
}	


Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,398
  • Joined: 20-March 10

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 03:14 PM

You have forgotten that n is uninitialized

it should have been initialized to two...

as that is the box you start with...

also the first for loop is strictly unnecessary as you close all the boxes first so you don't want to open those boxes in a for loop step two as the main loop does it for you.

In other words get rid of the first for loop and set n to 2 and you are golden.
Was This Post Helpful? 0
  • +
  • -

#8 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 03:27 PM

View Postsnoopy11, on 20 February 2017 - 03:14 PM, said:

You have forgotten that n is uninitialized

it should have been initialized to two...

as that is the box you start with...

also the first for loop is strictly unnecessary as you close all the boxes first so you don't want to open those boxes in a for loop step two as the main loop does it for you.

In other words get rid of the first for loop and set n to 2 and you are golden.


hey thanks i think i understand! is this what you mean i deleted the first for loop for opening every 2nd and i set n to 2 and i had to add int i; because it said its uninitialized. heres the function now:
void openBox()
{
	int i;
	int n = 2;
	for(n=n-1; n<boxSize; n++)
	{
		for (i=0; i<boxSize; i=i+n)
		{
			if (n == 2)
			{
				box[i] = open;
			}
			else if (box[i] == open)
			{
				box[i] = close;
			}
			else if (box[i] == close)
			{
				box[i] = open;
			}
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,398
  • Joined: 20-March 10

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 03:44 PM

Yeah looks about right...
Was This Post Helpful? 0
  • +
  • -

#10 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 04:37 PM

okay thank you i think its right

would this also work

void openBox(int skip)
{
    int i;
    for(i = skip; i < boxSize; i+=skip + 1) {
        if(box[i] == open)
            box[i] = close;
        else
            box[i] = open;
    }
    if(skip < boxSize)
        openBox(skip + 1);
}  



and called it with openBox(1) in main
Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,398
  • Joined: 20-March 10

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 04:43 PM

no that wont work...

your last code was correct why would you change it ?
Was This Post Helpful? 0
  • +
  • -

#12 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 05:06 PM

when i run it with the n = 2 code it seems the output i saw the skip method in some old c notes from school. you sure the function i had will open every 2nd as box[1] is box 2 and then open/close 3rd 4th 5th and so on to 150?
Was This Post Helpful? 0
  • +
  • -

#13 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,398
  • Joined: 20-March 10

Re: problem with nested loop in c program im so close

Posted 20 February 2017 - 10:00 PM

your current code is incorrect

your previous code is correct...

and yes I'm sure..........

but have it your way if you want.....It's no skin off my nose...

This post has been edited by snoopy11: 20 February 2017 - 10:01 PM
Reason for edit:: double post by forum software known bug

Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,409
  • Joined: 27-December 05

Re: problem with nested loop in c program im so close

Posted 21 February 2017 - 07:07 AM

View Postsnoopy11, on 21 February 2017 - 01:00 AM, said:

and yes I'm sure..........

Hmmm... Tired maybe? ;)/>/>/>

From post #8:
int n = 2;
for(n=n-1; ... 

How is that different from
int n;
for(n = 1; ... 


So, on the first iteration of the outer for loop (when n==1) that function flips every box from close to open.
On the second iteration (when n==2) it "opens" box[0] and every 2nd box thereafter, except that they're already all open anyway.
On the 3rd iteration (when n==3) it flips the state of box 0 and every 3rd box thereafter.
And so on ...
I don't think that's what you want.

The recursive function in post #10 seems to do what you wanted, but unless you were told to use recursion it would be preferable to use iteration with nested for loops. The added overhead of running recursion is sometimes justified but for this job recursion provides no benefit. There's nothing special about your "skip method". skip can be just another index variable to control the for loops.

The real point is that for this process you need 2 index variables to control your iteration. (Recursion is just another form of iteration, and in the recursive function, the skip argument that you send to the function has the same effect as would a 2nd index variable in nested loops.)

>>> So if you like the name "skip" instead of "j", use skip in the outer loop and use both skip and i together to control the inner loop. (But you still have to figure out how to make them work together correctly.)


One way to know if your code is right is to put a debugging printf statement in the appropriate place temporarily, while you're testing the program. For example, right after the closing } of the inner for loop you could do something like
printf("skip = %d:", skip);
for(i=0; i<boxSize; i++) {
    printf("%d ", box[i]);
}
printf("\n");


Was This Post Helpful? 0
  • +
  • -

#15 danptbosmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-February 17

Re: problem with nested loop in c program im so close

Posted 21 February 2017 - 05:55 PM

hey can you please show me what you mean? how do i change it from skip to j i did this but its not working :( i understand it but cant get the syntax down to open every 3rd 4th 5th and so on...i first have to open every 2nd box from box[1] which is box2.
int i, j;
    for(i = j; i < boxSize; i+=j + 1) 
	{
        if(box[i] == open)
        {
		
            box[i] = close;
        }
        else
        {
            box[i] = open;
        }
    }


Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »