Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-07-2004, 03:38 PM   #1 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
help with already written program

Code:
#include <iostream>
#include <cstdlib>

using namespace std;
int squars (int x, float y=0);
int main()
{
	int i(1);
	squars (i);
  
	cout<< "\n"<< "The average is "<< i/100<< "\n";
	system("PAUSE");	
	return 0;
}  
int squars (int x, float y[0])
{
	while (x<=10)
	{
	     y = x * x + y;
		 cout<< x*x<< endl;
		 x++;
	}
	return y;
}
the average of the square roots won't work. what am i doing wrong?
Androto is offline   Reply With Quote
Old 10-07-2004, 04:10 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
It is not clear to me what your mean with:
The average is " i/100
That is not an average of anything. Be more specific.
Furthermore, the code below contains a few changes. Also note that I changed floats into doubles. Use doubles as your default precision (in standard c++) unless there is a special reason not to.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

double squars (int x, double y=0);

int main()
{
  int i(1);
  squars (i);

  cout<< '\n'<< "The average is "<< i/100.0<< '\n';
  
  system("PAUSE");
  return 0;
}

double squars (int x, double y)
{
  while (x<=10)
  {
       y = x * x + y;
     cout<< x*x<< endl;
     x++;
  }
  return y;
}
__________________
Valmont is offline   Reply With Quote
Old 10-07-2004, 04:13 PM   #3 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
why did you change th ints for the squars function into doubles as well?

i am trying to add up all the square roots, and display the average of all the square roots. the average should end up to be 38.5
Androto is offline   Reply With Quote
Old 10-07-2004, 04:15 PM   #4 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
on that tutorial site you posted, the definition of a double is: double precision floating point number. but what does that mean?



also, how would you include that other way of making the program pause at the end? i remember the words void being part of it.
Androto is offline   Reply With Quote
Old 10-07-2004, 04:46 PM   #5 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
i changed the code a bit, and now it works, but i still don't know how to do that system("pause") thing the way you did it.
Code:
 #include <iostream>
#include <cstdlib>

using namespace std;
double squars (int x, double y=0);



int main()
{
  int i(1);
  squars (i);
  
  system("PAUSE");
  return 0;
}



double squars (int x, double y)
{
  while (x<=10)
  {
       y = x * x + y;
     cout<< x*x<< endl;
     x++;
  }
  cout << "The average of the square roots is " << y/10 << endl;
  return y;
}
Androto is offline   Reply With Quote
Old 10-07-2004, 05:39 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by Androto
why did you change th ints for the squars function into doubles as well?
Lets start with communication.
The first thing I wondered was: "what is squars?".
Then I hope to find out by analysing the squars() function. But it doesn't help me much. Then I look at the return type and the types it accepts as its arguments. What I see is this declaration:
Code:
double squars (int x, double y=0);
Then I see this implementation:
Code:
int squars (... float y[0])
{
	...	{
	     ...
		 ...
		 ...	}
	return y;
}
You are returning a "float", yet your function hints on returning an "int". I have nothing else but to gamble -return a float, and leave the rest. Or... return the int but change the second argument into an int.
I've chosen the first option. All of because I Don't know what a squars is.

Name your variables and functions right. Make sure a stranger (like me) knows what you mean and want.

********************************

Quote:
Originally posted by Androto
on that tutorial site you posted, the definition of a double is: double precision floating point number. but what does that mean?



also, how would you include that other way of making the program pause at the end? i remember the words void being part of it.
To answer your first question:
It means it is twice as precise as a float(ing) point variable.

The answer to your second question is even more simple:
All you need to do is to go back to here http://codenewbie.com/forum/t2403.html and read my code. Copy and paste the functions then use them. That's all. It's for free too .

Quote:
but i still don't know how to do that system("pause") the way you did it.
Well, if you are a bit more patient, then you will receive an answer too. Posting the same question twice within a few moments doesn't help . Read my answer above this one (the link) and you'll have your answer.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 07:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 01:58 PM
Need help on program B00tleg Standard C, C++ 1 10-12-2004 01:02 AM


All times are GMT -8. The time now is 07:10 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting