Help me with my basic program, nothing I create will run

This is a discussion on Help me with my basic program, nothing I create will run within the C Programming forums, part of the General Programming Boards category; If you want the loop to break at a certain point, just place an if with a break. Code: int ...

  1. #16
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    If you want the loop to break at a certain point, just place an if with a break.

    Code:
    int test = 1; /*1 is true hence infinite true while loop*/
    
    while ( test )
    {
       test++;
    
       if ( test > 10 )
       {
          break;
       }
    }
    
    printf("Loop broke as test reached 10\n");
    I'm just trying to be a better person - My Name Is Earl

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    45

    Arrow

    Oh one more question, from the book I'm reading I get the following code:
    Code:
    int main(void)
    {
       int x;
       x = 10;
    if (x == 10) {
       int x; /* this x hides the outer x */
    
       x = 99;
       printf("inner x: %d\n", x);
    }
       printf("outer x: %d\n", x);
    return 0;
    }
    Does this mean that X takes the function replaces the x with a new value and then it returns to the previous value of x when the function is over with. Also does this mean that functions can affect other functions (without the second int x; x would have the same value as it would outside right?) So... I'm not sure if I asked that right and I think I confused myself writing that...

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What do you want to actually do with the original block of code? Just search for prime numbers?

    And btw, in your last example, a new x is created. To you, the programmer, it appears that you have two x's, but to the compiler, they are separate variables, with separate addresses. Local variables only last from their declaration until the ending brace of whatever block they are in.
    Last edited by MacGyver; 05-11-2007 at 02:19 AM.

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by swgh View Post
    If you want the loop to break at a certain point, just place an if with a break.

    Code:
    int test = 1; /*1 is true hence infinite true while loop*/
    
    while ( test )
    {
       test++;
    
       if ( test > 10 )
       {
          break;
       }
    }
    
    printf("Loop broke as test reached 10\n");
    Okay I shall break out of all infinite loops. (= I suppose i can do that from now on.

    Question: when my terminal fills up by a program, does my computer have to store all the information on the screen or does it need to just print it to the screen. I would immediatly think the computer must save all the info, but I let my computer run up for quite some time (with the previously seen loop) and the computer seemed to run as it did before, maybe at some point it deletes or replaces some of the old things it stored?

  5. #20
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by MacGyver View Post
    What do you want to actually do with the original block of code? Just search for prime numbers?

    And btw, in your last example, a new x is created. To you, the programmer, it appears that you have two x's, but to the compiler, they are separate variables, with separate addresses. Local variables only last from their declaration until the ending brace of whatever block they are in.


    I just want the program to run, and search for prime numbers yes, after I get through this problem I'll try something more advanced with it.

    By not putting in the second variable, and throwing x somewhere in that function would the computer just assume to use the variable x from the last function, and why is that if so?

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Ravens'sWrath View Post
    I just want the program to run, and search for prime numbers yes, after I get through this problem I'll try something more advanced with it.
    Find a simple algorithm for finding prime numbers. Don't overcomplicate it.

    Quote Originally Posted by Ravens'sWrath View Post
    By not putting in the second variable, and throwing x somewhere in that function would the computer just assume to use the variable x from the last function, and why is that if so?
    Well, what should the compiler assume?

    Code:
    int main(void)
    {
    	int y;
    	y = 5; /* there is only 1 y, so the compiler knows which one to use. */
    
    	return 0;
    }
    Note the giant braces. When you say, y = 5, it looks for y inside the current braces. If there was a global variable also named y, then the compiler would still use the one inside main(). It assumes that you mean the most inner-declared variable.

    It won't look inside other functions, though, for a variable. A valid variable is either inside the current set of braces, inside the current function, or declared globally. That's about it.

  7. #22
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by MacGyver View Post
    Find a simple algorithm for finding prime numbers. Don't overcomplicate it.



    Well, what should the compiler assume?

    Code:
    int main(void)
    {
    	int y;
    	y = 5; /* there is only 1 y, so the compiler knows which one to use. */
    
    	return 0;
    }
    Note the giant braces. When you say, y = 5, it looks for y inside the current braces. If there was a global variable also named y, then the compiler would still use the one inside main(). It assumes that you mean the most inner-declared variable.

    It won't look inside other functions, though, for a variable. A valid variable is either inside the current set of braces, inside the current function, or declared globally. That's about it.
    Ok I can grasp that (=
    I'm thinking searching for an existing algorithm for finding prime numbers would be cheating myself but if thats all theres left to do... (=

    Thanks again,
    -Ravens'Wrath

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Of course, there are many examples of algorithms that find prime numbers. If you don't know how to search for prime numbers with a pencil and paper (and maybe a calculator), then you will need to use somebody else's algorithm.

    You can't expect to program a computer to do something that you don't know how to do, yourself.

    If you do know how to search for prime numbers by hand, then programming a computer to do it is an excellent challenge, and you'll learn a lot, as well.

    I won't let you fail, so don't worry.

    If you want to work with me on this, let me know, and we'll move along, smartly. There's been several good posts here, but they've been geared to a more C refinement to your code, rather than a C "let's solve this problem, learn a bit, and have some fun" kind of approach.

    If you want to go with this, just jot down the steps you use to find a prime, by hand, in either plain English or pseudo code. We'll flush it out from there.

    Adak

  9. #24
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    First, do you know what a prime number is? If not, I'll give a hint in terms of actual prime numbers: 2, 7, 23, and 31 are some small prime numbers.
    If you do know what prime numbers are, do you know how to find out whether a number is prime or not, that meets the definition? If not, you should research it then play a little game of "factors" to practice it (it's a game I made up long ago).
    If you know how to find prime numbers, then write a program that determines how to tell if a number is prime or not. As a treat, try writing a program where you can the game "factors" through merely inputting numbers and totaling up your score while you play.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I DO believe you're the kind of student I always wanted to sit next to during a difficult math test!

    Now, why are you holding back on 1, 3, 11, 13, & 17?

    He's a math geek, AND he's cagey - watch out for this guy!

  11. #26
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    OMG, warn people before you're posting such big images of yourself: http://www.ulillillia.us/aboutme/aboutmefavorites.html

  12. #27
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I was only giving some examples of prime numbers. 1 is not prime though. It's not even composite either.

    That link is to a very old file. This is the newer version, but still over a year old (unlike 2 or so years from that HTML version). Also, please stay on topic as well.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  13. #28
    Registered User
    Join Date
    May 2007
    Posts
    45
    Been a way for one day and so many posts (=
    Thanks for your time in trying to help. And to answer everyones question, yes I know what a prime number is :P Im just having trouble setting up a program that will find them...

    I have made another attempt to try do this but I have had the same problem as before, I can get a program to compile but thats about it, the following is how I'm attempting to solve the problem:

    Code:
    /* program to find first 100 prime numbers */
    
    #include <stdio.h>
    int main(void)
    {
    
    int counter, test_n, divisor;
    counter = 100;
    test_n = divisor = 2;
    
    while (counter) 
    
    {
    
    	if (test_n == divisor)
    		{
     		printf("%d is prime", test_n);
      		counter = counter - 1;
    		test_n = test_n + 1;
    		divisor = 2;
    	 	}
    	else
    		while (test_n % divisor)
    			{
    			divisor = divisor +1;
    			}
    	if (test_n != divisor)
    		{
    		divisor = 2;
    		test_n = test_n + 1;
    		}
    }
    
    
    return 0;
    }
    My program seems to be nearly right... But still doesn't give me all of the first 100 primes (although it does seem to list only primes, haven't checked all though).

    Any ideas why this isn't working...
    by the way, my trash has 76 copies of a.out and they're all attempts at this silly program...

    so any help would be greatly appreciated.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Basically, you are attempting to find the first 100 prime numbers by trial division, right? If so, the first part that you need to get right is: how to determine if an integer greater than 1 is prime using trial division?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    May 2007
    Posts
    45

    Thumbs up

    Quote Originally Posted by laserlight View Post
    Basically, you are attempting to find the first 100 prime numbers by trial division, right? If so, the first part that you need to get right is: how to determine if an integer greater than 1 is prime using trial division?
    Yes thats what I thought I had done, maybe I should go over my code to show what I think it should do...

    Code:
    while (counter) /* This should keep my program going as long as long as my counter is above zero (in other words until my program gets 100 primes */
    
    {
    
    	if (test_n == divisor) /* when my program gets to this point and finds the divisor and the number testing to be equal, the program shall reconginze this number as a prime */
    		{
     		printf("%d is prime", test_n);
       		counter = counter - 1;   
    		test_n = test_n + 1; 
    		divisor = 2;
    	 	}
    	else
    		while (test_n % divisor)  /* this should test whether or not the number can be a prime, if the while statement produces a value that is not true then the program should move on to the second part of the test.
    			{
    			divisor = divisor +1;
    			}
    	if (test_n != divisor)   /* this will prove the number to be prime or not going on results from the while loop above. */
    		{
    		divisor = 2;
    		test_n = test_n + 1;
    		}
    }
    Is there something that I am over looking or am I simply doing something completely wrong here?

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Program help
    By Ronzel in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 03:24 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 04:21 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. program won't run properly, help needed asap
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-16-2002, 09:52 AM

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