Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-07-2007, 04:42 AM
123456789asdf 123456789asdf is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Credits: 0
Rep Power: 0
123456789asdf is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 08:55 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,453
Last Blog:
wxWidgets is NOT code ...
Credits: 761
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

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.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 12:10 PM
123456789asdf 123456789asdf is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Credits: 0
Rep Power: 0
123456789asdf is on a distinguished road
Default

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!!!!!!!)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 12:11 PM
123456789asdf 123456789asdf is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Credits: 0
Rep Power: 0
123456789asdf is on a distinguished road
Default

Btw, i can't use #include <math.h> because of restriction
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-07-2007, 02:27 PM
birinight birinight is offline
Newbie
 
Join Date: Oct 2007
Posts: 7
Credits: 0
Rep Power: 0
birinight is on a distinguished road
Default

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;	
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 11-07-2007, 07:20 PM
DevilsCharm's Avatar   
DevilsCharm DevilsCharm is offline
Programming God
 
Join Date: Jul 2006
Posts: 887
Credits: 0
Rep Power: 14
DevilsCharm is on a distinguished road
Default

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".
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-07-2007, 10:54 PM
123456789asdf 123456789asdf is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Credits: 0
Rep Power: 0
123456789asdf is on a distinguished road
Default

TY very much helps me alot
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-08-2007, 11:38 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,453
Last Blog:
wxWidgets is NOT code ...
Credits: 761
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Assigning LG = 1.0 within the while loop is guaranteed to keep you from making progress.
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 11-09-2007, 01:02 PM
birinight birinight is offline
Newbie
 
Join Date: Oct 2007
Posts: 7
Credits: 0
Rep Power: 0
birinight is on a distinguished road
Default

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;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 11-15-2007, 03:37 PM
MPD Psycho's Avatar   
MPD Psycho MPD Psycho is offline
Newbie
 
Join Date: Nov 2007
Posts: 20
Credits: 0
Rep Power: 4
MPD Psycho is on a distinguished road
Default

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:

C++ Code:
  1. double sqrt_estimate (double s) {
  2.     double x0 = 1, x1 = 0, x2 = 0, temp = 0;
  3.     double epsilon = 1e-6;
  4.     int i = 0;
  5.     do {
  6.         if (i == 0) {
  7.             x1 = (s + s/x0)/2;
  8.         } else {
  9.             x1 = temp;
  10.         }
  11.         x2 = (x1 + (s/x1))/2;
  12.         temp = x2; i++;
  13.     } while ((x1 - x2) > epsilon);
  14.     return(x2);
  15. }

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

C++ Code:
  1. void sqrt_estimate (double s, double x0) {
  2.     double x1 = 0, x2 = 0, temp = 0;
  3.     double epsilon = 1e-6;
  4.     int i = 0;
  5.     printf("\n");
  6.     do {
  7.         if (i == 0) {
  8.             x1 = (s + s/x0)/2;
  9.         } else {
  10.             x1 = temp;
  11.         }
  12.         x2 = (x1 + (s/x1))/2;
  13.         temp = x2; i++;
  14.         printf("Status: current \"old\" = %f, current \"new\" = %f\n", x1, x2);
  15.     } while ((x1 - x2) > epsilon);
  16.     cout << "\nThe square-root of " << s << " is approximated as: " << x2;
  17.     printf("\nThe number of repeated approximations: %d\n", i);
  18. }

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

The epsilon thing is pretty sloppy, but they should work on simple numbers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 11:25 PM.

Contest Stats

Xav ........ 1276.19
MeTh0Dz|Reb0rn ........ 1048.58
marwex89 ........ 869.98
John ........ 868.39
morefood2001 ........ 868.04
WingedPanther ........ 761.06
Brandon W ........ 684.87
chili5 ........ 294.12
Steve.L ........ 216.18
dargueta ........ 192.86

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 81%

Ads