Not efficient.

This is a discussion on Not efficient. within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <string.h> int main() { unsigned int a, b, c, wh; char *array[3]={ "smelly", "Hello world", "piece of ...

  1. #1
    Unregistered
    Guest

    Question Not efficient.

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    unsigned int a, b, c, wh;

    char *array[3]={ "smelly",
    "Hello world",
    "piece of cake hellol asdjfksd asdfsf adsfa sfaafa"};

    printf("Please enter the array that you wish to truncate");
    scanf("%d", &wh);

    for(a=0; a<50; a+=5)
    {
    for(b=0; b<wh; b++)
    {
    for(c=a; c<(a+5); c++)
    {
    if ( c < strlen(array[b]) )
    printf( "%c", array[b][c] );
    }
    printf( "\n" );
    }
    printf( "-----\n" );
    }
    return 0;
    }

    If I assign number '3' to the address of variable 'wh', the loops
    truncate the array just fine. The problem is when I
    assign the number '1' to the address of variable 'wh', the loops truncate
    as usual but I found out that this is not efficient because the outer loop
    still loops until it reaches 50 bytes, while the first array just contains 6 bytes!
    How do I stop the outer loop loops when it is end of the first array?

    I had tried many ways to solve this but to no avail.
    Please help!

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Lightbulb

    I would use a "goto".

    Basically, you define a tag in your program (for example right after the loop):

    mytag:

    and then inside the loop when you reach the end of the string and you want to exit you call:

    goto mytag;

    This will break you out of all the loops you are in and continue the execution of the program from the tag that you previously defined.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > for(a=0; a<50; a+=5)

    You need to add another condition here
    for(a=0; a<50 && /*something else */; a+=5)

    Or you have
    if ( condition ) break;

    Somewhere inside the for(a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Engineer
    I would use a "goto".
    The purists may crucify you for this statement .

  5. #5
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    the only problem with goto is that it has the potential to be abused and make a program very difficult to follow, so it's considered bad practice. if you use it once to get out of a bind and it doesn't cause any problems and keeps the code shorter, then more power to you.
    pointer = NULL

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    It is a sensible way out of a deeply nested loop .

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > How do I stop the outer loop loops when it is end of the first
    > array?

    Functions. Functions are your friend.

    Or, you could add an additional check to the outside loops.
    Redesign your program. This is rather brutal looking. What is the purpose of the program? Identify it's overall purpose, then break it down into smaller tasks.

    Quzah.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    i'm not a purist

    quote:
    --------------------------------------------------------------------------------
    Originally posted by Engineer
    I would use a "goto".

    --------------------------------------------------------------------------------


    The purists may crucify you for this statement .

    I'm not a purist but i'll still crucify you... GOTO statements make code horrible, i once used them and found they are pretty much the antithesis of good coding... if i'm looking at some code and i see a GOTO i immediately close it before my mind can be tainted with such blasphemy.

    (j/k mostly, i'm just not a fan of spaghetti code)

  9. #9
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Exclamation

    You can crusify me as many time as you want, Cozman!

    I have been using goto's occasionally for more than 3 years now and never ran into any problems. I use them in my personal code, I use them at work, I use them everywhere I possibly can (j/k).

    Anyway, I really think that if you don't overdo it using goto's, you can actually improve you code (let's say 1 or 2 goto's if you really need them), but I do agree with you that making a spaghetti out of C code using goto's can make you debugging life a living hell.

    Cheers.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    -

    >>You can crusify me as many time as you want, Cozman!
    hmm... sounds like a bad religious pick up line

    >>Anyway, I really think that if you don't overdo it using goto's, you can actually improve you code (let's say 1 or 2 goto's if you really need them), but I do agree with you that making a spaghetti out of C code using goto's can make you debugging life a living hell.

    Yeah you're right, i have seen a few places where they are more efficient than other methods, just not part of my personal style.

  11. #11
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Thumbs up

    Maybe this came out wrong, but I really wasn't trying to pick you up.

    Glad that you see my point.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  12. #12
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Thumbs up

    Maybe this came out wrong, but I really wasn't trying to pick you up.

    Glad that you see my point.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    sure..

    sure you weren't (j/k)

  14. #14
    Unregistered
    Guest

    Wink Thank you all.

    Dear All,
    There is a new problem. If I assign number '2' to the variable wh, the outermost loop of course has to terminate when it reaches the end of the biggest size of array. In this case, the variable 'a' has to be 10 bytes instead of 6 bytes.

    I was wondering if I can used 'bubble sort' to sort the array (if it's more than 1 array) in ascending manner.
    So, I just return the strlen(array[0]) which got the highest length. Thus, the outermost loop will loop until the end of the biggest size of array!

    Please correct me if I'm wrong. All critics are welcome!
    Lastly, thank you for all of your anticipation.

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    int main() 
    { 
    	unsigned int i, wh; 
    
    	char *array[3]={ "smelly", "Hello world", "piece of cake hellol asdjfksd asdfsf adsfa sfaafa"}; 
    
    	printf("Please enter the array that you wish to truncate"); 
    	scanf("%d", &wh); 
    
    	for(i = 0; i < strlen(array[wh - 1]); i++)
    	{
    		if((i+1) % 5 == 0)
    		{
    			printf("\n");
    			continue;
    		}else
    		{
    			printf("%c", array[wh-1][i]);
    		}
    	}
    	printf("\n");
    
    	return 0; 
    }
    Just make sure you enter 1 for array[0], 2 for array[1], 3 for array[2]. Get it. Try it!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Most Efficient Way to Check a Variable Is A Whole Number
    By bengreenwood in forum C++ Programming
    Replies: 29
    Last Post: 05-28-2009, 01:33 PM
  2. Is std::map efficient for this problem?
    By dudeomanodude in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2008, 02:15 PM
  3. Efficient Algorithm
    By purple in forum C Programming
    Replies: 10
    Last Post: 02-05-2003, 02:01 PM
  4. How do I write more efficient code?
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 11:08 AM
  5. more efficient code
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21