Preparing for First Year in Computer Science - Page 4 - Studies and Job Offers - HWzone Forums
adplus-dvertising
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Preparation for first year in computer science


ggg123

Recommended Posts

I ran a simple tracking table of values ​​from 2 to 4. The initial numbers are 2 and 3, which means the output should be 5 / 2 = 2.5. 2.5 did indeed appear in the tracking table.

I found in my code an error in the second loop (I wrote n

Somehow the code still takes me out of 3.0. Apparently the code I wrote does not work the way I think and I can not find why. I suspect the Boolean variable is not sure.

- - - Unified response: - - -

My mistake, you're right.

In any case, you do not lack any intermediate stage. Just a little experience. You learn only through your fingers.

It is also advisable to use meaningful names for variables. It's hard to understand what s_prime is and what n_prime is

Another thing:

else if (i <= 1) {
continue;
}
else

It would have been better if you put the big block in the first else (if i is greater than 1) and then have no else continue.

Is it better to call variables with longer names such as number_of_prime that are more likely to be mistaken during the code?

Or is it better to leave n_prime and simply explain in the note the intention?

As for the proposed amendment of the code - I changed to what you suggested. A fundamental question - if I had left as I wrote, what would it have been on the computer? Longer run time of the program?

Link to content
Share on other sites

  • תגובות 151
  • Opens on
  • Last comment

You have 2 options to debug - or add prints of values, or use the built-in debugger and follow the different variables.

Regarding the amendment of the Code - a. Yes, another action is ongoing. B. It's more readable and clear, the code is shorter. Logically it is also easier to understand what happens when a certain condition is met, and not vice versa.

Variable names - It is more common to use the camleCase method so that you write the words connected and use a capital letter for each new word. For example: numOfPrimes, buttonArray. Note that in Excel you have a / alt shortcut to complete common words

Link to content
Share on other sites

Okay I printed values ​​after each update of the variables.

As I thought, there is some sort of problem with the Boolean variable that I can not understand.

I entered values ​​2 and 4. For 2 and 3 there is no problem, the program identifies them as primary and everything prints fine.

For the 4 value, because what is the prime void variable remains true and therefore the numOfPrimes and sumOfPrimes variables are updated and therefore the output error.

Here is the code of the important program (the instructions for printing the temporary variables until I understand the problem):


import java.util.Scanner;
public class EX2 {


public static void main (String [] args) {
Scanner s = new Scanner (System.in);
int x = s.nextInt ();
int y = s.nextInt ();
s.close ();
int sumOfPrimes = 0;
int numOfPrimes = 0;
int first_prime = 2;
boolean prime;

for (int i = x; i <= y; i ++) {
if (i == first_prime) {
sumOfPrimes = sumOfPrimes + i;
numOfPrimes ++;
System.out.println (sumOfPrimes + "/" + numOfPrimes);
}
else if (i> 1) {

for (int n = 2; n
if (i% n! = 0) {
prime = true;
}
else
{
prime = false;
break;
}
}
if (prime = true) {
sumOfPrimes = sumOfPrimes + i;
numOfPrimes ++;
System.out.println (prime);
System.out.println (sumOfPrimes + "/" + numOfPrimes);
}
}
}

if (numOfPrimes> 0) {
double average = sumOfPrimes / (double) numOfPrimes;
System.out.println (average);
}
else
System.out.println ("0");
}


}



This is a code section that probably does not work as I thought:


else
{
prime = false;
break;
}

I guess I have some basic mistake here that I do not understand.

Link to content
Share on other sites

Enters the name, the Boolean variable is indeed updated to false but for some reason it later returns to true and updates the variables.

Here is the output (for X = 2 and Y = 4):

true

2/1

true

5/2

false

true

9/3

3.0

- - - Unified response: - - -

Update: I've fixed the issue. I would have to write in the condition of the IF checking if the prime number is not prime = true and the boolean variable must be initialized to true or false.

Unfortunately, I can not understand why the if should be written like this. The Boolean variable should be true or false, and then if the condition is equal to true, it would have to be modified. I do not understand why it did not work?

And why when I change the condition in if to prime, I must initialize the variable at the beginning of the program? After all, he gets a value in the loop.

Link to content
Share on other sites

Fine, you found the problem :)

A common mistake for beginners is to use the = operator as a test if x = y. In practice, this is placement - you have entered the value for true and you have not checked if it is true.

The expressions prime == true and prime are equivalent because the prime expression returns a boolean value. Also, prime == false equals prime!

Link to content
Share on other sites

Wow, I was a total idiot. The segment that operator = is used for positioning and == used for equality is very misleading. You really have to change your way of thinking.

You knew from the beginning that this was the mistake and did not say, eh? :)

just kidding :). The way you helped me find the problem greatly helped me. In the end, I realized that doing a follow-up table and printing variables (things I had not thought about before) helps you understand how the program works and also find the problem.

By the way, I still did not understand why you should initial the prime at the beginning of the program. After all, he gets value in the loop, so why? If it is not initialized, the program does not fold.

Link to content
Share on other sites

Each type gets a dipole value if it is not initialized. Bollian, for example, is initialized to false. This is not a compilation error.

In any case it is good practice to initialize variables. What if the loop is not done at all?

Link to content
Share on other sites

Each type gets a dipole value if it is not initialized. Bollian, for example, is initialized to false. This is not a compilation error.

In any case it is good practice to initialize variables. What if the loop is not done at all?

I did not get it. If Bollian is formatted with a dipole to false, why does it give me an error when I do not initialize it?

Link to content
Share on other sites

This is the error

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The local variable prime may not have been initialized

at Ex2.main (Ex2.java:32)

Appears on the prime within the parentheses of the if.

Link to content
Share on other sites

Only class fields are automatically initialized. Internal variables of functions you must reformat yourself, otherwise the compiler will give an error.

But the variable gets a value later in the function.

I have already used variables that I did not initialize but only declarative, and then they received values ​​and everything was fine.

I do not understand why this particular case is not giving me.

Just an example in the specific case in my program - I declared and gave a value to double average only at the end of the program.

Equally I could only declare an average at the beginning of the program, and at the end give the value. For that he does not give me an error.

If so, why does not it work in the same way with the Boolean variable I have declared?

- - - Unified response: - - -

Update: I think I understand.

If the loop does not exist, my Boolean value does not get a value. That's why I need to initialize it (although I do not even use it if the loop does not exist).

Am I right?

Link to content
Share on other sites

ארכיון

This discussion has been archived and new comments can not be added.


×
  • Create new ...

At the top of the news:

new on the site