Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey again.
-----------first question-----------
My first question is about a program that is supposed to find prime numbers until an, user specified, max.
I've used the Eratosthenes's algorithm, that consists in:
- create a list of numbers since 2 to the max value;
- the first number on that list is prime(2);
- remove all multiples of the first number on the list(2);
- do this until the last number checked is equal to the square root of the original max, the next number checked would be 3 and then 5 (4 had been removed for being a multiple of 2);Here's my source code:
#include<iostream>
#include<cmath>
using namespace std;int main()
{cout << "\t*********************************\n"
<< "\t**Bem vindo ao PrimeFinder v1.0" << (char) 169 << "**\n"
<< "\t*********************************\n\n"
<< "This program is protected by the universal copyright laws\n";
int allNumbers[300];
int numNumbers = 0;
int max;
int maxPrime;
cout << "Inserir numero maximo a encontrar numeros primos (<300) -> "; cin >> max;
maxPrime = sqrt(max);
for(int i = 2; i <= max; ++i)
allNumbers[numNumbers++] = i;
for(int d = allNumbers[0]; d < maxPrime; ++d)
{
for(int h = 1; h <= (numNumbers-1); ++h)
{
if(allNumbers[h+1] % d == 0){
allNumbers[h+1] = 0;
}
}
}
cout << "Os primos ate' " << max << endl;
for(int l = 0; l<(numNumbers-1); ++l)
{
if(allNumbers[l] == 0)
continue;
else
cout << allNumbers[l] << endl;
}
system("Pause");
}Note: Just ignore the Portuguese strings.
As deleting elements from an array is impossible I've associated them with an '0'.
I think it all makes sense but sometimes the program also associate 5 and 7 as 0, not counting them as primes, why?
-----------second question-----------
I need to make a program that allows the user to "fly" an asterisk around the screen.
My original idea was to create a couple of ints to hold the x and y coordinates:
int xpos = 0;
int ypos = 0;Then create a while(true) loop so that the program never ends;
Detect the arrow keys press using the getch() function.(UP -> 72; DOWN -> 80; LEFT ->77; RIGHT -> 75)
And finally use a couple of for loops(a x and a y loop) to cout a number of spaces equal to the xpos and a number of carriage returns equal to the ypos and at the end cout an asterisk...but it doesent work at all as I predicted & I can get the asterisk to move up...any ideas?Best Regards

Some minor English quibbles:
I've used the Eratosthenes's algorithm, that consists in: --> which consists of
create a list of numbers since 2 to the max value; --> a list of numbers fromAnd now onto the content of the message...
Note: Just ignore the Portuguese strings.
Done and done.As deleting elements from an array is impossible I've associated them with an '0'.
Not impossible, just a pain. I should probably point out both vectors and lists have an erase function.I think it all makes sense but sometimes the program also associate 5 and 7 as 0, not counting them as primes, why?
After quickly glancing over your code, I'd say it's because the nested loops will notice that (5 % 5 == 0).I can get the asterisk to move up...any ideas?
Windows implementation:#include <windows.h>
void putChr(short x, short y, char c) {
::COORD pos;
pos.X = x; pos.Y = y;
::SetConsoleCursorPosition(::GetStdHandle(STD_OUTPUT_HANDLE), pos);
std::cout << c;
}If you're using *NIX, you'll need to either use curses.h or ncurses.h.

![]() |
Editing a txt file, renam...
|
VB 6 Question
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |