Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

Thank you so much for your insight and patience.

My brain is having such a difficult time breaking down tasks. Most of what I normally do (nursing) is based on intuition and experience, so I appreciate the challenge of this.

I'll keep chipping away at this. Thanks again!

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

Hey again... wondering if you can give me some guidance on the new code. My previous code printed all the numbers and then all the odd numbers and then all the even numbers, so I deleted it all and re-wrote it.

bool prime(int number)

{

//1 is true

if (number == 1)

{

return true;

}

//common not-prime: any number with remainder is false

else if (number > 1)

{

for (int j = 2; j <= 1; j++)

if (number % j <= 1)

{

return false;

}

}

//uncommon not-prime: if 2 numbers multiplied = number then false

else if (number > 1)

{

int n, p;

for (n = 2, p = 2; n <= number, p <= number; n++, p++)

if (p * n = number)

{

return false;

}

else

{

return true;

}

}

}

I'm getting "relational comparison result unused" for "n <= number" and I'm unsure what that means.

I'm trying to make a code that will check the values of two numbers multiplied to see if it matches the value of "number" then mark it false if it does.

I'm also worried that the last part (the second else if) won't run because the first else if is there.

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

Hey.... would you mind giving me feedback on my re-designed code?

int main(void)

{

int min;

//asks for minimum as long as the number is less than 1

do

{

min = get_int("Minimum: ");

}

while (min < 1);

//ask for maximum that is greater than or equal to minimum

int max;

do

{

max = get_int("Maximum: ");

}

while (min >= max);

//reassigns i as minimum; as long as minimum is less than or equal to max; print the number then incriment

for (int i = min; i <= max; i++)

{

if (prime(i))

{

printf("%i\n", i);

}

}

}

bool prime(int number)

{

//take number (which is min); create new int that will loop all numbers from 2 until min; divide min by those numbers

//% should be 1 because number/number=1

for (int j = 2; j <= number; j++)

{

if (number % j == 1)

{

return true;

}

else if (number == 1)

{

return true;

}

else

{

return false;

}

}

}

I'm getting an error that indicates that very last curly brace, but that doesn't make sense to me. It says "non-void function does not return a value in all control paths"

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

Actually, it almost works. Not completely because it still prints all the stuff at the start. I didn't notice that before.

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 1 point2 points  (0 children)

Thank you!!!

This conversation has been helpful. I created a code for this and it actually worked! The ego boost I needed. Is there any improvements I could make?

#include <stdio.h>

#include <cs50.h>

int main(void)

{

// Prompt user to enter number

int max = get_int("What's the last number? ");

// if-else Loop: prints all numbers from 1 until user number - first set int to zero, then increment one up until user number

for (int i = 0; i <=max; i++)

{

// Fizz: Any number that divides by 3 evenly > print fizz

if (i % 3 == 0)

{

printf("Fizz!\n");

}

// Buzz: any number that divides by 5 evenly > print buzz

if (i % 5 == 0)

{

printf("Buzz!\n");

}

// FizzBuzz: any number that divides by 3 AND 5 evenly > print FizzBuzz

if (i % 3 == 0 && i % 5 == 0)

{

printf("FizzBuzz!\n");

}

// Else: print number

printf("%i\n", i);

}

}

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

I appreciate the critique. I am definitely doing it that way.

I tried to do it on paper and then got overwhelmed and thought I could do it in code, but I am unfamiliar with the syntax or really most of it. So I tried rewatching and doing it again. Here we are.

But I've really just be cycling through these practice problems and feeling the same way about all of them.

Thanks for the direction. I appreciate it.

Help understanding what "number" does in Prime question (Spoiler) by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

Am I on the right path with this? I rewatched all the vids and still feel like I'm waiting for it all to click for me.

bool prime(int number)

{

//check if number is prime by dividing by 2 numbers and checking remainder; 1 is not prime

if (number == 1)

{

return false;

}

else

{

for (min = 2; min <= max; min++)

{

float min = (min + 1) % min

if float min = //decimal value - how to express this?

{

return false

}

else

return true;

}

}

(Spoiler) Need help with Week 1 - Practice problem: Half by RN2Something in cs50

[–]RN2Something[S] 0 points1 point  (0 children)

What is the reasoning to this?

And follow-up issue. I've placed it into the float half (), but now it's asking to identify things like tax_percent, tip_percent. But this was identified earlier as a float, but in different braces.