Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Help with Square root and calculator program!!!

  1. #1
    123456789asdf is offline Newbie
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Unhappy Help with Square root and calculator program!!!

    Square root:
    Using this function NG = 0.5 ( LG + n / LG ) where LG initial guess is 1.0
    so the program will look like this

    Enter a number: 2
    The square root of 2.000000 is approximately 1.4142135624

    but what my program get is
    Enter a number: 2
    The square root of 2.000000 is approximately 1.5000000000

    I'm using double for all of my input and output.

    Calculator:
    (
    Program 2

    Write the program calculator which should implement a simple calculator.

    The program should allow the user to repeatedly enter expressions consisting of an operand, an operator, and another operand. It should then perform the calculation, output the result, and should wait for the next input.

    Your program must have a function scan_data with three output parameters, returning the operator and the two operands from the input line. Your program must also have a function calculate which should perform the calculation entered by the user. This function should have three input parameters (operator and two operands), and should return the result of the calculation. The program should ignore any incorrect input, such as a wrong operator, or an operand that is not a number.

    Your program should support following operators:

    + add
    - subtract
    * multiply
    / divide
    ^ power (accumulator to the power of operand)
    q quit

    For calculating the power of a number, your program is allowed to use the math library's pow function. The function prototype of this function is:

    double pow(double x, double y);

    The function returns the value of raising x to the power of y (that is, x ^ y).
    Example:

    ./calculator
    Please enter first operand, operator, and second operand, or q to quit.
    1.3 + 2.5
    result: 3.800000
    176.12312 - 83.311
    result: 92.812120
    81327.12 * 23
    result: 1870523.760000
    738 / 3
    result: 246.000000
    2.25 ^ 3
    result: 11.390625
    q

    )
    That's the outline but i have no idea how to make the operator.

    Plz help me out, thx

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143
    For the square root, you need to use a loop to repeatedly throw the result back into the function.

    For powers, you again need a loop that runs y times.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    123456789asdf is offline Newbie
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0
    Thx for your help. But i still have probelm
    here's my program
    /*calculate the square root of a number
    */
    #include <stdio.h>

    int
    main(void)
    {
    double n; /* input number */
    double LG; /* input */
    double NG; /* output number */
    /* Get number input */
    printf("Enter a number: \n");
    scanf("%lf", &n);


    while (NG-LG>= 0.0000000001){
    /* input number*/
    LG=1.0;
    /* Calculate the square root*/
    NG=0.5 * (LG +n / LG);

    }
    /* Display the results */

    printf("The square root of %lf is approximately %lf", n, NG);

    return (0);
    }


    What did i do wrong? or it can't be loop like this, PLs help me to decode a perfect program PLS!
    (P.S. it's blowing my headoff!!!!!!!)

  5. #4
    123456789asdf is offline Newbie
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0
    Btw, i can't use #include <math.h> because of restriction

  6. #5
    birinight's Avatar
    birinight is offline Learning Programmer
    Join Date
    Oct 2007
    Location
    Here
    Posts
    36
    Rep Power
    0
    Here's an example for program 2.
    It'n not clean code. I had nothing better to do this afternoon, so I've made this in half an hour.
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    
    float calculate(float num1, float num2, char operand);
    void stripInput(char* input, float& num1, float& num2, char& operand);
    char* subString(const char* str, char* substr, int start, int end=-1);
    
    int main()
    {
    	char input[256];
    	do {
    		cin.getline(input,256);
    		float num1, num2;
    		char operand;
    		if(strcmp(input,"q") != 0)
    		{
    			stripInput(input, num1, num2, operand);				
    			cout << num1 << operand << num2 << " = " << calculate(num1,num2,operand) << endl;
    		}
    		
    	}while(strcmp(input,"q") != 0);
    }
    
    float calculate(float num1, float num2, char operand)
    {
    	switch(operand)
    	{
    		case '+':
    			return num1+num2;
    			break;
    		case '-':
    			return num1-num2;
    			break;
    		case '*':
    			return num1*num2;
    			break;
    		case '/':
    			return num1/num2;
    			break;
    		case '^':
    			return (float)pow((double)num1,(double)num2);
    			break;
    		default:
    			cout << "Error - Invalid Operator" << endl;
    			break;
    	}
    	return 0;
    }
    
    void stripInput(char* input, float& num1, float& num2, char& operand)
    {
    	char* substr;	
    	int i;
    	for(	i=0;
    		i < strlen(input) &&
    		(input[i] != '+' &&
    		input[i] != '-' &&
    		input[i] != '*' &&
    		input[i] != '/' &&
    		input[i] != '^');
    		++i);
    	char tmp[256];
    	num1 = atof(subString(input, tmp, 0, i));
    	num2 = atof(subString(input, tmp, i+1));
    	operand = input[i];
    }
    
    char* subString(const char* str, char* substr, int start, int end)
    {
    	if(end == -1)
    		end=strlen(str);
    	int count=0;
    	for(int i = start; i < end; ++i)
    		substr[count++] = str[i];
    	substr[count]='\0';
    	return substr;	
    }

  7. #6
    DevilsCharm's Avatar
    DevilsCharm is offline Programming God
    Join Date
    Jul 2006
    Posts
    884
    Rep Power
    0
    It doesn't seem like making a calculator would be so complicated, but I guess there are a lot of underlying principles that need to be elaborated upon, "codually".

  8. #7
    123456789asdf is offline Newbie
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0
    TY very much helps me alot

  9. #8
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143
    Assigning LG = 1.0 within the while loop is guaranteed to keep you from making progress.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    birinight's Avatar
    birinight is offline Learning Programmer
    Join Date
    Oct 2007
    Location
    Here
    Posts
    36
    Rep Power
    0
    Quote Originally Posted by DevilsCharm View Post
    It doesn't seem like making a calculator would be so complicated, but I guess there are a lot of underlying principles that need to be elaborated upon, "codually".
    You know that usualy making simple code it's harder than complicated code.

    I know for sure that there are functions in C++ that do what I've made, but I don't program in ISO C++ in a wile, so I've made functions to resolv my problems. I prefered that way(I could search for documentation though).

    For example if I've used the String from C++ I wold smpliefied my algorithm, but I didn't know if this guy is familiared with Sring type.

    As for complicated code it's simple to undurstand sometimes when you are new to a language.

    For example:

    a=a==5?b:c;

    it's simple but I think it's a little harder to undestand at first.

    if(a==5)
    a=b;
    else
    a=c;

  11. #10
    MPD Psycho's Avatar
    MPD Psycho is offline Newbie
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0
    Here is my version(s) of the square-root-estimation program:

    -> This version only takes the input value, and then returns the resulted value back:

    [HIGHLIGHT="C++"]double sqrt_estimate (double s) {
    double x0 = 1, x1 = 0, x2 = 0, temp = 0;
    double epsilon = 1e-6;
    int i = 0;
    do {
    if (i == 0) {
    x1 = (s + s/x0)/2;
    } else {
    x1 = temp;
    }
    x2 = (x1 + (s/x1))/2;
    temp = x2; i++;
    } while ((x1 - x2) > epsilon);
    return(x2);
    }[/HIGHLIGHT]

    -> This version takes both the input and the user's estimation, then prints the results in more details:

    [HIGHLIGHT="C++"]void sqrt_estimate (double s, double x0) {
    double x1 = 0, x2 = 0, temp = 0;
    double epsilon = 1e-6;
    int i = 0;
    printf("\n");
    do {
    if (i == 0) {
    x1 = (s + s/x0)/2;
    } else {
    x1 = temp;
    }
    x2 = (x1 + (s/x1))/2;
    temp = x2; i++;
    printf("Status: current \"old\" = %f, current \"new\" = %f\n", x1, x2);
    } while ((x1 - x2) > epsilon);
    cout << "\nThe square-root of " << s << " is approximated as: " << x2;
    printf("\nThe number of repeated approximations: %d\n", i);
    }[/HIGHLIGHT]

    Both versions only need to include <iostream>, using namespace std.

    The epsilon thing is pretty sloppy, but they should work on simple numbers.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with Square Root function
    By Gz1987 in forum C and C++
    Replies: 4
    Last Post: 12-08-2009, 01:46 AM
  2. Square root or multiplication
    By echoBravo in forum General Programming
    Replies: 7
    Last Post: 11-25-2009, 08:43 AM
  3. calculate Square root of a number PROBLEM
    By Esi_Gold in forum C and C++
    Replies: 8
    Last Post: 05-06-2008, 04:53 AM
  4. finding square root and prime numbers.?
    By aladin in forum General Programming
    Replies: 1
    Last Post: 11-07-2007, 05:49 PM
  5. Roulette / Square root C program help!!!
    By madaznfootballer in forum C and C++
    Replies: 4
    Last Post: 11-07-2007, 05:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts