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");
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");
Double Helix STL
Oh one more question, from the book I'm reading I get the following code:
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...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; }
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 01:19 AM.
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?
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?
Find a simple algorithm for finding prime numbers. Don't overcomplicate it.
Well, what should the compiler assume?
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.Code:int main(void) { int y; y = 5; /* there is only 1 y, so the compiler knows which one to use. */ return 0; }
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.
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
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
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!
OMG, warn people before you're posting such big images of yourself: http://www.ulillillia.us/aboutme/aboutmefavorites.html
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
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:
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).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; }
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.
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes thats what I thought I had done, maybe I should go over my code to show what I think it should do...
Is there something that I am over looking or am I simply doing something completely wrong here?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; } }