| | |
The greater number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
It works. I'm having a problem trying to display the greater number
to the output. For example what ever number is greater the
first or the second it still displays the first number.
Couse that is what is coded in the cout. But I dont know
what to put in it to display the greater number instead
of the fisrt number. Also I dont know how to connect the fisrt part of the code to the second. For example the void block of code is completlly independent of the main. The program will run the same with the void function in or out.
to the output. For example what ever number is greater the
first or the second it still displays the first number.
Couse that is what is coded in the cout. But I dont know
what to put in it to display the greater number instead
of the fisrt number. Also I dont know how to connect the fisrt part of the code to the second. For example the void block of code is completlly independent of the main. The program will run the same with the void function in or out.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void Max(int& x, int& y) { // start of function body int max; int other; // variable declaration if (x >= y) { // find the maximum number max = x; } else { (x <= y); max = y; } } int main() { void Max( int& , int&); // function declaration (prototype) int firstnum, secnum; cout << "\nEnter a number: "; cin >> firstnum; cout << "Great! Please enter a second number: "; cin >> secnum; cout << "\nThe maximum of these two values is " << firstnum << endl; // the function is //called here system("PAUSE"); return 0; }
Last edited by WaltP; Mar 11th, 2007 at 3:07 pm. Reason: Fixed: 'language' means the language you are using -- like C or CPP...
Your function's prototype should not be inside another function, as your are doing here. It should also be BEFORE the function definition.
Change the function's return type from
By the way, how come you declare the variable 'other' when you don't have any use for it?
Change the function's return type from
void to int. The return the value of 'max'. All that's necessary is for you to call the function in the cout call, and the value will be printed out.By the way, how come you declare the variable 'other' when you don't have any use for it?
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
you should just output the maximum of the two:
that said, your max function is wrong. It finds the max but should return the result. Here's a cleaner version:
C++ Syntax (Toggle Plain Text)
cout << "the max is " << max(firstnum, secondnum)) << endl;
C++ Syntax (Toggle Plain Text)
int max(int a, int b) { if(a > b) return a; else return b; }
•
•
•
•
All that's necessary is for you to call the function in the cout call, and the value will be printed out.
•
•
•
•
you should just output the maximum of the two:
C++ Syntax (Toggle Plain Text)
cout << "the max is " << max(firstnum, secondnum)) << endl;
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
I will try to switch that around. About the other I dont know. I'm new to this. It all looks like chinese to me right now. I get the code from the professor I just need to tweek it to his specifications. If you ask me why I do certain things i dont know. thats what i'm trying to figure out.
Thanks for the help guys.
Thanks for the help guys.
•
•
•
•
Here's a cleaner version:
C++ Syntax (Toggle Plain Text)
int max(int a, int b) { if(a > b) return a; else return b; }
c Syntax (Toggle Plain Text)
if (x >= y) { // find the maximum number max = x; } else { // (x <= y); remove this line, it does nothing max = y; }
Last edited by WaltP; Mar 11th, 2007 at 3:14 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
but I didn't want to post that one for clarity's sake.
C++ Syntax (Toggle Plain Text)
int max(int a, int b) { return (a > b ? a : b); }
•
•
•
•
The statement is not absolute as such. It all depends on the problem under consideration.

•
•
•
•
That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
but I didn't want to post that one for clarity's sake.C++ Syntax (Toggle Plain Text)
int max(int a, int b) { return (a > b ? a : b); }
Purists frown on multiple returns. They can be harder to debug because sometimes a
return gets lost in complex or long functions. IMO, you should try to use only one return point if the code to support it does not make the code overly complex. But don't make the code difficult to read to accomplish it. For this piece of code I'd probably use:
c Syntax (Toggle Plain Text)
int max(int a, int b) { int rtn = a; if(a < b) rtn = b; return rtn; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
I guess it will depend on the coding standards of the company you are working for. Here are two implementations of the function which finds the index of an element in the array and returns -1 if not found.
Take your pick...
c Syntax (Toggle Plain Text)
// MULTIPLE RETURNS int indexOf2 (int array[], int length, int element) { for (int i = 0; i < length; ++i) { if (array [i] == element) return i ; } return -1 ; } // SINGLE RETURN int indexOf1 (int array[], int length, int element) { int index = -1 ; for (int i = 0; i < length; ++i) { if (array [i] == element) { index = i ; break ; // btw even break and continue are frowned upon } } return index ; }
Take your pick...
Last edited by ~s.o.s~; Mar 12th, 2007 at 12:49 pm.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- project plz who can help me (C++)
- bank system problem !!!! (C++ programming) (C++)
- Cannot get PAIR::big to just display greater number (C++)
- who can help me in c++ project (C++)
- Please help with data file and array (C++)
- new to windows apps (C)
- dont know where to start (C)
Other Threads in the C++ Forum
- Previous Thread: shouldn't this work??
- Next Thread: How to controll I/O in soundcard
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int integer java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






