LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2006, 01:43 PM   #1
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Rep: Reputation: 15
I can compile this program, but its kind of stuck, can anyone see whats wrong?


#include <iostream>
#include <cmath>
using namespace std;

main()
{
cout << "This program calculates the prime numbers between 0 and ";

int roof, numerator, denominator;
cin >> roof;
int score[roof]; //If the value 2 or 1 exist in one of the arrays, then the score[--number] is a prime number.

for (numerator = 1; numerator <= roof; numerator++)
{
denominator = 1;

for (denominator; denominator <= roof; denominator++)
{
if (numerator%denominator == 0)
{
score[numerator--]++;
}
}
}
cout << score[0] << endl;
cout << score[1] << endl;
cout << score[2] << endl;
cout << score[3] << endl;
cout << score[4] << endl;
}



Im trying to do this. If i make the roof to 6, then I just want to make these calculation.
% is modulus
!= no score

1%1 = score
1%2 !
1%3 !
1%4 !
1%5 !
1%6 !

2%1 = score
2%2 = score
2%3 !
2%4 !
2%5 !
2%6 !

3%1 !
and so on until 6%6
 
Old 03-11-2006, 02:51 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I'm having trouble understanding what you're trying to do in this program but the reason it's gettings stuck is that the statement
Code:
score[numerator--]++;
decrements the numerator variable so it never gets past roof. Thus numerator<=roof is always true.

And please use the [code] tag to make you code more readable
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What kind of program should I use... r_squared Linux - Software 3 03-07-2005 10:08 AM
whats wrong with my fsck? Dillius Mandriva 5 02-19-2005 04:18 PM
Whats wrong with my lilo? mrgrieves Linux - Software 5 07-06-2004 02:31 AM
Whats wrong with my network? erik_wout_ew2 Linux - Networking 13 11-13-2003 10:50 AM
Whats wrong with this? OlRoy Programming 2 05-28-2003 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration