Sign in to follow this  

Dynamically Add Rows to an Array

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok another question (I'll probably have a lot of these)

I've got a small block of code that calls for a user to enter a number, said number defines the number of columns in an array. Then it loops through the array x times prompting for the value of that column.

Compiles fine, runs fine, until the end. I get this:

Run-Time-Check failure #2 -Stack around the variable x was corrupted

Here is the code, it looks simple enough:


int x = 0;
int entries[1];
int sum = 0;
int average = 0;
cout << "Enter the number of entries: ";
cin >> x;

for (int n = 0; n < x; ++n)
{
cout << "Enter the value for number [" << n << "]: ";
cin >> entries[n];
}


I apologize for all these noobish questions, but well.. I'm a noob :D

Share this post


Link to post
Share on other sites
You are declaring an array with one element, then attempting to write "x" elements to it. This is undefined behaviour, meaning that the compiler may accept the code but bad things will happen when you try to run it.

Two solutions are to use a container, like diablos_blade suggests, or to use a large array (e.g. 1000 elements), and then ensure that the user never writes more than that number of elements into the array.

Share this post


Link to post
Share on other sites
Another option is the use dynamic arrays, although using vector is better. Here is an example :

int *array = 0;
cout << "Enter the number of entries : ";
int Size = 0;
cin >> Size;

array = new int[Size]; //create an array of size Size, dynamically

for(int i = 0; i < Size; ++i){
//ask for input
cin >> array[i];
}

//do stuff with arrray


//and when you are done
delete [] array; //free the memory

Share this post


Link to post
Share on other sites
Thank you! That worked perfectly.. I am running into one more snag, though..

http://pastebin.com/yF1fD9YQ

(It's getting big so I thought I'd just put it there)

I've created a math function that adds all of the elements in the array together, and stores them into sum, returning sum to the main function.

It compiles fine, and runs, but sum returns as (what looks to be) a memory address as opposed to the sum of the array elements.

Share this post


Link to post
Share on other sites
Hello ... try it like this.



#include <iostream>

using namespace std;

int sum(int numbers[], int num);

int main ()
{
int size;
cout << "Enter the number of entries: ";
cin >> size;

int *array = new int[size];

cout << endl;
for(int i = 0; i < size; i++)
{
cout << "Enter the value for number: ";
cin >> array[i];
}

int total = sum(array, size);
cout << "\nThe value of all elements in the array is: " << total << endl;

delete [] array;
}


int sum(int numbers[], int num)
{
int total = 0;

for (int i = 0; i < num; i++)
total += numbers[i];

return total;
}



Share this post


Link to post
Share on other sites
That works perfectly.. Thank you..

But can you help me out a little to see where I went wrong? I don't know if it's my tired eyes or what, but the only thing I see different is the delete array statement was moved into the main function. If you could show me, I could learn this a lot better, and these posts would be few and far between :)

I understand what it does and how it does it, I'm just not sure what the difference is from what I did. Thanks!

Share this post


Link to post
Share on other sites
This is your code ...



#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

int *array = 0;
int i = 0;
int total = 0;
int sum(int numbers[], int num);

int main ()
{
cout << "Enter the number of entries : ";
int size = 0;
int total=sum(array, size);
cin >> size;
array = new int[size]; //create an array of user-inputted size, dynamically

for(int i = 0; i < size; ++i)
{
//ask for input
cout << "Enter the value for number ";
cin >> array[i];
}
cout << "The value of all elements in the array is " << sum;
}

int sum(int numbers[], int num)
{
int sum=0;
for (i=0; i<num; i++)
{
sum+=numbers[i];
return sum;
}
delete [] array; //free the memory
}





First of all, you never get to free the memory you allocated because you return the sum first, so that statement is unreachable.

Then, you only want to return the sum after the for loop is done because only then all the elements are added to it. You return it after it added the first element.

You call the sum function before you create the array.

You want to use the return value of the sum() function. Instead, you display sum, the name of the function, which results in displaying an address.

Don't use global variables unless you have a very good reason for doing so.

[Edited by - ArthY303 on December 23, 2010 4:12:25 PM]

Share this post


Link to post
Share on other sites
WooHoo! I did it on the first try (I think)

This one takes the input and computes the factorial of the input.


// factorials.cpp : Takes the input from the user and
// calculates the factorial of that number
// by Michael Phelps

#include "stdafx.h"
#include <iostream>

using namespace std;
int process(int);

int main()
{
int x;

cout << "Please enter a positive integer ";
cin >> x;
if (x < 0)
{
cout << "That is not a positive number";
}
else
cout << "!" << x << " is " << process(x) << "." << endl;
}

int process(int number)
{
int temp;

if (number <= 1) return 1;

temp = number * process(number -1 );
return temp;
}


Compiles, and works! I might be getting the hang of this.l.

Share this post


Link to post
Share on other sites
Quote:
Original post by PsyberMind
WooHoo! I did it on the first try (I think)
Congratulations [smile]

A few constructive comments on your process function...

The name 'process' is unhelpfully generic, always give things descriptive names, in this case something like 'factorial' would be sufficient.

Your function implementation is reasonably long-winded for what it does. First you have separated the declaration of temp from its initialisation - this wasn't necessary as you could have both initialised and declared temp in a single statement, which is the preferred practice when it's possible to do so:

int temp = number * factorial(number - 1);

Secondly, you don't actually need an explicit variable at all! You can simply return the value directly rather than assigning it to temp first:

int factorial(int number)
{
if (number <= 1) return 1;
return number * factorial(number - 1);
}


Share this post


Link to post
Share on other sites

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this