C
C#3y ago
daniel1

tough homework

so the first part of the assigment goes like this
80 Replies
daniel1
daniel1OP3y ago
"We define a three-digit positive integer as "beautiful" if the multiplication of its first digit by its last digit is equal to the square of its second digit." so my qustion is am i supposed to just type out a random number between 100 to 999 ? and then after i do that how am i supposed to do the second part of the assigment
Klarth
Klarth3y ago
A definition isn't a problem. What is it asking you to do?
daniel1
daniel1OP3y ago
We define a three-digit positive integer as "beautiful" if the multiplication of its first digit by its last digit is equal to the square of its second digit. The program must receive 20 positive integers and check if they are three digits, if so you must check how many "beautiful" numbers there are. An appropriate message must be displayed. what i am asking is where do i even begin this program how do i defiene an intger as a whole class that needs so many critearies i need to make sure that its three digits and then that multuplication thing
Klarth
Klarth3y ago
Yeah, that's ambiguous as to how you should "receive" the data. I would ask for clarification or go based on how input for previous exercises in the same course were done.
daniel1
daniel1OP3y ago
i did do something similar to this but this is a whole nother level i did some stuff with for and count
Klarth
Klarth3y ago
There's nothing that requires you to write a class. This is an exercise in (potentially) string parsing, array/string indexing, and numeric data types. It's a bit hard to give advice without solving the problem for you. There are a few approaches: 1. requires the number in both string and int form. 2. You can use modulus and division to get the digit. eg. int secondDigit = (num % 100) / 10;
daniel1
daniel1OP3y ago
num1 = num / 100; num2 = num / 10; num3=num % 10 / 10; is this how i am supposed to do the part of dividing i dont exactly rember how to divide corectly num 1 is supposed to be the first number from the left num2 is supposed to be the number in the midle num 3 is supposed to be on the right
Klarth
Klarth3y ago
Second needs a modulus. Try running a some inputs. Like num = 853; and see what each result gives you.
daniel1
daniel1OP3y ago
"num2 = num / 10%10;" like this ?
Klarth
Klarth3y ago
No.
daniel1
daniel1OP3y ago
num%10 ?
Klarth
Klarth3y ago
I suggest you try checking it instead of guessing. The compiler and debugger are great teachers. $debug
MODiX
MODiX3y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Klarth
Klarth3y ago
Demonstrates how to view your app state at runtime instead of wasting time adding in Console.WriteLines to inspect your results.
daniel1
daniel1OP3y ago
int num, count1 = 0,num1,num2,num3,num4,num5; for (int i = 1; i <= 20; i++) { Console.WriteLine("enter a three digit num"); num = int.Parse(Console.ReadLine()); num1 = num / 100; num2 = num / 10 % 10; num3 = num % 10; num4 = num1 * num3; num5 = num2 * num2; } } } } am i on the right path (i have checked the dividing part its now corect) hold on how do i check if num is a three digit number in the first place
Klarth
Klarth3y ago
There's multiple ways. You can check the string representation if it's 3 characters in length (assuming the parse passes). You can check the logarithm. You can check if the number is in a certain range.
daniel1
daniel1OP3y ago
num/100==0 is this it ? i tried 100<=num>=999 but it gave me an eror massge
Klarth
Klarth3y ago
Have you tried it? Because the last one isn't valid syntax.
daniel1
daniel1OP3y ago
but is this working ? is it coreect
Klarth
Klarth3y ago
Try it and see? You don't have to ask a random guy on the internet for something that takes 5 seconds to test.
daniel1
daniel1OP3y ago
static void Main(string[] args) { int num, count1 = 0,num1,num2,num3,num4,num5; for (int i = 1; i <= 20; i++) { Console.WriteLine("enter a three digit num"); num = int.Parse(Console.ReadLine()); num1 = num / 100; num2 = num / 10 % 10; num3 = num % 10; num4 = num1 * num3; num5 = num2 * num2; if (num / 100 == 0 && num4 == num5) { count1++; } Console.WriteLine("count was equal to=" + count1); } what is wrong with this i tried puting it in but it gives me a zero every time i put in 222 2 times 2 equals 4 meaning its supposed to give me a count equals 1 but it kept giving me zero
Klarth
Klarth3y ago
Have you inspected the local state of your variables like the debugging tutorial shows?
daniel1
daniel1OP3y ago
what do you mean by that i know i assigned count1=0 but you have to do it otherwise it wont work
daniel1
daniel1OP3y ago
i am not gusseing i am running it and puting in numbers
Klarth
Klarth3y ago
Are num1 through num5 correct right before the if statement? How can you be sure of that? Something is wrong between your assumptions and the real program state.
daniel1
daniel1OP3y ago
and your sure its inbetween num5 and num1 ? 100<=num<=999
Klarth
Klarth3y ago
Not after actually running it and checking the state. 😛
daniel1
daniel1OP3y ago
what is the problem with this statemant why is it red
Klarth
Klarth3y ago
num1 though num5 seem fine, so the issue is your if statement. Because that's not how inequalities work in C#. You can't have a compound inequality like that (barring pattern matching, but that's not a beginner concept). Can you write that as two inequalities and combine them with a logical operator?
daniel1
daniel1OP3y ago
if (100<=num && num4 == num5&&num<=999) perfecto now it works thenk you for the assinetence
Klarth
Klarth3y ago
👍 Honestly, take some time to go through the debugger. It's the most valuable ~15 minutes of programming-related material you'll ever go through.
daniel1
daniel1OP3y ago
i still dont understand how to use it
Klarth
Klarth3y ago
I don't say that lightly.
daniel1
daniel1OP3y ago
like i mean litarly how to activate it
Klarth
Klarth3y ago
Are you using Visual Studio (Community)? Not Visual Studio Code. They're two separate products.
daniel1
daniel1OP3y ago
the file is called visual studio 2019 its not called comunity and its not called code so i have no idea
Klarth
Klarth3y ago
Ok, good enough. So you find the point where you want to examine the program state, then left click in the gutter to set a breakpoint (red dot). Then run with F5 (Start with Debugging). Then examine the Locals window (below).
Klarth
Klarth3y ago
Klarth
Klarth3y ago
The arrow there is a red dot, normally, when execution isn't stopped there. It's really helpful to check intermediate results and see the code execution path (with stepping or adding many breakpoints).
daniel1
daniel1OP3y ago
Klarth
Klarth3y ago
There are other features in the tutorial, too. Breakpoints, stepping, and call stack are the most important.
daniel1
daniel1OP3y ago
is this it
Klarth
Klarth3y ago
No, you don't need to go there.
Klarth
Klarth3y ago
daniel1
daniel1OP3y ago
what do you mean by the point where i want to examine the program state then left click in the gutter to set a breakpoint (red dot).
Klarth
Klarth3y ago
Say something isn't working how you think it should. Maybe it's a loop. So you put breakpoints before the loop and within, so you can examine the preconditions, and how the loop works each iteration.
daniel1
daniel1OP3y ago
left click on what
Klarth
Klarth3y ago
There's a small bar to the left of the line number that's left clickable. In VS2022, if you hover, a grey dot shows. I'm not sure if that was there in VS2019.
Klarth
Klarth3y ago
Klarth
Klarth3y ago
If I wanted to place one on that line.
daniel1
daniel1OP3y ago
i do have that blue bottle but only on certain lines
Klarth
Klarth3y ago
Not the brush, the dot.
edgarka_
edgarka_3y ago
Just keep left clicking to the left from your code until you see red filled circle
daniel1
daniel1OP3y ago
i dont understand still how do i enter this debuging mod i just left clicked
Klarth
Klarth3y ago
You need to place the breakpoint first, before going into debugging. At least in this scenario.
daniel1
daniel1OP3y ago
and it dealted lines of code
Klarth
Klarth3y ago
Well, undo that. I'm not sure what you did. You're not clicking the right spot.
daniel1
daniel1OP3y ago
static void Main(string[] args) { int num, count1 = 0,num1,num2,num3,num4,num5; for (int i = 1; i <= 20; i++) { Console.WriteLine("enter a three digit num"); num = int.Parse(Console.ReadLine()); num1 = num / 100; num2 = num / 10 % 10; num3 = num % 10; num4 = num1 * num3; num5 = num2 * num2; if (100 <= num && num4 == num5 && num <= 999) { Console.WriteLine("count was equal to=" + count1); }
} now its not working
Klarth
Klarth3y ago
The breakpoint?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
daniel1
daniel1OP3y ago
what breakpoint what does it even look like
Klarth
Klarth3y ago
A literal red dot after you click. Which is what the tutorial I linked before showed, too.
daniel1
daniel1OP3y ago
i dont have that how do i make the program work agien i litarly have the same exact code and its not working
daniel1
daniel1OP3y ago
daniel1
daniel1OP3y ago
what is wrong with the program it worked just fine before i left clicked on the side
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Klarth
Klarth3y ago
You clicked the part that cut lines. I don't know why you didn't just undo.
daniel1
daniel1OP3y ago
man i cant undo i dont know how to undo
Klarth
Klarth3y ago
You have nothing in your if condition now. If you actually read your own code.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Klarth
Klarth3y ago
This area here with the red line should be the gutter.
daniel1
daniel1OP3y ago
what that dosent make sense i can clearly see its filled with words
Klarth
Klarth3y ago
daniel1
daniel1OP3y ago
and leters
Klarth
Klarth3y ago
It executes nothing.
daniel1
daniel1OP3y ago
i tried placing the command isnide
Klarth
Klarth3y ago
Because there's nothing in the body.
daniel1
daniel1OP3y ago
oh yeah it works now thx
Klarth
Klarth3y ago
Anyways, you can start a new project to play around with the debugger.
Want results from more Discord servers?
Add your server