| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
3 function problem (builds but doesnt run) C++
Hey guys I'm new to the forums. I have this problem I've been kinda banging my head against the wall with. Its supposed to calcualte standard deviation. I'm compiling in Visual studio 2003. This is the last line that it has in the little output log. Any light you guys/gals couldshed on this would be really be awsome.
The program '[3616] standard_deviation.exe: Native' has exited with code 0 (0x0). Here's the Code: Code:
#include <iostream>
#include <cmath>
using namespace std;
int get_data(int[], int& );
double do_math(int[], int&, double, double, double);
void _cout_(double, double, double);
int main(int L[10], int& n, double x_bar, double s_dev, double varience)
{
int get_data (int L[], int& n);
double do_math(int L[], int& n, double x_bar, double s_dev, double varience);
void _cout_(double x_bar, double s_dev, double varience );
}
int get_data(int L[10], int& n)
{
int i;
cout<<"How many data values do you have?";
cin>>n;
for ( i=1; i<=n; i++)
{
cout<<"Enter data value #"<<i<<" : ";
cin>>L[i];
return L[10];
return n;
}
}
double do_math(int L[], int& n, double x_bar, double s_dev, double varience)
{
//Calculate the Sample mean
int i;
double ex, esqd;
ex=0;
for(i=1; i<=n; i++)
ex= ex+L[i];
x_bar = double(ex)/double(n);
//Calculate Sum of the squares of deviations
esqd = 0;
for (i=1; i<=n; i++)
esqd = esqd + pow(L[i]-x_bar, 2);
//Calculate standard deviations
varience = double(esqd/double(n-1));
s_dev= sqrt(varience);
return x_bar;
return s_dev;
return varience;
}
void _cout_(double x_bar, double s_dev, double varience)
{
cout<<"Mean of the sample is: "<<x_bar<<endl;
cout<<"Varience of the sample is: "<<varience<<endl;
cout<<"Standard deviation of the sample is: "<<s_dev<<endl;
}
Last edited by B-Con : April 28th, 2006 at 07:31 PM. Reason: Added [code] tags. |
|
#2
|
|||
|
|||
|
when you call the function in main, the array variable doesn't accept the square brackets
get_data(L,n); just use the variable itself for the function, a function accesses the arrays address not value get data make the return type void and remove your return statements, see if that works |
|
#3
|
|||
|
|||
|
Hey thanks for the repply. I got it to compile, but I'm having some hang ups on the get data part. I lets me enter how many #'s i'd like to enter, and and I can input the 1st number, but when i hit return its comes up with this error.
Unhandled exception at 0x00420a0c in standard_deviation.exe: 0xC0000005: Access violation writing location 0x00000005. Is it something where I didnt declare my variables right? I sorta see how this is supposed to work, but i'm still pretty new to functions in general. Thanks again for the reply I'm getting closer. Here's what my new code looks like: Code:
#include <iostream>
#include <cmath>
using namespace std;
void get_data(int[], int);
void do_math(int[], int, double, double, double);
void _cout_(double, double, double);
void main(int L[10], int n, double x_bar, double s_dev, double varience)
{
get_data (L, n);
do_math(L, n, x_bar, s_dev, varience);
_cout_(x_bar, s_dev, varience);
}
void get_data(int L[], int n)
{
int i;
cout<<"How many data values do you have?";
cin>>n;
for (i=1; i<=n; i++)
{
cout<<"Enter data value #"<<i<<" : ";
cin>>L[i];
}
}
void do_math(int L[10], int n, double x_bar, double s_dev, double varience)
{
//Calculate the Sample mean
int i;
double ex, esqd;
ex=0;
for(i=1; i<=n; i++)
ex= ex+L[i];
x_bar = double(ex)/double(n);
//Calculate Sum of the squares of deviations
esqd = 0;
for (i=1; i<=n; i++)
esqd = esqd + pow(L[i]-x_bar, 2);
//Calculate standard deviations
varience = double(esqd/double(n-1));
s_dev= sqrt(varience);
}
void _cout_(double x_bar, double s_dev, double varience)
{
cout<<"Mean of the sample is: "<<x_bar<<endl;
cout<<"Varience of the sample is: "<<varience<<endl;
cout<<"Standard deviation of the sample is: "<<s_dev<<endl;
}
|
|
#4
|
|||
|
|||
|
you can't define your own prototype for main. it is either
int main(); or int main(int argc, char *argv[]); You need to declare your variables rather than including them as parameters. Also, what is wrong with this logic: 1. allocate space for 10 values 2. ask the user how many values s/he wants to input 3. input that many values what if step 2 yields >10? You need dynamic allocation here. Also, start your input loop at 0, not 1 becuase arrays have zero-based indexing. |
|
#5
|
|||||
|
|||||
|
Quote:
Unfortuantely I didnt design the program. I thought the same thing about the array, but I was told to do it this way. My teacher roughed out the program adn then told us thats how he wanted it laid out. I could make this happen with out even using functions. The book I have doesnt really explain the variables in fucntions very well. Quote:
I'll just change it up and tell him thats how I got it to work and he can dock me points if he wants. Quote:
He had us set the array up starting at 1 because of the goofy "Enter input #<<i<<": "; statement. I'll change it up and see what I come up with. I'm not an efficient programer by any stretch of the imagination, but I'm trying. I appriciate the help, I'm going to finish the book outside of class this summer, and then start into some object oriented stuff. Hopefully my questions will be a litter more interesting in the future. |
|
#6
|
|||
|
|||
|
about the first point: what was his exact wording, because he may have meant you should declare the pointer first and dynamically allocate it after the input (or maybe not).
about the loop, start it at 0 and then have cout << "Enter input #" << i+1 << ": "; to correct for it. by the way, this thread illustrates what happens when the OP doesn't specify all of the requirements on his/her program. there was no indication that the professor required a certain sequence of events, so my solution assumed you were free to design the program. we need to know all the requirements to give appropriate help. |
|
#7
|
|||||
|
|||||
|
Quote:
We never got to pointers, (or if we did he didnt call them that) pointers are addressed a couple chapters after the one we were currently working in. He gave us this as a single function program and told us he wanted us to break it up into a 3 functions. The functions were supposed to do the following: get data, passed the data to to a function to do math and then passed the final variables into a statement that outpute the data. He didnt really adress making he arrays dynamic. I can go back and read up on this because its evident that this is probably where I'm lacking knowlege. Quote:
Thats a fair enough fix, it just wasnt how he showed us. That way makes sense. Quote:
He said we're allowed to design it anyway we please as long as it does the required parts (3 functions for this one). I just figured what ever I had going on was an obviouse fix or I would have explained in depth. I'll be sure to explain more thuroughly in my next post. |
|
#8
|
|||
|
|||
|
try this ...
if you want the user to decide how big to make the array Code:
int maxSize;
cout<<"Please enter number of elements in array: ";
cin>>maxSize;
int *x = new int[maxSize];
int h[maxSize];
for (int i=0;i<maxSize; i++)
{
cin>>h[i];
}
|
|
#9
|
|||
|
|||
|
@xreddawg909x:
why do you dynamically allocate an array and then fill the non-dynamic one? In the C89 standard (and therefore in some compilers) the int h[maxSize] will not even be allowed because you need a constant array size. So just use x. Also, don't forget delete[] x. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > 3 function problem (builds but doesnt run) C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|