Thread Breakpoint at an array assignment

im trying to make an array that is (n x n) large depending on what value you enter, and then populate that array with random integers. Could you guys please help me with two problems?

  1. Any values of n greater than 4 give "error at display" in the last rows, if i display what value it would have been if it wasn't an error it is the same across, and something huge like 20019238394
  2. Why does it create the same number all the way across? Is it not generating new random numbers?

    #include <iostream> #include <string> #include <cmath> #include <cstdlib> #include <time.h> using namespace std; int arrayCreate(int); int main(int argc, const char * argv[]) { int n; cout << "enter how big your array is (n) , it will be shown as (n x n)" << endl; cin >> n; cout << "------------------------------" << endl; arrayCreate(n); return 0; } int arrayCreate(int n){ srand (time(NULL)); int y=0, x=0; int original[x][y]; //putting in random values for (int y=0; y< n; y++){ for (int x=0; x< n; x++){ int check = (rand() % 9 + 1); if (check < 10) original[x][y] = check; else cout << "error"; } } //displaying those values for (y=0; y<n; y++){ for (x=0; x<n; x++){ if (original[x][y] < 10) cout << original[x][y] << " "; else cout << "error at display"; } cout << " y is " << y << endl;} return original[x][y]; }

-------------Problems Reply------------

You're declaring your array with size [0][0]. What you wanted was size [n][n].

As a result, you're writing to unallocated memory. To correct:

int original[n][n];

should work for you.

when creating a new array you should allocate it on the heap, so it would be:

int** original = new int* [n]; // creates column
for(int i=0; i<n; i++)
original[i] = new int [n]; // creates rows

then when it's no longer needed, have to free memory

for(int i=0; i<n; i++)
delete original[i];

delete original;

Category:c# Views:3 Time:2018-08-05
Tags: arrays random

Related post

  • Javascript unexpected console output with array assignment; 2012-01-25

    I am getting unexpected console output after array assignment in webkit browsers (Chrome 16.0.912.77 and Safari 5.1.2 - 7534.52.7). Here is my function that demonstrates the error: function test() { var myArray = []; console.log(myArray); // Should b

  • (bool)true var does not get converted to array on array assignment, but (bool)false does. Why? 2010-07-12

    Using PHP... an example. This produces a warning - as expected - and $myVar stays as bool(true). $myVar = true; $myVar[] = 'Hello'; // Warning: Cannot use a scalar value as an array But this next example 'works', $myVar is converted into an array wit

  • Problem with array assignment 2010-07-13

    I've got the following problem with an array assignment... I've got a global float array in declared as... public static float camObjCoord[] = new float[8000]; I'm then filling its contents with. public void addcube(float highx, float low

  • How to use multiple syncronized threads to sort an array? 2010-11-21

    I want to sort an array using multiple syncronized threads. For example I want to use at least 2 threads to sort the array and another thread to show me at some moments the situation of sorting. Some ideas please? --------------Solutions-------------

  • PHP 5.3 Windows Array assignment causes fault 2011-02-19

    I am struggling to understand why PHP is faulting without feedback or error (beyond windows error log, fault in php5 faulting module php) whilst executing the following block of code: $projects = array(); $pqRes = $connection->getResult("SELECT bi

  • PHP Multi Dimension Array Assignment 2011-12-02

    I need to fill a multidimensional array and here is my code I have so far for it. while($num > $i) { $default[$i]=0; $defaultcounter=0; $default2[$i]=0; $default3[$i]=0; $query="Select * from `issues` WHERE `app`='" . $applist[$i] . "'" . "AND `st

  • Why array assignment operation doesn't exist but structure assignment does in C language? 2012-01-20

    int a[10]; int b[10]; a = b; // illegal typedef struct { int real; int imag; } complex; complex c,d; c = d; //legal [I realize that a and b are addresses in 1st case,but symbols in 2nd case] --------------Solutions------------- For historical info, t

  • Type mismatch on an Array assignment in ASP 2009-03-15

    Its been a while since I've worked with ASP, but I'm getting a Type mismatch error on what seems to be a simple assignment statement. Can anyone shed some light on why this might be happening. This works, but when I try to foreach an unassigned Quest

  • What kind of memory semantics govern array assignment in c#? 2009-08-12

    Given the following: byte[] sData; and a function declared as private byte[] construct_command() if I were to then assign the result of construct_command() to sData would sData just point to the contents of what's returned from the function or would

  • java array assignment question 2009-12-28

    While using method Arrays.deepToString(Object [] a) I am facing this problem which I can put down in this way. Object [] not_allowed = new int[]{7, 9, 8}; Object [] allowed = new int[][]{{1, 2, 3}, {6, 5, 4}}; Why i am not allowed to assign int[] to

  • Parallel array assignment in PHP 2010-02-02

    Most languages make it easy to take an array like [1, 2, 3] and assign those values to variables a, b, and c with a single command. For example, in Perl you can do ($a, $b, $c) = (1, 2, 3); What's the corresponding trick in PHP? [Thanks so much for t

  • thread safe containers in arrays 2010-02-03

    can anyone please explain, whether accessing an array by multiple threads, where each thread is working on a different element of the array. so there are n elements, and n threads ==> the nth thread is working on the nth element of the array? can

  • Thread and two dimensional array in objective C? 2010-03-26

    Hey, guys, I am just starting to wrap my head around objective C and I am doing a little project on Iphone. And I just encountered a weird problem. I had to deal with images in my program so I have a lot local variables declared like temp[width][heig

  • Java array assignment (multiple values) 2010-04-04

    I have a Java array defined already e.g. float[] values = new float[3]; I would like to do something like this further on in the code: values = {0.1f, 0.2f, 0.3f}; But that gives me a compile error. Is there a nicer way to define multiple values at o

  • Thread safety in C# arrays 2010-05-09

    Does having 2 different threads : one reading from a C# array (e.g from first location), and another one writing to the same C# array but to a different location(e.g to the last location) is thread safe or not? (And I mean here without locking readin

  • numpy array assignment problem 2010-06-17

    I have a strange problem in Python 2.6.5 with Numpy. I assign a numpy array, then equate a new variable to it. When I perform any operation to the new array, the original's values also change. Why is that? Please see the example below. Kindly enlight

  • Ruby variable (Array) assignment misunderstanding (with push method) 2010-09-08

    I have discovered a flaw in my understanding of Ruby or programming theory or both. Look at this Code: #!/usr/bin/ruby -w @instance_ar = [1,2,3,4] local_ar = @instance_ar local_ar_2 = local_ar ### irrelevant_local_ar = [5,6,7,8] ### for i in irreleva

  • PLPGSQL array assignment not working, "array subscript in assignment must not be null" 2010-12-24

    When assigning mobilenumbers to a varchar[] in a loop through results it gives me the following error: "array subscript in assignment must not be null" Also, i think the query that joins member uuids, and group member uuids, into one, grouped on the

  • C/C++ arrays assignment 2010-12-25

    Sample code: int ar[3]; ............ ar[0] = 123; ar[1] = 456; ar[2] = 789; Is there any way to init it shorter? Something like: int ar[3]; ............ ar[] = { 123, 456, 789 }; I don't need solution like: int ar[] = { 123, 456, 789 }; Definition an

  • NSMutable array - assign and retain objects 2011-02-21

    I need some info about how to assign, retain objects. For example - if we have two viewcontrollers and needed to pass an array data from viewcontrlr 1 to viewContrl 2, how can we send the object from view 1 to view 2 and release it in view 1 and reta

  • c++ array assignment of multiple values 2011-04-20

    so when you initialize an array, you can assign multiple values to it in one spot: int array [] = {1,3,34,5,6} but what if the array is already initialized and I want to completely replace the values of the elements in that array in one line so int a

  • Perl threading with hash of arrays 2011-06-14

    I am trying to share a hash of arrays with threads::shared module as below. use warnings; use strict; use threads; use threads::shared; my @allThreads; share (my %array); foreach ('alpha', 'beta', 'gamma') { $name = $_; push @allThreads, threads->

  • difference between push and normal array assignment in javascript 2011-07-19

    I'm using flot api to draw charts. Look at this chart type HERE. In that example they've used values like, var sin = [], cos = []; for (var i = 0; i < 14; i += 0.5) { sin.push([i, Math.sin(i)]); cos.push([i, Math.cos(i)]); } Now the chart was draw

  • pointer to array in C : Address Arithmetic and Array Assignment 2011-08-02

    I have following pointer to array variable. int (*p)[3]; int a[3] = { 1,2,3 } ; int b[3] = { 11,22,33 } ; int c[3] = {111,222,333} ; I want to store these 3 array into variable p. How i have to allocate the memory for p and How should i store these 3

  • Powershell array assignment assigns variable, not value? 2012-01-09

    I have an example of a program that creates an array, and then attempts to assign the value of that array multiple times into another array as a multidimensional array. $a [email protected](0,0,0) $b = @($a,$a,$a) $b[1][2]=2 $b 'And $a is changed too:' $a The outpu

Copyright (C), All Rights Reserved.

processed in 0.158 (s). 11 q(s)