0

Anyone know how to create triangular array in C? I tried to use malloc, firstly for first "dimension" and then i used for loop to create second dimension with malloc, but my leading teacher said that this isn't correct.

 int **price_of_diamond, i, j;
price_of_diamond=malloc((count*sizeof(int)));
for(i=0;i<conut;++i){
    price_of_diamond[i]=malloc((i-1)*sizeof(int));
}

Tip for the task was to "create triangular array(getting shorter arrays)". Program worked in theory but teacher said that this is wrong implementation, but didn't said what was bad

oszust002
  • 65
  • 6
  • 4
    Show the code you tried and explain why it failed. – Iharob Al Asimi Jan 29 '15 at 17:31
  • but there is ++i not i++ – oszust002 Jan 29 '15 at 17:55
  • Guys, this is all wrong since he's trying to get a triple dimentional array - Malloc-ceptioning with a two dimentional one would just be messy, and since it's 2 dimentions and not 3 it wouldn't be helpful for the OP. – A. Abramov Jan 29 '15 at 18:10
  • @A.Abramov you are still talking about 3D-array. Why? I asked about something like triangle matrix, where in every line i have different amount of columns. For example if i have 3 prices and i want to sum them, then i want to create 2D-triangle array, in which there is a sum of two prices Example: Price 1: 1; – oszust002 Jan 29 '15 at 18:16
  • @A.Abramov you are still talking about 3D-array. Why? I asked about something like triangle matrix, where in every line i have different amount of columns. For example if i have 3 prices and i want to sum them, then i want to create 2D-triangle array, in which there is a sum of two prices Example: Price 0= 1; Price 1=2; Price 2=3; And i want to create array with every possible combination of sum of 2 prices(we don't add prices of the same index) And i want to have 2D-array sum[i][j] where i and j is index of 2 prices which were summed – oszust002 Jan 29 '15 at 18:21

2 Answers2

1

The first allocation should use (int*) not (int).
you shouldn't use malloc with a size <= 0 in your loop (when i=0 and i=1). Using (i+1), your arrays will vary from 1 to count size.

price_of_diamond = malloc(count * sizeof(int*));
for(i=0;i<count;++i) price_of_diamond[i]=malloc((i+1)*sizeof(int));
tetorea
  • 141
  • 8
-2

Creating arrays is not about inception-ing them into each other. You can't "add a dimention" to an array by malloc-ing it again, that'd just re-assign the new array into what used to be the first array. The solution is initiallizing it as a 3d array, like so:

const int sizeDimOne=4; // size of the first dimention
const int sizeDimTwo=4; // size of the second dimention
const int sizeDimThree=4; // size of the third dimention
int **threedim = malloc(sizeDimOne*sizeDimTwo*sizeDimThree*sizeof(int)); // declaring an array is simple : you just put in the values for each dimention.

Never forget to free it in the end of the code, data-leaks are bad! :)

free(array); // Super important!

would create the array. If you want to manually assign the values, let me draw an example from a great site: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

/*Manually assigning a double-dimentional array for example.
* a very simple solution - just assign the values you need,
* if you know what they are. */
int a[3][4] = {  
{0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
{4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
{8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

EDIT: seeing your code, i see you guys use pointers for declaration. Here's a great example from one of the sources i mentioned earlier, slightly modified, regarding that exact use:

const int nrows = 3; // number of rows.
int **array;
array = malloc(nrows * sizeof(int *)); /* That's because it's an array of pointers in here,
* since you're using the pointer as an array, the amount of datarequired changes.
* dont forget to free! 
*/
A. Abramov
  • 1,823
  • 17
  • 45
  • Which one, the last? What does it say? i'd compile it myself but i`m not home. – A. Abramov Jan 29 '15 at 18:02
  • array = malloc(sizeDimOne*sizeDimTwo*sizeDimThree*sizeof(int)); – oszust002 Jan 29 '15 at 18:03
  • Ehh forgot the cast. Editted – A. Abramov Jan 29 '15 at 18:06
  • ? Casting in C is [not necessary/deprecated](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). On further inspection: this is an array of pointers to pointers *to pointers* to ints -- it's Inception alright but probably not what the OP meant. – Jongware Jan 29 '15 at 18:09
  • I meant, casting the array into a triple pointer, my bad :) edit: It's actually exacly what he meant, i think. It's a pointer to a pointer to a pointer to an int, and each pointer gets a specific amount of data - which is similar to an array. In order to access an element, you can just go to the pointer in position (pointer+dim1pos*dim2len*dim3len+dim2pos*dim3len+dim3pos). – A. Abramov Jan 29 '15 at 18:11
  • (Interpreting the italics switches as `*`) You got the size right, but each single *element* is then a pointer-to-pointer-to-int. I feel it ought to be a simple int, that is, the array is of type `int *` -- one level deep. – Jongware Jan 29 '15 at 18:37
  • You don't need the cast except if you are using a c++ compiler, in that case you should use `new int[count + 1]` instead. – Iharob Al Asimi Jan 29 '15 at 22:06