473,320 Members | 2,024 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.

Program HELP with Sign Function

I have a program that works fine but when I put in a zero the sign is supposed to return zero but doesn't
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <iostream.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. ////////////////////////////////////////////////
  8. //Power Function
  9.  
  10.  int power(int,int);
  11. int power(int base,int exp)
  12. {
  13.   int sum=1;
  14.   for (int i = 0; i < exp; i++)
  15. {
  16.     sum= base*sum;
  17. }
  18.       return sum;
  19. }
  20. ///////////////////////////////////////////////
  21. //Sign Function
  22.  
  23. int sign(int base)
  24.  {
  25.  
  26.      int m;
  27.   if (base<0)
  28.     {m=-1;}
  29.   if (base>0)
  30.       {m = 1;}
  31.   if (base=0)
  32.       {m = 0;}
  33.        return m;
  34.   }
  35. //////////////////////////////////////////////
  36. //Absolute Function
  37. int abso(int);
  38. int abso(int base)
  39.  {
  40.   if (base<0)
  41.     {
  42.    base=base*-1 ;
  43.      }
  44.      return base;
  45.  }
  46. ////////////////////////////////////////////
  47. //Square Function
  48. int square(int);
  49. int square (int x)
  50. {
  51.   return x * x;
  52. }
  53. //////////////////////////////////////////
  54. int main()
  55. {
  56.     int base, exp,val;
  57.     cout<<"*******************************************************************\n";
  58.     cout<<"*******************************************************************\n";
  59.     cout<<"******************      Math Mechine       ************************\n\n";
  60.     cout<<"Please input the base number\t";
  61.     cin>>base;
  62.     cout<<"\nPlease input the exponent\t";
  63.     cin>>exp;
  64.  
  65.        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  66.  
  67.  
  68.     cout<<"The answer to the power function is: "<<power(base,exp)<<endl<<endl;
  69.  
  70.  
  71.     cout<<"The answer to the sign function is: "<<sign(base)<<endl<<endl;
  72.  
  73.     cout<<"The answer to the absolute function is: "<<abso(base)<<endl<<endl;
  74.  
  75.     cout<<"The answer to the square function is: "<<square(base)<<endl<<endl;
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.       cout<<"\n\n";
  85.  
  86.       system("PAUSE");
  87.       return 0;
  88. }
  89.  
Apr 12 '07 #1
2 3228
Try making the base 0 and it returns a garbage value for the sign function.
Apr 12 '07 #2
ilikepython
844 Expert 512MB
I have a program that works fine but when I put in a zero the sign is supposed to return zero but doesn't
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <iostream.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. ////////////////////////////////////////////////
  8. //Power Function
  9.  
  10.  int power(int,int);
  11. int power(int base,int exp)
  12. {
  13.   int sum=1;
  14.   for (int i = 0; i < exp; i++)
  15. {
  16.     sum= base*sum;
  17. }
  18.       return sum;
  19. }
  20. ///////////////////////////////////////////////
  21. //Sign Function
  22.  
  23. int sign(int base)
  24.  {
  25.  
  26.      int m;
  27.   if (base<0)
  28.     {m=-1;}
  29.   if (base>0)
  30.       {m = 1;}
  31.   if (base=0)
  32.       {m = 0;}
  33.        return m;
  34.   }
  35. //////////////////////////////////////////////
  36. //Absolute Function
  37. int abso(int);
  38. int abso(int base)
  39.  {
  40.   if (base<0)
  41.     {
  42.    base=base*-1 ;
  43.      }
  44.      return base;
  45.  }
  46. ////////////////////////////////////////////
  47. //Square Function
  48. int square(int);
  49. int square (int x)
  50. {
  51.   return x * x;
  52. }
  53. //////////////////////////////////////////
  54. int main()
  55. {
  56.     int base, exp,val;
  57.     cout<<"*******************************************************************\n";
  58.     cout<<"*******************************************************************\n";
  59.     cout<<"******************      Math Mechine       ************************\n\n";
  60.     cout<<"Please input the base number\t";
  61.     cin>>base;
  62.     cout<<"\nPlease input the exponent\t";
  63.     cin>>exp;
  64.  
  65.        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  66.  
  67.  
  68.     cout<<"The answer to the power function is: "<<power(base,exp)<<endl<<endl;
  69.  
  70.  
  71.     cout<<"The answer to the sign function is: "<<sign(base)<<endl<<endl;
  72.  
  73.     cout<<"The answer to the absolute function is: "<<abso(base)<<endl<<endl;
  74.  
  75.     cout<<"The answer to the square function is: "<<square(base)<<endl<<endl;
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.       cout<<"\n\n";
  85.  
  86.       system("PAUSE");
  87.       return 0;
  88. }
  89.  
Hi the bolded statement above should be:
Expand|Select|Wrap|Line Numbers
  1. if (base == 0)
  2.  
Apr 12 '07 #3

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

Similar topics

8
by: RoSsIaCrIiLoIA | last post by:
Do you know how to write a self-checking program in standard C? Do I can think that if I write in a file.c static g="1234567"; in the file.exe (or file) there is in some place...
12
by: Raed Sawalha | last post by:
I have the following table which i can not switcha to design view error message said Could not open in Design view. Quote values differently inside a '<% ...."value"... %>' block. and the...
1
by: TPK | last post by:
Here is what I want to do with javascript. On a page with text place a javascript link that: 1) When a user clicks the link (onClick) a new browser window opens (the easy part) NewWindow =...
2
by: cool17 | last post by:
#include "expression.h" #include <stdlib.h> #include <stdio.h> #include <string.h> xContainer splitTerm(string,int,int);
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
10
by: vincex200 | last post by:
My group needs help with this program. We attempted to start it and got no where. Please help us. Write a C++ program that will read data from a file, perform computation on the data, then print...
30
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.