C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 27th, 2006, 08:48 PM
hizdudeness hizdudeness is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 4 hizdudeness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 10 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 29th, 2006, 03:29 PM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
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

Reply With Quote
  #3  
Old April 30th, 2006, 10:49 PM
hizdudeness hizdudeness is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 4 hizdudeness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 10 sec
Reputation Power: 0
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;
	 
	 
}

Reply With Quote
  #4  
Old May 1st, 2006, 03:33 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #5  
Old May 1st, 2006, 05:00 PM
hizdudeness hizdudeness is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 4 hizdudeness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 10 sec
Reputation Power: 0
Quote:
1. allocate space for 10 values
2. ask the user how many values s/he wants to input


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:
3. input that many values
what if step 2 yields >10? You need dynamic allocation here.


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:
Also, start your input loop at 0, not 1 becuase arrays have zero-based indexing.


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.

Reply With Quote
  #6  
Old May 1st, 2006, 05:20 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #7  
Old May 1st, 2006, 05:40 PM
hizdudeness hizdudeness is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 4 hizdudeness User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 10 sec
Reputation Power: 0
Quote:
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).


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:
about the loop, start it at 0 and then have cout << "Enter input #" << i+1 << ": "; to correct for it.


Thats a fair enough fix, it just wasnt how he showed us. That way makes sense.


Quote:
this thread illustrates....we need to know all the requirements to give appropriate help.


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.

Reply With Quote
  #8  
Old May 9th, 2006, 02:03 AM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
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];
}

Reply With Quote
  #9  
Old May 9th, 2006, 06:08 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
@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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > 3 function problem (builds but doesnt run) C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT