Dynamically Add Rows to an Array

Started by
10 comments, last by PsyberMind 13 years, 3 months ago
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
Advertisement
Take a look at std::vector, specifically the push_back function.
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.
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, dynamicallyfor(int i = 0; i < Size; ++i){ //ask for input cin >> array;}//do stuff with arrray//and when you are donedelete [] array; //free the memory
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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.
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;    }    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;    return total;}
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!
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;        }        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;                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]
Thank you.. it makes sense now.. I can see I've still got a ways to go :)
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.

This topic is closed to new replies.

Advertisement