+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

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

  1. #1
    Newbie 123456789asdf is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    4

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57
    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Newbie 123456789asdf is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    4
    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!!!!!!!)

  4. #4
    Newbie 123456789asdf is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    4
    Btw, i can't use #include <math.h> because of restriction

  5. #5
    Learning Programmer birinight is an unknown quantity at this point birinight's Avatar
    Join Date
    Oct 2007
    Location
    Here
    Posts
    36
    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;	
    }

  6. #6
    Programming God DevilsCharm is an unknown quantity at this point DevilsCharm's Avatar
    Join Date
    Jul 2006
    Posts
    885
    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".

  7. #7
    Newbie 123456789asdf is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    4
    TY very much helps me alot

  8. #8
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,698
    Blog Entries
    57
    Assigning LG = 1.0 within the while loop is guaranteed to keep you from making progress.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #9
    Learning Programmer birinight is an unknown quantity at this point birinight's Avatar
    Join Date
    Oct 2007
    Location
    Here
    Posts
    36
    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;

  10. #10
    Newbie MPD Psycho is an unknown quantity at this point MPD Psycho's Avatar
    Join Date
    Nov 2007
    Posts
    20
    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.

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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