hello,

i have some simple questions but i don't know why my code is not correct

see these questons:

1-Write an algorithm to accept three numbers and display the largest one?

this is my code

int num1,num2,num3;
	cout <<"Please enter three numbers:";
	cin >> num1 >> num2 >> num3;
	if(num1 > num2 && num3){
	cout <<"The largest number is: " << num1;
	}else if(num2 > num1 && num3){
	cout <<"The largest number is: " << num2;
	}else{
	cout <<"The largest number is: " << num3;
	}

why this code doesn't wrok correctly???? :(

it made me angry :@ because if i'm the compiler then i can understand the code

i tried also this code to accept four numbers

#include <iostream>
using namespace std;
int main()
{
int num1,num2,num3,num4,largest;
	cout <<"Please enter four numbers:";
	cin >> num1 >> num2 >> num3 >> num4;
	largest = num1;
	if(num2 > num1)
	largest = num2;
	if(num3 > num2)
	largest = num3;
	if(num4 > num3)
	largest = num4;
	cout <<"The largest number is: " << largest;
	return 0;
	}

please try this input 8,5,6,4 >> the output will be 6!!!

please tell me whats the wrong

****

2- accept a number and display all the numbers from 0 to the number entered. Then modify the flowchart so that it displays only odd numbers.
e.g. Given 10, it should output: 1 3 5 7 9

this is my code

int num,count = 1;
cout<<"Please enter a number: ";
cin >> num;
while(count > num){
cout << " "<< num;
num = num + 2;
}
cout <<"end";
    return 0;
}

why this one doesn't work?

waiting for you

Recommended Answers

All 23 Replies

With first one, it looks like you need to compare to 'largest' each time. That is,

if(num2 > largest)
	largest = num2;
	if(num3 > largest)
	largest = num3;
	if(num4 > largest)
	largest = num4;

For the second one, I would suggest using more descriptive variable names so you can keep them clear. It looks like you are increasing 'num' each time, but I'd say you need to be increasing 'count' until it gets to 'num'.

Give those a shot and let us know if it works.

Dave

commented: thanks *_^ +1

firstly i congratulate you for your attempt on "being the compiler" and checking the code as the compiler would have. That's a nice approach. But unfortunately the compiler is not as logical about logical operators as it should have been. There's something called "operator precedence" which kills the fun. So when you are doing

if (num1 > num2 && num3)

the > operator gets more preference and checks whether num1 is greater than num2. Lets say this is TRUE. Then it does something like TRUE && num3. This is definitely not the way you wanted it to be. right??
So, basically you have to do something like:

if (num1 > num2 && num1 > num3)

Do this for all your IFs.

Your 2nd and 3rd code has been explained by daviddoria. Go through them and do as you have been told and come back if you have any doubts.

Happy to help.
Cheers...

commented: thanks *_^ +1

Here's the correct code for the 2nd program

int num;
cout<<"Please enter a number: ";
cin>>num;
for(int i=1;i<num;i=i+2)
{
cout <<" "<< num;
}
cout <<"end";
    return 0;
}

Here's the correct code for the 2nd program

int num;
cout<<"Please enter a number: ";
cin>>num;
for(int i=1;i<num;i=i+2)
{
cout <<" "<< num;
}
cout <<"end";
    return 0;
}

Please don't give away the answer if the OP is working towards it. Your code is also incorrect. Test it out.

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.

Given two numbers, you can use std::max(a,b) to return the largest out of a and b.
Thus using the same logic you can just nest std::max for 3 inputs, so std::max( std::max(a,b) , c) returns the largest out of the 3 inputs. There is a very similar patterns for 4 inputs and so on.

thanks every one for helping me

actully i answered the first question and it works fine :)

but the second question i will try to do it again

thanls again :)

:( i tried but i doesn't work

this is my code

int num,count = 1;
cout<<"Please enter a number: ";
cin >> num;
while(count > num){
cout << " " << count;
count = num + 2;
}
cout <<"end";

why this one diesn't work it should be fine

cout<<"Please enter a number: ";
cin >> num;
while(count > num){

if the user input '10' so the varible num = 10

so it will be like this

while(1 > 10){

then it shlould print '1' firstly and then +2 untill comes to '10'

am i right?

Nope, you reversed things,
while(1 > 10){
is false and the loop doesnt run.
try something like

cout<<"Please enter a number: ";
cin >> num;
while(num>count){

thanks for your helping

but why the output is 1???

change this line

int num,count = 1;

to

int num;
int count = 1;

It will be good to initialize something like

int num=0;

but it is completely optional

change this line

int num,count = 1;

to

int num;
int count = 1;

They are identical.

Just to add, the program you have (i.e shown below) isn't supposed to print anything in case user enters any number greater or equal to 1. It should just print "end"

int num,count = 1;
cout<<"Please enter a number: ";
cin >> num;
while(count > num){
cout << " " << count;
count = num + 2;
}
cout <<"end";

:( i tried but i doesn't work

this is my code

int num,count = 1;
cout<<"Please enter a number: ";
cin >> num;
while(count > num){
cout << " " << count;
count = num + 2;
}
cout <<"end";

Evstevemd is right in that it shouldn't be count > num. When you have a loop statement like that, each time through, it's asking that mathematical statement like a question.
You have it as count > num so it is skipping over the loop as he said (as things in the parentheses that are false cause it to stop).

You want to ask "is count less than num?" ( count < num ) if that's true, then that's the signal to keep going. Once you've reached the end of the range and it's false, the loop will stop.

After changing the loop condition to what you have been told, your main problem will be line no 6. Here's the modified code:

#include <iostream>
using namespace std;

int main(){
    int num, count = 1;
    cout<<"Please enter a number: ";
    cin >> num;
    while(count < num){
        cout << " " << count;
	count = count + 2;      //it shoudn't be count = num +2
    }
    cout <<" end";
}

Try to understand why its working the way its working...and you already have the technique...just be the compiler and do a dry run.

Hope it helps.

Let me clarify that I quoted the post for the sake of quoting the post, not as a reflection that the code was correct, but good catch NP-complete. However, try not to give the OP something they can compile and turn in for a grade.

i guess this is too easy to fetch him a grade:) ...but still...i'll be more carefull next time...

and thanks for the compliment...

thanks NP-complete for your helping and the most important think is to know why it works like this :)

i thing it's good for me to have just a simple mistake hehehe because i'm beginer

it's realy help me

thanks everyone

opss hehehe i forgot

OK it's solved :P

C/C++ logic is always just testing for zero.
In C/C++, zero is false, anything else is true.

int i = 3;
while ( i ) // test if i is non-zero
{
 i= i-1; // this executes 3 times
}

One result of this is that the term " .. != 0 " .. is always redundant in C.
You can leave it out, because C/C++ always tests for nonzero.
Testing a number for true/false is perfectly valid C/C++:

int i = 42;
if ( i ) i = i-1; // "if(i)" is same as "if( i != 0 )"

I use this feature of C/C++ all the time in my work.
for instance, the cleanest way to count through a list backwards is:

int i= listSize;
while(i--) // quit on zero, countdown
 list[i]= 0;

.. or, for system functions that return zero/NULL when they fail:

//
// open a file
//
FILE *f = fopen( "c:\\input.txt", "r" );
//
// did the file open okay?
//
if(f) // this works because 'fopen' returns zero on failure
 {
 // .. (do some stuff)

 // close the file
 fclose(f);
 }
else printf( "could not open the file" );

Also remember that most operators in C, like '+' or '<' are "binary operators", meaning, they only work for two numbers; the one on the left, and the one on the right.

So, when you have a string of binary operators all in a row, like:

int x = a + b * c;

.. the language doesn't do anything fancy with them all together. It decides which operator to do first, and does that, then does the next thing, until there's nothing left to do, always two numbers at a time.


So, when you write:

if( num1 > num2 && num3 ) // this tests two things: num1 > num2, and num3 != 0.

.. you are saying something like, "if A is greater than B, and C is not zero".

The single most useful Google query for a beginning C programmer is "operator precedence". Try it. I used to keep a chart on my desk at all times.

this is a code that takes n numbers and displays the max

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.