473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A problem with max function

7
Hello everybody.
I recentky wrote a program that calculates the maximum of two numbers.
I used the function max with her library stdlib.h
but when i compiled the program I got an error:
call of nonfuction.
I'm using the borland 3.0 complier


thanks in advance,
Leonid
Dec 15 '06 #1
9 7452
horace1
1,510 Expert 1GB
Hello everybody.
I recentky wrote a program that calculates the maximum of two numbers.
I used the function max with her library stdlib.h
but when i compiled the program I got an error:
call of nonfuction.
I'm using the borland 3.0 complier


thanks in advance,
Leonid
it is difficult to say exactly what is wrong without seeing the code - post the code!
Dec 15 '06 #2
Leonid
7
The code:

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main(void)
{
unsigned int a,b,c,max,max1,sum=0;
double m1,m2,p,l,k,s;
while (1)
{
cin>>a>>b;
if (a==0 || b==0) break;
max1=max( a, b);
for (c=max1;c>0;c--)
{
m1=a/c;
m2=b/c;
if ((int)m1==m1 && (int)m2==m2)
{
max=max(m1, m2);
cout<<"the max mmm is: "<<max<<endl;
}
switch (max)
{
case 1: p=a/b;
cout<<"the mana is: "<<p<<endl;
break;
case 2: k=a*b;
s=sqrt(k*2);
cout<<"the sqrt is: "<<s<<endl;
break;
case 3: if (max1==a)
{
for (l=max1;l>=b;l--)
sum=sum+l;
cout<<"the sum is: "<<sum<<endl;
}
else
{
for (l=max1;l>=a;l--)
sum=sum+l;
cout<<"the sum is: "<<sum<<endl;
}
break;
default: if (max%2==0) cout<<"zugi";
else cout<<"izugi";
}
}
}
}
Dec 15 '06 #3
Ganon11
3,652 Expert 2GB
It would be very simple for you to write your own max() function, so why not try doing that?
Dec 15 '06 #4
horace1
1,510 Expert 1GB
your problem is you have a variable called max, i.e.
Expand|Select|Wrap|Line Numbers
  1.   unsigned int a,b,c,max,max1,sum=0;
  2.  
you cannot have a variable the same name as a function (in this case max()) within the same scope. Change the variable name!
Dec 15 '06 #5
Leonid
7
your problem is you have a variable called max, i.e.
Expand|Select|Wrap|Line Numbers
  1.   unsigned int a,b,c,max,max1,sum=0;
  2.  
you cannot have a variable the same name as a function (in this case max()) within the same scope. Change the variable name!

i changed the name but it is still not working...
Dec 15 '06 #6
horace1
1,510 Expert 1GB
i changed the name but it is still not working...
you have a compilicated mixture of int and float variables where you have to cast between them

done a few more casts and this will compile but may not give you the answer you expect
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6. int main(void)
  7. {
  8. unsigned int a,b,c,max2,max1,sum=0;
  9. float m1,m2,p,l,k,s;
  10. while (1)
  11. {
  12. cin>>a>>b;
  13. if (a==0 || b==0) break;
  14. max1=max( a, b);
  15. for (c=max1;c>0;c--)
  16. {
  17. m1=a/c;
  18. m2=b/c;
  19. if ((int)m1==m1 && (int)m2==m2)
  20. {
  21. max2=(int) max(m1, m2);   // ** added int
  22. cout<<"the max mmm is: "<<max2<<endl;
  23. }
  24. switch (max2)
  25. {
  26. case 1: p=a/b;
  27. cout<<"the mana is: "<<p<<endl;
  28. break;
  29. case 2: k=a*b;
  30. s=sqrt(k*2);
  31. cout<<"the sqrt is: "<<s<<endl;
  32. break;
  33. case 3: if (max1==a)
  34. {
  35. for (l=max1;l>=b;l--)
  36. sum=sum+(int) l;  // ** added int
  37. cout<<"the sum is: "<<sum<<endl;
  38. }
  39. else
  40. {
  41. for (l=max1;l>=a;l--)
  42. sum=sum+ (int)l;  // ** added int
  43. cout<<"the sum is: "<<sum<<endl;
  44. }
  45. break;
  46. default: if (max2%2==0) cout<<"zugi";
  47. else cout<<"izugi";
  48. }
  49. }
  50. }
  51. }
  52.  
Dec 15 '06 #7
Leonid
7
you have a compilicated mixture of int and float variables where you have to cast between them

done a few more casts and this will compile but may not give you the answer you expect
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6. int main(void)
  7. {
  8. unsigned int a,b,c,max2,max1,sum=0;
  9. float m1,m2,p,l,k,s;
  10. while (1)
  11. {
  12. cin>>a>>b;
  13. if (a==0 || b==0) break;
  14. max1=max( a, b);
  15. for (c=max1;c>0;c--)
  16. {
  17. m1=a/c;
  18. m2=b/c;
  19. if ((int)m1==m1 && (int)m2==m2)
  20. {
  21. max2=(int) max(m1, m2);   // ** added int
  22. cout<<"the max mmm is: "<<max2<<endl;
  23. }
  24. switch (max2)
  25. {
  26. case 1: p=a/b;
  27. cout<<"the mana is: "<<p<<endl;
  28. break;
  29. case 2: k=a*b;
  30. s=sqrt(k*2);
  31. cout<<"the sqrt is: "<<s<<endl;
  32. break;
  33. case 3: if (max1==a)
  34. {
  35. for (l=max1;l>=b;l--)
  36. sum=sum+(int) l;  // ** added int
  37. cout<<"the sum is: "<<sum<<endl;
  38. }
  39. else
  40. {
  41. for (l=max1;l>=a;l--)
  42. sum=sum+ (int)l;  // ** added int
  43. cout<<"the sum is: "<<sum<<endl;
  44. }
  45. break;
  46. default: if (max2%2==0) cout<<"zugi";
  47. else cout<<"izugi";
  48. }
  49. }
  50. }
  51. }
  52.  
I'm still getting an error, this time the compiler says that max is undeclared idenificator
Dec 16 '06 #8
horace1
1,510 Expert 1GB
I'm still getting an error, this time the compiler says that max is undeclared idenificator
my code works OK with DEV-C++ GNU compiler. Had problems with Borland Turbo C V3.0 in that it could not find max() - which I guess is the one you are using?

If you cannot find max() try adding the following template function
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. T max (T x, T y)
  3.     { return x > y ? x : y; }
  4.  
Dec 16 '06 #9
horace1
1,510 Expert 1GB
my C++ code works OK with Borland Cbuilder 6 so I guess this is a problem with Borland Turbo C V3.0. However, it is worth noting that the following equivalent C program works OK under Turbo C V3.0
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7. unsigned int a,b,c,max2,max1,sum=0;
  8. float m1,m2,p,l,k,s;
  9. while (1)
  10. {
  11. scanf("%d %d", &a, &b);
  12. if (a==0 || b==0) break;
  13. max1=max( a, b);
  14. for (c=max1;c>0;c--)
  15. {
  16. m1=a/c;
  17. m2=b/c;
  18. if ((int)m1==m1 && (int)m2==m2)
  19. {
  20. max2=(int) max(m1, m2);   // ** added int
  21. printf("the max mmm is: %d\n ");
  22. }
  23. switch (max2)
  24. {
  25. case 1: p=a/b;
  26. printf("the mana is: %f\n ", p);
  27. break;
  28. case 2: k=a*b;
  29. s=sqrt(k*2);
  30. printf("the sqrt is: %f\n ", s);
  31. break;
  32. case 3: if (max1==a)
  33. {
  34. for (l=max1;l>=b;l--)
  35. sum=sum+(int) l;  // ** added int
  36. printf("the sum is: %d\n ", sum);
  37. }
  38. else
  39. {
  40. for (l=max1;l>=a;l--)
  41. sum=sum+ (int)l;  // ** added int
  42. printf("the sum is: %d\n ", sum);
  43. }
  44. break;
  45. default: if (max2%2==0) printf("zugi");
  46. else printf("izugi");
  47. }
  48. }
  49. }
  50. }
  51.  
In fact looking at the Turbo C V3.0 header file stdlib.h it does not appear to define the max() macro for a C++ program
Dec 16 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
26
by: _R | last post by:
Given that VS2005 has made an effort to clean up the syntax of VC++ (in C++/CLI), is there any future plans to do away with function protos, ala C#/VB? What are they needed for these days?
9
by: Roshni | last post by:
Hi, I wanted to know how do function pointers sometime access illegal memory access ? Could any one give me an example ? Thanks, Roshni
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
4
by: B. Williams | last post by:
I have written this program for an assignment that requires a static member function to set a static data member, but I can't figure out how to get it to change the value once set. Would someone...
6
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
7
by: Laurent Pointal | last post by:
on win32] Given the following: 45 .... (<generator object at 0x00A79788>,) .... File "<stdin>", line 1 SyntaxError: invalid syntax
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
3
by: viki1967 | last post by:
Hi there. I have a problem with function updateSum(id). The code is this: <script language="javascript" type="text/javascript"> <!--
10
by: lasmith329 | last post by:
I've never posted a question on any site before, but after racking my head over this hurdle for days, I've caved. I'm working on a program that creates a kml file and exports it to Google Earth. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.