Thread: C Code

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    C Code

    Consider the following C program segment . Rewrite it using no gotos or breaks.

    j = -3;

    For (I = 0; I < 3; I++){

    Switch ( j + 2) {

    Case 3:

    Case 2: j--; break;

    Case 0: j +=; break;

    Default: j = 0;

    }

    if ( j > 0) break;

    j = 3 – I

    }

  2. #2
    Is this written in C?

    Well the best way I think this works is:

    Code:
    int main() {
    	int i, j;
    
    	j = -3;
    
    	for (i = 0; i < 3; i++){
    		if ((j + 2) == 2) {
    			j--;
    		}else if ((j + 2) == 0) {
    			j++;
    		}else {
    			j = 0;
    		}
    
    		if (j < 0)
    			j = 3 - i;
    	}
    
    	return 0;
    }
    My syntax may be wrong though.
    Last edited by Stack Overflow; 05-25-2004 at 10:37 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    thanx for the fast reply...

    yes ... it is C.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, use code tags. Second off, stop having people do your work for you. Had you actually read the forum rules, you'd have known this. Don't try and disguise this as "well I just wanted to see if you could" either. Trust me, someone here, or more than one someones here can do just about anything you post. So take that as a given, read the forum guidelines, and don't try and disguise your homework or tests as "can anyone do this" BS.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Oh,

    :/ I thought he was asking syntatically since break and goto are cheap most of the time.

    I'll have to keep on the look out for people like that


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Stack Overflow
    Oh,

    :/ I thought he was asking syntatically since break and goto are cheap most of the time.

    I'll have to keep on the look out for people like that


    - Stack Overflow
    Yeah, when you read this:
    >Consider the following C program segment . Rewrite it using no gotos or breaks.
    Well, you get the idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM