Could someone help me debug this program, only the else part is processed on every input.
This id a program for grading students. a student inputs a mark and the the grade is displayed
func main(){
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d",&x)
if (100 <= x) && (x<=75){
fmt.Println("D1")
}else if (74 <= x)&&(x <= 70){
fmt.Println("D2")
}else if (69 <= x )&&(x<=65){
fmt.Println("C3")
}else if (64 <= x)&&(x <= 60){
fmt.Println("C4")
}else if (59 <= x)&&(x <= 55){
fmt.Println("C5")
}else if (54 <= x)&&( x<= 50){
fmt.Println("C6")
}else if (49 <= x )&&(x<= 45){
fmt.Println("P7")
}else{
fmt.Println("Work harder")
}
}
You have a logic problem.
Change
if (100 <= x) && (x<=75){
to
if 75 <= x && x <= 100 { // numbers here are ordered from smallest to greatest
because a number can't be greater than 100 and smaller than 75.
And it's the same for the other lines of course.
Note that you could make less comparisons. Suppose you test if the number is smaller than 100 initially, then you don't have to test if it's smaller than 75 just after you tested it's smaller than 75.
A typical Go code would probably have a switch here instead of all those if/else. See switch in the documentation. Here's how it could be written with a switch :
switch {
case x > 100:
fmt.Println("Congrats!") // you forgot this one
case x >= 75:
fmt.Println("D1")
case x >= 70:
fmt.Println("D2")
case x >= 65:
fmt.Println("C3")
case x >= 60:
fmt.Println("C4")
case x >= 55:
fmt.Println("C5")
case x >= 50:
fmt.Println("C6")
case x >= 45:
fmt.Println("P7")
default:
fmt.Println("Work harder")
}
A last comment : This type of switching code rarely occurs because normally the thresholds and related notes are stored as data, for example in a struct.
Your IF statement says:
if 100 is less than x (which means x has to be greater than 100)
AND
x less than (or equal to) 75
do this--
x will never be greater than 100 AND less than 75, so it always does the ELSE ...
all if and if else statements provided in this code are logically incorrect
example:
if (100 <= x) && (x<=75)
here if (100 <= x) is false, the compiler will not even consider the next condition.
This is called SHORT CIRCUIT, the lazy evaluation of condition statements.
this is also applicable to OR conditions i.e in case the first condition is true the second condition will not be evaluated.
So according to the code you have written, the ideal solution will be
func main() {
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d", &x)
if (100 >= x) && (x >= 75) {
fmt.Println("D1")
} else if (74 >= x) && (x >= 70) {
fmt.Println("D2")
} else if (69 >= x) && (x >= 65) {
fmt.Println("C3")
} else if (64 >= x) && (x >= 60) {
fmt.Println("C4")
} else if (59 >= x) && (x >= 55) {
fmt.Println("C5")
} else if (54 >= x) && (x >= 50) {
fmt.Println("C6")
} else if (49 >= x) && (x >= 45) {
fmt.Println("P7")
} else {
fmt.Println("Work harder")
}
Thankx finaly got with your help. hope it helps someone else some time in future
package main
import "fmt"
func main(){
var x int
fmt.Println("Enter your marks")
fmt.Scanf("%d",&x)
if (75<= x) && (x<=100){
fmt.Println("D1")
}else if (70 <= x)&&(x <= 74){
fmt.Println("D2")
}else if (65 <= x )&&(x<=69){
fmt.Println("C3")
}else if (60 <= x)&&(x <= 64){
fmt.Println("C4")
}else if (55 <= x)&&(x <= 59){
fmt.Println("C5")
}else if (50 <= x)&&( x<= 54){
fmt.Println("C6")
}else if (45 <= x )&&(x<= 49){
fmt.Println("P7")
}else{
fmt.Println("Work harder")
}
}
Related
The answer that I want should be 88.5 but it turns out to be 3.60434e+006. I think there's no problem with my quotient formula. What should I do?
#include <iostream>
using namespace std;
int main()
{
int Grade_one, Grade_two;
int Average = Grade_one+Grade_two;
double average = Average/2;
cout<<"Please input your Grade No.1: ";
cin>>Grade_one;
cout<<"Please input your Grade No.2: ";
cin>>Grade_two;
if (Grade_one == Grade_two){
cout<<"Your Grades are Same."<< endl;
}
if(Grade_one >= 50 && Grade_two >= 50 && Grade_one <= 74 && Grade_two <= 74){
cout<<"Hey! You should focus on your study, your grade is concerningly LOW."<< endl;
}
else if(Grade_one <= 49 && Grade_two <= 49){
cout<<"DUDE! you will fail for sure if you don't study."<< endl;
}
else if(Grade_one >= 75 && Grade_two >= 75 && Grade_one <= 100 && Grade_two <= 100){
cout<<average <<endl;
}
return 0;
}
"result"
Please input your Grade No.1: 88
Please input your Grade No.2: 89
3.60434e+006
Process returned 0 (0x0) execution time : 3.238 s
Press any key to continue.
The answer should be 88.5. How should I fix this?
Grade_one and Grade_two undefined when you first use them. You also need to cast Average to double if you want the result to be double as well.
#include <iostream>
using namespace std;
int main()
{
int Grade_one, Grade_two;
cout<<"Please input your Grade No.1: ";
cin>>Grade_one;
cout<<"Please input your Grade No.2: ";
cin>>Grade_two;
int Average = Grade_one+Grade_two;
double average = (double)Average/2;
if (Grade_one == Grade_two){
cout<<"Your Grades are Same."<< endl;
}
if(Grade_one >= 50 && Grade_two >= 50 && Grade_one <= 74 && Grade_two <= 74){
cout<<"Hey! You should focus on your study, your grade is concerningly LOW."<< endl;
}
else if(Grade_one <= 49 && Grade_two <= 49){
cout<<"DUDE! you will fail for sure if you don't study."<< endl;
}
else if(Grade_one >= 75 && Grade_two >= 75 && Grade_one <= 100 && Grade_two <= 100){
cout<<average <<endl;
}
return 0;
}
int Grade_one, Grade_two;
int Average = Grade_one+Grade_two;
Here, you read uninitialized variables. This is Undefined Behaviour and everything could happen: a crash, trash data, weird behaviour, ...
You need to delay the use of a variable only after you set it to a useful value, e.g.:
int Grade_one, Grade_two;
cout<<"Please input your Grade No.1: ";
cin>>Grade_one;
cout<<"Please input your Grade No.2: ";
cin>>Grade_two;
int Average = Grade_one+Grade_two;
double average = Average/2;
You are calculating average before Grade_one and Grade_two are initialized, that cannot work (it is undefined behavior). If you want to define something now and execute it later thats a function. You can use a lambda expression here. Average is a misnomer, it is a sum, not an average. You are using integer arithmetics for the division, but to get correct result it has to be calculated with floating points. I think you got the conditions for the if right, but the last case is simpler if you just make it an else without the condition. Last but not least, you should initialize variables always:
#include <iostream>
using namespace std;
int main()
{
int Grade_one = 0;
int Grade_two = 0;
auto average = [&](){
int Sum = Grade_one + Grade_two;
return Sum / 2.0;
};
Grade_one = 42;
Grade_two = 12;
if (Grade_one == Grade_two){
cout<<"Your Grades are Same."<< endl;
}
if(Grade_one >= 50 && Grade_two >= 50 && Grade_one <= 74 && Grade_two <= 74){
cout<<"Hey! You should focus on your study, your grade is concerningly LOW."<< endl;
}
else if(Grade_one <= 49 && Grade_two <= 49){
cout<<"DUDE! you will fail for sure if you don't study."<< endl;
}
else {
cout<<average() <<endl;
// ^^ call the lambda
}
return 0;
}
You need to modify your code slightly. Save this piece of universally useful code somewhere (preferably in a header file):
template <class Lam>
class Expr
{
public:
using ResultType = std::remove_reference_t<decltype((std::declval<Lam>())())>;
operator ResultType() { return lam(); }
ResultType value() { return lam(); }
Expr(Lam&& lam) : lam(std::move(lam)) {}
private:
Lam lam;
};
#define EXPR(...) Expr([&](){return (__VA_ARGS__) ;})
When you #include this piece in your program, you can proceed with minimal modifications:
int Grade_one, Grade_two;
auto Average = EXPR(Grade_one+Grade_two); // note the auto
auto average = EXPR(Average/2.0); // and the decimal point
I however strongly recommend exploring the other answers first.
I am making a C++ code where you will create an array using a do while loop.
Here is the full code:
const int size = 10;
double *pt1;
//executable
pt1=new double[size];
int i = 0;
do{
cout <<"mile" << "[" << i << "]" << setw(3);
if(*(pt1+i) >= 100 && *(pt1+i) <= 250)
{
cin >> *(pt1+i);
i++;
}
else if( *(pt1+i) > 100 && *(pt1+i) < 250)
{
cout << "100-250 only";
continue;
}
}while(i < 10);
There is an input validation where the numbers that will be accepted are only numbers from 100 to 250 but it keeps on looping. I can't find where the problem is. Any help would be greatly appreciated!
The first error is that you are testing the value of the input before you actually get the input. That makes no sense, you need to switch the order around. So this
if(*(pt1+i) >= 100 && *(pt1+i) <= 250)
{
cin >> *(pt1+i);
...
}
else if( *(pt1+i) > 100 && *(pt1+i) < 250)
{
...
}
should be this
cin >> *(pt1+i);
if(*(pt1+i) >= 100 && *(pt1+i) <= 250)
{
...
}
else if( *(pt1+i) > 100 && *(pt1+i) < 250)
{
...
}
Secondly I think you meant
else if( *(pt1+i) < 100 || *(pt1+i) > 250)
Or better you could have just said
else
Then there is no chance of getting the logic wrong. When you have only two choices, you just need to test for the first choice and use else for the second choice. There's no need to test for the opposite of the first choice, using a plain else will do that automatically.
Also continue at the end of a loop is not necessary, loops continue automatically.
Finally pt1[i] is much easier to read than *(pt1+i).
I was trying to solve this problem and from the comments section in the editorial, I was directed to the following solution :
#include <bits/stdc++.h>
using namespace std;
#define MAX(a,b,c) max(a,max(b,c))
int n,a,b,c,dp[4001];
int f(int x)
{
if (x == 0) return 0;
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // <- **I have doubt here**
if (!dp[x]) dp[x] = MAX(f(x-a),f(x-b),f(x-c)) + 1;
return dp[x];
}
int main()
{
cin >> n >> a >> b >> c;
memset(dp,0,sizeof(dp));
cout << f(n) << endl;
}
I wanted to know:
What is the need of the if statement that returns 0xACCE97ED for the test case:
4000 1 2 3. This test case dosen't work when that specific if statement is missing.
Why specifically 0xACCE97ED is being returned? Because when I tried to return any other number (say 9999), then the output is expected output + 9999.
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // -1395746835
Well looking at the dp function, it is basically maximizing values and this specific if statement is saying:
if x < 0
the length of the ribbon you cut is negative (which should be impossible)
or if x > 0 and x < a, b, c which means you can still cut X but all available sizes would result into having a ribbon of negative length
return 0xACCE97ED; return a random negative value which happens to spell out ACCEPTED because this state is invalid
And since the third if statement will try to get the max value, 0xACCE97ED will never be selected as the max value.
0xACCE97ED means "ACCEPTED" in the 1ee7 speech. nothing else specific about this value.
What is the need of the if statement that returns 0xACCE97ED for the test case: 4000 1 2 3
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // <- **I have doubt here**
because the function f is recursive, in the next line it calls itself:
if (!dp[x]) dp[x] = MAX(f(x-a),f(x-b),f(x-c)) + 1;
return dp[x];
with a smaller values for x so presumable it will eventually make that if statement true and will return "accepted" (0xACCE97ED).
I have a problem with the code. It compiles with no errors, but right after taking input from the user it even with correct values it seems to skip the first conditional statement and go directly to ELSE which causes the program to terminate. I can't seem to find the cause for this behavior.
I think it might the issue with the way the conditional statement is constructed:
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){
I need to check if the value entered is 25 <= S <= 75 and is divisible by 5, as well as the other value being 0.2 < U < 0.7
Course Assignment
//#include "stdafx.h" // Header File used VS.
#include <iostream>
//#include <iomanip> // Used to format the output.
#include <cstdlib> // Used for system().
#include <math.h> // Used for sqrt().
using namespace std;// ?
int main (){
int S; // Gram/Litre
double U; // Specific Max. Growth Rate. Per Hour.
double D; // Maximum Dilution Rate.
const int K = rand() % 7 + 2; // Saturation Constant - Randomly Gegerated Number Between 2 & 7. In Hour/Litre.
cout << "Enter value between 25 and 75, divisible by 5, for S in Gram/Litre: ";
cin >> S;
cout << "Enter value bigger than 0.2, but less than 0.7, for U per Hour: ";
cin >> U;
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){ // Check Condition ***May Need Adjustments***
D = U * ( 1 - sqrt ( K / ( K + S) ) ); // Might have to adjust values to fit data type double. Add .00
cout.precision(3); // Prints 3 values after decimal point.
cout << "Maximum dilution rate is: " << D << endl;
if( D < 0.35 && D < 0.45 ){
cout << "Kinetic parameters are acceptable." << endl;
}
else{
cout << "Kinetic parameters are not acceptable." << endl;
}
}
else{
cout << "Invalid Input. Program will now terminate." << endl;
}
system("PAUSE"); // Pauses the program before termination.
return 0;
}
First, if you want 25 <= S <= 75, you should have
25 <= S && S <= 75, not S <= 25 && S <= 75. Same with U < 0.2 and D < 0.35 - they should be 0.2 < U and 0.35 < D.
Second, the above statement returns a boolean - thus, if S is a value between 25 and 75, the boolean will be cast to an integer value of 1, and 1 % 5 == 0 always be false. (Similarly, if S is outside this range, the boolean will be cast to an integer 0 and 0 % 5 == 0 will always be true)
A correct, complete if-statement is as follows:
if((25 <= S && S <= 75) && (S % 5 == 0) && (0.2 < U && U < 0.7)){
...
if(0.35 < D && D < 0.45){
...
}
...
}
If you read a number between 25 and 75 from the input, the if( ((S <= 25 is always false.
You must use if( ((S >= 25 && ....
The problem mainly lies in your loop conditions. For example, in this line from your code:
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){
//...
}
The if condition S <= 25 && S <= 75 simply can be rewritten as S <= 25 because in words, your parameter states that if S is less than or equal to 25 OR if S is less than or equal to 75, and so on.
The same problem exists here: U < 0.2 && U < 0.7. Once again, the if statement simply checks whether U is less than 0.2 and U is less than 0.7, the latter always being true if the former is true.
However, in your output statement before accepting the 2 inputs, you state that S should have a range of 25 <= S <= 75, meaning that S is greater than 25; not less. The same issue for U: you are expecting input ranging between 0.2 < U < 0.7.
How you should rewrite your if-then statement is as follows:
if( (S >= 25 && S <= 75) && (S % 5 == 0) && (U > 0.2 && U < 0.7) ){
//...
}
This not only makes your if-statement's conditions easier to read and comprehend, but it also eliminates the errors. This should work now. The meaning of the code stays the same: S has to be between 25 and 75 (including these numbers), it should be divisible by 5, and U should be between 0.2 and 0.7.
BTW, the same mistake exists in this part of your code also:
if( D < 0.35 && D < 0.45 ){...
I fixed it below:
if( D > 0.35 && D < 0.45 ){...
Good luck!
If I want to make a if statement that requires more than one thing to be true do I need to do it with "else if"? Because I think it looks ugly so I would prefer if I could solve that in one statement.
Here is the code:
if(x == 2 OR 4 OR 6 OR 8 OR 10)
{
something......
}
etc. etc.
return 0;
Will that work?
if (x == 2 || x == 4 || x == 6 || x == 8 || x == 10)
{
...
}
OTOH, if your intent is, "If x is even...":
if (x % 2 == 0)
{
...
}
There isn't much of a better option than this:
if (x == 2 || x == 4 || x == 6 || x == 8 || x == 10)
If you wanted to optimize at the cost of readability:
if (x > 0 && x <= 10 && (x % 2 == 0))
The % will be optimized into a bit-wise AND by the compiler.