Thread: need c program

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    need c program

    Hello world,

    I would be very gracefull if someone could send me a program with some math algorithms, which calculates smth (no matter what) but do it long, for some seconds. Please help for c newbie.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here you go:
    Code:
    #include <time.h>
    
    int main(void)
    {
        time_t start = time(0);
        while (difftime(time(0), start) < 3);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(){
    	int i;
    	int j;
    	int k;
    	for(i = 0; i < 30000; i++)
    		for(j = 0; j < 30000; j++)	
    			for(k = 0; k < 30000; k++){
    			}
    	printf("%d", 1+1);
    	return 0;
    }
    This calculates the number 2 (as the sum of 2 integers) but it takes a while to do so.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by hilarius View Post
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(){
    	int i;
    	int j;
    	int k;
    	for(i = 0; i < 30000; i++)
    		for(j = 0; j < 30000; j++)	
    			for(k = 0; k < 30000; k++){
    			}
    	printf("%d", 1+1);
    	return 0;
    }
    This calculates the number 2 (as the sum of 2 integers) but it takes a while to do so.
    You might want to do something inside the loop, otherwise your compiler may just optimize all three loops away to nothing.
    Code:
    while( time( NULL ) != -1 )
        printf("thinking...\n");
    printf("got it! 1+1 is %d!\n", 1+1 );
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hilarius View Post

    This calculates the number 2 (as the sum of 2 integers) but it takes a while to do so.
    And maybe not - if compile optimization is on
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Aww... this thread is becoming like my nickname....

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    thanks for that

    But it be nice if code would be ~100 or more lines..

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by auryc
    But it be nice if code would be ~100 or more lines..
    Looks like you did not get the drift. Read the homework guidelines and do your own homework before asking for help.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM