# Prime Number calculation code

• (2 Pages)
• 1
• 2

## 15 Replies - 11305 Views - Last Post: 18 August 2010 - 01:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=186483&amp;s=421d92f3cf70d2280e964fb4619119c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sfloodberg

Reputation: 0
• Posts: 4
• Joined: 08-August 10

# Prime Number calculation code

Posted 17 August 2010 - 10:35 PM

I am in week 6 of C# programming class and having a very hard time. The lab this week is to
Debug the problems and correct the code; which I have done, no problem there.
Here is where I run into the problem, next, after doing so you are required to walk through the code line by line. Prepare a written narrative of what each line performs and compare that to the comments (specifications) given in the source code. Determine whether the code meets the program specifications or not. If not, determine what is required to modify the code so that it does. Update the code.
Modify the code so the prime numbers are stored in a file. Then, retrieve the values and calculate the mean of the prime numbers found.
I look at the code and go black I can't figure out what is wrong, if there is something wrong and how to adjust the to modify the code so that as a prime number is found, it is written to a file. Then, input the values from the file, calculate the average of the prime numbers, and display the value (with an appropriate message).
Attached is the code with the comments already added. I am free to do a online chat tutor if you’re up to it!!
Thanks!

Is This A Good Question/Topic? 0

## Replies To: Prime Number calculation code

### #2 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

## Re: Prime Number calculation code

Posted 18 August 2010 - 05:02 AM

1. We are not a code writing service.
3. So what part of the code have you changed? That looks like the document that was given to you by the professor and it looks like you have made no changes to it.

It looks like you have not even tried to do anything to help yourself. We are happy to HELP you, but we aren't going to write code for you or hold your hand. YOU have to actually try.

• Saucy!

Reputation: 6245
• Posts: 24,013
• Joined: 23-August 08

## Re: Prime Number calculation code

Posted 18 August 2010 - 06:19 AM

Here is the content:

Quote

Here is the  HYPERLINK [url=http://www.devryu.net/ec/courses/49364/CRS-CIS170B-4171043/lab_files/PrimeNumberLabSourceCode.txt[/url] "new" source code  for this lab.  (Click on the link and copy it to a Console Application code window.)  You will do the following to that code:
I intentionally placed a few bugs in the program.  Debug the problems and correct the code.
After doing so you are required to walk through the code line by line.  Prepare a written narrative of what each line performs and compare that to the comments (specifications) given in the source code.  Determine whether the code meets the program specifications or not.  If not, determine what is required to modify the code so that it does.  Update the code.
Modify the code so the prime numbers are stored in a file.  Then, retrieve the values and calculate the mean of the prime numbers found.

```public static void Main()
{
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
bool blnPrime;
int intDivisor;
short intNumberToBeTested;
for (intNumberToBeTested = 2; intNumberToBeTested <= 100;  intNumberToBeTested++)
{
blnPrime = true;
for (intDivisor = 2; intDivisor < intNumberToBeTested; intDivisor++)
if (intNumberToBeTested % intDivisor <= 0)
blnPrime = false;

if (blnPrime == true)
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}
dblAverage = (double)intPrimeTotal / intPrimeCount;
Console.WriteLine(dblAverage);
}
}

```

My suggestion: if you're looking at the code and "going black", then perhaps this isn't for you. Drop the class before you fail.

### #4 fonephixer

Reputation: 0
• Posts: 26
• Joined: 21-February 09

## Re: Prime Number calculation code

Posted 18 August 2010 - 06:48 AM

I am not fluent in C#, however, this code looks pretty straight forward. I would leave any coding questions to the guys above, and do respect the idea of doing your own work, especially for a class.

My questions would be, what part of this problem are you confused with. I see a few missing {} in your for and if statements, but as I say, I am not sure if this is legal or not in C#.

if you are confused about Primes, I always remember a line from my favorite movie Contact. "Primes - numbers that are only divisible by one and themselves" - Ellie Arroway. So the list of Primes between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
[ copied from http://primes.utm.ed.../small/1000.txt ]

if it is the % that is giving you difficulty, I believe this is the modulus operator, which will give you only the remainder of a standard division, therefore, 5 % 2 would be 1 ( 5 / 2 = 2 r = 1).

see if this helps, if not, please post more, and show us the attempts you've made.

:-)

This post has been edited by fonephixer: 18 August 2010 - 06:51 AM

### #5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5101
• Posts: 9,283
• Joined: 08-June 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 07:41 AM

Quote

I intentionally placed a few bugs in the program. Debug the problems and correct the code.
After doing so you are required to walk through the code line by line. Prepare a written narrative of what each line performs and compare that to the comments (specifications) given in the source code.

This is why it hurts you more than helps you for us to fix your problems. You wouldn't be able to write that narrative or debug the code if we did it for you.

Your professor is trying to teach you something. Go to him or your TA and tell them you're having trouble. They'll probably help you out.

### #6 Robin19

Reputation: 272
• Posts: 552
• Joined: 07-July 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 08:28 AM

fonephixer, on 18 August 2010 - 05:48 AM, said:

I see a few missing {} in your for and if statements, but as I say, I am not sure if this is legal or not in C#.

It is legal in this sense. If a for or if statement doesn't have brackets, the compiler assumes the next line of code (and only the next line of code) is what should be looped or if applied. It is still debated on whether it is better to have the brackets or not have the brackets.

### #7 sfloodberg

Reputation: 0
• Posts: 4
• Joined: 08-August 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 08:54 AM

If you read the attachment I made for the post, I didn't ask you to write the code, nor did I ask for you to solve it for me. I asked for you to possible assist me in understanding of the code. I have already debugged it, that isn't an issue. I can't believe that only after 6 weeks of trying,#2 JackOfAllTrades says "My suggestion: if you're looking at the code and "going black", then perhaps this isn't for you. Drop the class before you fail." Rude, I am asking for assistance not for you to do it for me. I have been trying all week, this is my first post, I didn't know to add what I have done already, It is now week 7 and it is almost over and I am still working on it.

### #8 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:00 AM

well what part don't you understand? Variable declarations? For loops? Incrementing numbers? Math operations? Printing to the command prompt? Understanding what a prime number is?

### #9 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5101
• Posts: 9,283
• Joined: 08-June 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:03 AM

You might consider it rude, but you're not the one here every day dealing with dozens of posts begging us to do their work for them. Most of the question-answerers on this site are professional developers. Even if we did help you with your homework, it probably wouldn't do you much good. We solve problems the best way we know, not the way your professor just taught. So you'd fail, because we'd show you something you haven't learned yet, and your professor would know you had external help.

The best advice I can give you is to talk to your professor, TA, or other classmates. Helping with homework is a tricky subject, for reasons I've already mentioned. This site isn't a tutoring site. We're here to help people with coding issues.

Good luck with your assignment and class.

### #10 sfloodberg

Reputation: 0
• Posts: 4
• Joined: 08-August 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:10 AM

What I am not understanding is how the variables work together, for instance
double dblAverage;
int intPrimeCount = 0;
int intPrimeTotal = 0;
Wouldn't intPrimeCount = 0, be a variable that is made by the developer. In html, there are variables that are used by every coder that never change that do what it says to do, in these programming languages it seems that you can make then say however and do whatever it is that you want. If that is the case then how do I know what kind of variable to write? I know that might sound like I am talking in circles. Sometimes I dissect things so much I miss what is actually shown. Sometimes when I look at the code I can't tell if it is just being declared or actually doing something. The 'int' part of code, I haven't understood from day one.
double dblAverabge; // it realy isn't doing anything but declaring here right?
Did I make any sense?

### #11 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5101
• Posts: 9,283
• Joined: 08-June 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:27 AM

I think you're confused by what a "variable" is. There is no such thing as a variable in HTML. A variable is basically a container you can store a value in. C# uses what is called "strongly typed variables", which means that you can only store a specific type of data in them, depending on how they are declared.

So, to break down a declaration:
```int intPrimeCount = 0;
```

int means that the variable we're declaring can only hold Integer values. intPrimeCount is the name of the variable. It's what we will use to refer to it later. The reason that it has "int" as part of its name is that your professor is using what is known as Hungarian notation. Note that this has absolutely no bearing on the variable itself, except for the name. = 0 sets the variable we just declared to a value of zero. Later, we can retrieve that value, or replace it with a new one.

```double dblAverage;
```
is doing exactly the same thing, minus the last step. This variable is not given a value right away. This variable is able to hold data of type double, which for our purposes means that it can have decimals.

Hope this helps.

This post has been edited by insertAlias: 18 August 2010 - 09:27 AM

### #12 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:33 AM

yes, you can make variables whatever you want. You start by determining a type for the variable. Is it just characters? If so, it's a "string". Is it a number without a decimal? If yes, normally an "int" will work(depending on how large the number is). Is it numberic with a decimal? You can use either "decimal" or "double". There are a number of different types that you can use. You have to decide which is best for the occasion.

For the variable name, just give it whatever. Normally, you will want to give it a descriptive name so you will know what it is for. If you just named a variable "a", then later in the code, you will have no idea what that is for.

And this may sound harsh, but if you are 7 weeks in and you still haven't grasped the concept of variable declarations, GO TO YOUR PROFESSOR AND LET HIM KNOW. He may advise you on dropping the class and trying again next semester. You should definitely go talk to your professor and let him know. He should be able to give you guidance on what you should be doing next.

• Saucy!

Reputation: 6245
• Posts: 24,013
• Joined: 23-August 08

## Re: Prime Number calculation code

Posted 18 August 2010 - 09:33 AM

Sorry if you think I'm rude, but judging by the assignment you should be far past the point of understanding variables. Which just sort of goes to prove my point.

Not everyone has the mindset or ability to be a programmer. There's no shame in that. Read the pertinent link in my signature for more information.

### #14 sfloodberg

Reputation: 0
• Posts: 4
• Joined: 08-August 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 10:08 AM

My professer already knows I am having issues and there are much more in the class, if I drop the class I loose my finacial aid, so I would rather finish. Not to mention that "google it" is a common answer. I understand everything else, but that, loops, if/else, methods etc. It just wasn't clicking with variables and also calling files(which we just learned in week 6). The way you described it,insertAlias, really helped a lot. I just needed it explained differently. I am doing this all online. I am sure that if I was in a class room I wouldn't be having such a hard time. I just went from learning html on my own a few years back to pseudocode class to C#, I just needed a little help. Once I have it, I have it and it's cake later. This is more of the issue that I am having when I say "having the variables work together", the first example was a little elementary, it is more examples like this one:
for (intDivisor = 2; intDivisor < intNumberToBeTested; intDivisor++)
if (intNumberToBeTested % intDivisor = 0)// this line is a little bit more understandable. The other to lines throw me a bit.
blnPrime = false;

This post has been edited by sfloodberg: 18 August 2010 - 10:12 AM

### #15 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5101
• Posts: 9,283
• Joined: 08-June 10

## Re: Prime Number calculation code

Posted 18 August 2010 - 10:17 AM

blnPrime = false shows an assignment to a variable that already exists. In this case, it's a "boolean" value, which means it can be one of two values: true, and false.

```for (intDivisor = 2; intDivisor < intNumberToBeTested; intDivisor++)
```

This is what is called a "for loop", and it does quite a few things. First, it assigns the value 2 to intDivisor. Then, it says "loop while intDivisor is less than intNumberToBeTested. Then it says, "at the end of each block, add one to "intDivisor".