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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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

  3. #3
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    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.
    I like your logic statements, but I dislike the way they're joined together. It's sort of like a magician. He's NOT actually doing magic, but it seems like he might be (or is), because of the artful way he misdirects your eye, so you can't see what he's really doing, even though he's right in front of you.

    That's what you code does for me - it misdirects me just enough to miss some logic actions that we need.

    I've tried to leave out some syntax items you'll need to add, and I haven't run this program yet, so expect some bugs.

    Your previous program was the "I sat down at the keyboard and typed up some code w/o having worked out how to find prime numbers, step by step, by hand, first".

    This program I wrote is the same - and dangerous because I don't work with square roots and prime's, and haven't written a program to do this in a VERY long time.

    When you craft code on paper first, you get the quality product you're capable of. When you sit down and pound keys first, you get only as good as your previous knowledge extends, and little improvement.

    Humans are very lazy and find efficient ways to do repetitive work with the tools they can get: Quicksort is a fine example.

    Computers are very dumb, and will happily do a Bubblesort on a 10million records sized database, on a regular basis.

    /* program to find first 100 prime numbers */
    Code:
    #include <stdio.h>
    #include <math.h>  /* for square root function */
    int main(void)
    {
    
      int counter, test_n, divisor;
      counter = 100;
      divisor = 3;
      printf("\n\n")
      for (test_n = 1; test_n < 4; test_n++)
           printf("Our prime numbers are &#37;7d ", test_n) 
    
      while (counter) 
    
      {
          
          max = sqr(test_n)       /* this is the max number we have to test for primeness */
          while (divisor < max)   /* main inner working loop */
          {
                if (test_n % divisor == 0) {
                     is_prime = 0  /* test_n is not prime */       		
                     break
                }
                else  
                     is_prime = 1  /* OK, maybe it is a prime after all */
    
                divisor = divisor +1;  /* this should be changed to improve the program. How'd you change it? */
          }
         
          if (is_prime) {  /* it's a prime number */
               counter--       /* print the prime and continue countdown to 100 primes  */
               printf("Our prime numbers are %7d ", test_n) 
          }
                
    
          /* reset divisor and is_prime, increment test_n,  */
          divisor = 3 
          test_n++  
          is_prime = 0
    
      }
    
    
    
      return 0;
    }
    Last edited by Adak; 05-13-2007 at 02:47 AM.

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, 02: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, 03: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