Thread: dynamic array

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    dynamic array

    I'm trying to write a program using a dynamic array that will sum all the numbers before the number entered. (You enter 3 it does 1+2+3=6) Keep getting really wrong output numbers but can't find the error.
    Code:
    #include <iostream>
    using namespace std;
    void summation(int sumArray[], int array_size);
    typedef int* IntArrayPtr;
    int n, array_size, sum = 0;
    int main()
    {
    	
    	char choice = 'c';
    	while (choice == 'c' || choice == 'C')
    	{
    	cout <<"What is your ending number?: ";
    	cin >> array_size;
    	IntArrayPtr sumArray =;
    	sumArray = new int[n];
    	summation(sumArray, n);
    	cout <<"To quit enter q; to continue enter c.";
    	cin >> choice;
    	}
    	if (choice == 'q' || choice == 'Q')
    	{
    		return 0;
    	}
    }
    void summation(int sumArray[], int n)
    {
    	while (n<array_size)
    	{sum=sum + sumArray[n];
    	cout <<sum;
    	n++;}
    	cout <<"The sum of 1 to "<<array_size
    	<<" is "<<sum<<endl;
    }

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    First, the above code does not compile, so you may want to look it over again and fix any syntactical issues.

    Second, this line executes before you assign a value to n, which is bound the source of your problem.
    Code:
    sumArray = new int[n];
    Your code also has memory leak since you never free sumArray before you assign a new array to it.

    You'd be better off using a vector, as it will simplify your code greatly, and is much more convenient to use.
    Last edited by dra; 04-22-2009 at 11:53 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    There are many things wrong.

    1) On the line of sumArray's declaration you are most probably missing a 0 between the equal and semi-colon signs;

    2) You are never defining the variable n thus completely messing sumArray and your whole summation() function;

    3) The error right above is because you most probably think that your int enumeration at the top is setting all those variables to 0 but it is not doing this. It sets only sum to 0;

    4) sumArray has no initial values thus using it in your calculations is just plain wrong;

    5) Your if() statement does nothing given that it will always be executed after the while loop;

    6) You are not using delete[] to free previously allocated memory.

    I would also note that a typedef is useless and unwanted in this case and using dynamic memory allocation is questionnable at best in this code. This could easily be done in 3-4 lines of code without that many variables and stuff.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    There are many things wrong.

    1) On the line of sumArray's declaration you are most probably missing a 0 between the equal and semi-colon signs;

    2) You are never defining the variable n thus completely messing sumArray and your whole summation() function;

    3) The error right above is because you most probably think that your int enumeration at the top is setting all those variables to 0 but it is not doing this. It sets only sum to 0;

    4) sumArray has no initial values thus using it in your calculations is just plain wrong;

    5) Your if() statement does nothing given that it will always be executed after the while loop;

    6) You are not using delete[] to free previously allocated memory.

    I would also note that a typedef is useless and unwanted in this case and using dynamic memory allocation is questionnable at best in this code. This could easily be done in 3-4 lines of code without that many variables and stuff.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by dra View Post
    First, the above code does not compile, so you may want to look it over again and fix any syntactical issues.
    Compiled for me using Microsoft Visual C++ 2008

    Second, this line executes before you assign a value to n, which is bound the source of your problem.
    Code:
    sumArray = new int[n];
    Thought line 5
    Code:
    int n, array_size, sum = 0;
    declared n and set it to 0
    Your code also has memory leak since you never free sumArray before you assign a new array to it.
    Thanks fixed it...
    You'd be better off using a vector, as it will simplify your code greatly, and is much more convenient to use.
    This is a HW problem and I must use a dynamic array.
    Still obviously learning C++ so if I got something wrong please excuse my ignorance

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by Desolation View Post
    There are many things wrong.

    1) On the line of sumArray's declaration you are most probably missing a 0 between the equal and semi-colon signs;
    Thanks fixed.
    2) You are never defining the variable n thus completely messing sumArray and your whole summation() function;
    Isn't it declared in line 5?
    3) The error right above is because you most probably think that your int enumeration at the top is setting all those variables to 0 but it is not doing this. It sets only sum to 0;
    Is there any way to fix this without declaring each one individually to 0?
    4) sumArray has no initial values thus using it in your calculations is just plain wrong;
    for a dynamic array can I just use
    Code:
    sumArray = new int[n];
    to set it to 0?
    5) Your if() statement does nothing given that it will always be executed after the while loop;
    How so...when I compile this the first time it does not ask me to enter C or Q, however every time after time it asks me to enter C or Q and then continues if I enter C and quits if I enter q. Doesn't that mean the if statement is working?
    6) You are not using delete[] to free previously allocated memory.
    Thanks fixed.
    I would also note that a typedef is useless and unwanted in this case and using dynamic memory allocation is questionnable at best in this code. This could easily be done in 3-4 lines of code without that many variables and stuff.
    Have to use a dynamic array for this assignment, and was just following the template my book gave me.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    2-3) No. A comma separates statements so

    int n, array_size, sum = 0;

    is the same as

    int n;
    int array_size;
    int sum = 0;

    What you want to do is something like:

    A) int n = 0, array_size = 0, sum = 0;

    B) int n, array_size, sum;
    n = array_size = sum = 0;

    4) I am not sure about the default values but I'm certain they will never be 0,1,2,3...n like you are using it for.

    5) You could very well enter any other character than c and C and it would quit as well.

    As for the assignment, what a load of crap. No offense there. I mean, why would your teacher want you to do that ? He is teaching you to use features of the language that have nothing to do with your assignment. Meh.

    Oh well.

    What you want to do:

    1) Create your array and initialize memory for it;
    2) Fill it with the appropriate values;
    3) Do some calculations to get the sum;
    4) Free the memory you have used using delete[].

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Quote Originally Posted by Desolation View Post
    What you want to do:

    1) Create your array and initialize memory for it;
    2) Fill it with the appropriate values;
    This is what I'm having trouble with I believe.
    1) For initialize would [codeIntArrayPtr sumArray =0;[/code] take care of it?
    2) This is what I'm really stuck on how would I fill it with an increasing value each time? I tried using my counter value "n" and re wrote to program to declare it to zero at the start like you suggested, however still the same problem of getting the wrong numbers so it's obviously not filling the array.
    As for the teacher believe me I know we spent the first 2 weeks of class learning stuff like who made the first programmable computer, what year, ect. Asking him questions is pointless, I've been stuck with this code for over 2 weeks and all he tells me is "your close keep trying".

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    1) Actually no. What happens is that new[] will allocate a block of memory for your variable and return a pointer to the first element of the block. What this does is put junk in your array.

    So

    2) Yes you should fill it with an increased value. Here's a tip:

    You are trying to fill an array from 0 to array_size with values ranging from 1 to n. That does sound like a loop to me (:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM