Computing.Net > Forums > Programming > 2 questions, 1 post

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

2 questions, 1 post

Reply to Message Icon

Name: Deimos
Date: February 23, 2008 at 12:04:02 Pacific
OS: MS-DOS just kidding! Win
CPU/Ram: AMD Athlon x2 5200+ / 2GB
Product: Custom made
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: February 25, 2008 at 18:27:58 Pacific
Reply:

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 from

And 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.


0
Reply to Message Icon

Related Posts

See More


Editing a txt file, renam... VB 6 Question



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: 2 questions, 1 post

2 questions on NQC www.computing.net/answers/programming/2-questions-on-nqc/12810.html

Simple C question www.computing.net/answers/programming/simple-c-question/12616.html

A couple of batch file questions www.computing.net/answers/programming/a-couple-of-batch-file-questions/17648.html