How do I base size of arrays on annother number?

This is a discussion on How do I base size of arrays on annother number? within the C++ Programming forums, part of the General Programming Boards category; Say I get the input from a user of how many arguements they want to enter: cin>>int num; Then, I ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    How do I base size of arrays on annother number?

    Say I get the input from a user of how many arguements they want to enter: cin>>int num;
    Then, I want to create an array with (num) slots in it. This is what I have, but it doesn't work. Everything else is the rest of my program, which, suprisingly, doesn't work (or generates more errors). I want to eventually build something which will calculate standard deviation for you, but I can't even get past the first part, finding the average.

    Code:
    #include <iostream.h>
    
    double temp;
    int main()
    {
    	double num;
    	cin>>num;
    	float arguments[num];
    	for (int i=0; i<=num; i++)
    	{
    		cout<<"Enter data member number "<<i<<"-> ";
    		cin>>arguements[i];
    		double temp =+ arguements[i];
    	}
    	double mean = temp / num;
    	return 0;
    }


    Gets:

    --------------------Configuration: deviationcpp - Win32 Debug--------------------
    Compiling...
    deviationcpp.cpp
    C:\My Documents\C++\Deviation\deviationcpp.cpp(9) : error C2057: expected constant expression
    C:\My Documents\C++\Deviation\deviationcpp.cpp(9) : error C2466: cannot allocate an array of constant size 0
    C:\My Documents\C++\Deviation\deviationcpp.cpp(9) : error C2133: 'arguments' : unknown size
    C:\My Documents\C++\Deviation\deviationcpp.cpp(13) : error C2065: 'arguements' : undeclared identifier
    C:\My Documents\C++\Deviation\deviationcpp.cpp(13) : error C2109: subscript requires array or pointer type
    C:\My Documents\C++\Deviation\deviationcpp.cpp(13) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
    C:\My Documents\C++\Deviation\deviationcpp.cpp(14) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    deviationcpp.obj - 7 error(s), 0 warning(s)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > double temp;
    Try to avoid using globals

    > double num;
    This needs to be an int - arrays can only have integer sizes.

    > float arguments[num];
    This should be
    &nbsp; float *arguments = new float[num];
    This will create a dynamic array, as if you had declared
    &nbsp; float arguments[num];

    > for (int i=0; i<=num; i++)
    Like for all arrays, the loop should be
    for (int i=0; i<num; i++)

    > double temp =+ arguements[i];
    1. temp is uninitialised
    2. it goes out of scope before you have a chance to use it in calculating the average.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > If an array can only carry integer sizes, does that mean the user can't input numbers with decimals?
    No - the size of an array (the bit in the [] ) and what each element contains (the type) are totally different.

    float arr[1.234]; // is meaningless

    but

    float arr[2];
    arr[0] = 1.234; // is fine
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    double temp =+ arguements[i];
    1. temp is uninitialised
    2. it goes out of scope before you have a chance to use it in calculating the average.
    Well, how would I keep temp 'in scope' long enough to calcualte the average? Sorry, Im still really bad on the syntax of c++.
    EDIT: Yeah, I realized that I made a mistake about the size of an array to the data it can carry, so I deleted my post to cover up my stupidity
    Last edited by Dual-Catfish; 09-21-2001 at 03:33 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    double temp = 0;
    before you enter the loop in the first place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Okay Everything compiles alright; exept when I output the mean it always equals 0.. why is this?

    How many instances of data do you want to enter? ->6
    Enter data member number 1-> 3
    Enter data member number 2-> 5
    Enter data member number 3-> 6
    Enter data member number 4-> 2
    Enter data member number 5-> 4
    Enter data member number 6-> 3
    The mean(average) of your data = 0
    Press any key to continue

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    #include <iostream.h>
    
    int main()
    {
    	double temp = 0;
    	int num;
    	cout<<"How many instances of data do you want to enter? ->";
    	cin>>num;
    	float *arguments = new float[num];
    	for (int i=0; i<num; i++)
    	{
    		cout<<"Enter data member number "<<i+1<<"-> ";
    		cin>>arguments[i];
    		double temp =+ arguments[i];
    	}
    	double mean = temp / num;
    	cout<<"The mean(average) of your data = "<<mean<<"\n";
    	return 0;
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > double temp =+ arguments[i];
    You have two temp's in scope here - the one you've just declared, and the one at the start of main.

    Unfortunately for you, you update this one, whilst the one at the start of main remains untouched.

    temp =+ arguments[i];
    is all you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Just an fyi whenever you use new always check for NULL pointer, most likely meaning insufficient memory (Dereferencing a null pointer is bad ju-ju). And always do a delete to put the allocated memory, from the new, back in the heap. They're just good programming practices.

  10. #10
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I want to add error checking, but I don't know of any easy way to check if the input is numerical or not, if anyone could help me out, that'd be great; also, the 'float devmean = temp / num;' isn't returning the right value. I don't know if its the calculations before it, or whatnot.


    Code:
    #include <iostream.h>
    #include <math.h>
    #include <string>
    
    // This program finds the standard deviation of a set of data.
    // 1: Mean
    // 2: (Data Member - mean) * 2.
    // 3: Mean of data from 2.
    // 4: Square root of numbers from 3.
    
    int main()
    {
    	float temp = 0;
    	int num;
    	cout<<"How many instances of data do you want to enter? ->";
    	cin>>num;
    	cout<<"\n--------------------------\n";
    	float *arguments = new float[num];
    	for (int i=0; i<num; i++)
    	{
    		cout<<"Enter data member number "<<i+1<<"-> ";
    		cin>>arguments[i];
    		temp = temp + arguments[i]; 
    	}
    	float mean = temp / num;
    	temp = 0;
    	cout<<"\n\nThe mean(average) of your data = "<<mean<<"\n";
    	cout<<"\n--------------------------\n";
    	for (i=0; i<num; i++)
    	{
    		arguments[i] = arguments[i] - mean;
    		arguments[i] = arguments[i] * 2;
    		temp = temp + arguments[i];
    		cout<<"Squared deviation of data member "<<i+1<<" = "<<arguments[i]<<"\n";
    	}
    	float devmean = temp / num;
    	float squareroot = sqrt( devmean );
    	cout<<"\n\n";
    	cout<<"The mean of the squared deviations = "<<devmean;
    	cout<<"\nStandard Deviation of your data = "<<squareroot<<"\n\n";
    	
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Don't know about how to make sure it's an int, but your float devmean = temp/num is probably running into typecasting problems. You're dividing a float (temp) by an int (num). Try a static cast.

  12. #12
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    If I knew what I static cast was, then maybe
    On a side note, what is a static cast? Can you give me an example or modify my code so that it fits in?

  13. #13
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Output:


    The mean(average) of your data = 4.75

    --------------------------
    Squared deviation of data member 1 = -5.5
    Squared deviation of data member 2 = -3.5
    Squared deviation of data member 3 = -1.5
    Squared deviation of data member 4 = 0.5
    Squared deviation of data member 5 = 2.5
    Squared deviation of data member 6 = 4.5
    Squared deviation of data member 7 = 0.5
    Squared deviation of data member 8 = -3.5
    Squared deviation of data member 9 = 0.5
    Squared deviation of data member 10 = 2.5
    Squared deviation of data member 11 = 4.5
    Squared deviation of data member 12 = -1.5


    The mean of the squared deviations = 0
    Standard Deviation of your data = 0

    Press any key to continue

    Based on my above code; can anyone see whats wrong?
    Also, I might get output like this:

    The mean(average) of your data = 5.17857

    --------------------------
    Squared deviation of data member 1 = -3.27714
    Squared deviation of data member 2 = -3.75714
    Squared deviation of data member 3 = 0.942858
    Squared deviation of data member 4 = -3.45714
    Squared deviation of data member 5 = -5.67714
    Squared deviation of data member 6 = 8.50286
    Squared deviation of data member 7 = 6.72286


    The mean of the squared deviations = 5.44957e-007
    Standard Deviation of your data = 0.000738212

  14. #14
    V
    Guest
    The zero variance is because of a math error on your part.

    You have each element set to its variation from the mean TIMES TWO, instead of its variation SQUARED. Because of how a mean works, if you sum the variations from the mean, you always get zero.

    So, instead of:

    arguments[i] = arguments[i] * 2;

    you need:

    arguments[i] = pow(arguments[i],2);

  15. #15
    V
    Guest
    Oh, and, as mentioned, for good form you should have this line just before the return:

    delete[] arguments;

    This line frees the memory that was allocated to your array. Anytime you allocate memory with new, you should use delete somewhere to deallocate it.

    There are a *few* exceptions to this, but as a rule of thumb, it's a really good idea to get yourself into the habit of matching every new with a delete.

    BTW, you use delete[] instead of delete because you made an array with new.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-26-2009, 02:46 PM
  2. HELP! Changing the base of a floating point number..
    By yigster in forum C Programming
    Replies: 6
    Last Post: 03-27-2008, 05:36 AM
  3. Replies: 11
    Last Post: 03-25-2003, 04:13 PM
  4. Variable size arrays
    By crag2804 in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 06:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21