# Thread: organising data in arrays

1. ## organising data in arrays

Hi I was hoping I might as a few questions concerning organising data in arrays

for example, say I have 3 arrays, int array1[19], int array2[5] and float array3[5]

array1 is a set of 20 roles of the dice. So I role a dice, and that number is saved in the array1, then I role the dice again and the next number is saved in array1 etc until 20 readings are saved.

each dice value, has a number of points attached to it.

so a dice roll of 1 is worth 5 points, 2=10, 3=15, 4=20, 5=25, 6=30

teh user rolls the dice 20 times, each time typing in the result and the code saving it into the array1

what technique would I then use to get the code to automatically calculate the points for each dice roll and save that in another array?

hopefully this isnt too complicated, Im just trying to understand how you would manipulate data using arrays

thank you and all the best

2. Rather than trying to visualise the whole thing in your head, identify some simple part (say roll a dice 20 times) and see where you get to.
http://cboard.cprogramming.com/c-pro...t-process.html

Small problems are easy to solve by stepwise refinement. You do something, and the next step becomes a lot clearer to you.

3. ok, perhaps I should put something together so that we have a real working example. According to the development process you've posted, here is the first stages so perhaps we can work on the next stages together and it will help me understand better how the development process works and also help me learn the skills Im trying to improve on. Here is teh basic structure of the program so that anyone who has time and is willing can work on it with me:

Code:
```#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

int main()

{

int dice[19];
int diceface[6];
float points[6];
int loop;
srand(time(NULL));

for(loop=0; loop<19; loop++)

{
dice[loop] = (int)(rand()%6);
}

for(loop=0; loop<6; loop++)

{
diceface[loop] = loop;
}

for(loop=0; loop<19; loop++)

{
dice[loop] = loop*5;
}

fflush(stdin);
getchar();

return 0;

}

```

4. what i need to understand is the process of matching and manipulating data from arrays so hopefully this little project will be suffcient

I was hoping to read some articles but I just search for arrays I get basic articles explaining how to actually make arrays so wasnt sure what to sepcifically search for.

5. in your initialization, you should initialize your array of points so that points[0] = 5 (score for roll == 0). points[1] = 10 (score for roll == 1) etc.

then you first loop generates the rolls. that looks good. (although now you are only generate 19 rolls, not 20)

now to get the score for each roll, index the points array by the value in the dice array. dice[n] contains an index from 0 to 5 that you use to get the points from points array.

score for roll 0 = points[dice[0]];
score for roll 1 = points[dice[1]];
etc

on the other hand if the score for each roll is just a multiple of the roll as your example (or some other calculation based on the number of the roll), you can do something like in loop 3, except it should look something like

score for roll 0 = (dice[0] + 1) * 5;
score for roll 1 = (dice[1] + 1) * 5
etc

but if for example the points for each roll weren't related, say 1,4,5,9,16,100, the computation method wouldn't work. then the first method is used.

6. dmh if the scores werent in multiples, how would I index the different arrays together?

cud u give some practical examples in code?

for the points, could i just put points[]=diceface[]*5?

would this automiatically understand that points[1]=diceface[1]*5, points[2]=diceface[2]*5 etc?

7. Code:
`for the points, could i just put points[]=diceface[]*5?`
you can't leave out the array index, thats a syntax error, so you do that in a loop
Code:
```for(i=0;i<6;++i) {
points[i] = diceface[i] * 5;
}```

8. lets say for example, you have four arrays:

array1[10]
array2[10]
array3[10]
array4[10]

array1 is a set of 10 random numbers e.g. 2, 5, 2, 1, 0, 3 etc
array2 is a set of values e.g. 1,2,3,4,5, etc
array3 is a number linked to each of the values in array2 e.g. 1,2,4,8,16 etc (so 0=0, 1=1. 2=2. 3=4, 4=8, 5=16 etc)

array4 needs to basically be an array of numbers determined by the random numbers in array1. So array4 using the example figures above would be 2, 16, 2, 1, 0, 4 etc because it2 in array1 means that array4[0] will be 2 but its equivilent from array3.

Im sorry this is complicated to explain, but am quite stuck on how to do it so any help or suggestions would be greatly appreciated if anyone follows what im trying to say

9. Ive tried using temporary variables to store things in, but im getting this error:

1>\\homespace.shu.ac.uk\b0034376\mywork\visual studio 2010\projects\assignment 3\assignment 3\prototype 3.cpp(71): error C2108: subscript is not of integral type

any idea what the error means?

10. ok i figured it.

seems c doesnt like floats in the [] of an array for some reason

11. my coding seems to not be working. Any ideas whats wrong with it:

Code:
``` for(loop=0; loop<readings; loop++)
{
temp1 = signalOutput[loop];
temp2 = column1[temp1];
temperatureArray[loop] = column2[temp2];
printf("\n %i = %f", signalOutput[loop], temperatureArray[loop]);
}
```

12. Originally Posted by Dawud.Beale
seems c doesnt like floats in the [] of an array for some reason
You obviously don't understand how arrays work. Think of a box with compartments, you can't put something into the 2.6th compartment either, can you?

13. that looks ok in isolation. what do you mean by 'not working'?

things to check
are the values in signalOutput integers, and are they all within the the range of column1?
are the values in column1 integers, and are they all withing the range of column2?

how to check these thing:

one technique is use assertions.
- add '#define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))' to the top of your program. this macro returns the number of elements in an array.

Code:
```#include <assert.h>
#define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
...

{
temp1 = signalOutput[loop];
assert(temp1 < ELEMENTS(column1));  // this will throw an assertion if an element is out of range. if the element is withing range, it will do nothing
temp2 = column1[temp1];
assert(temp2 < ELEMENTS(column2)); // this will throw an assertion if an element is out of range. if the element is withing range, it will do nothing
temperatureArray[loop] = column2[temp2];
printf("\n %i = %f", signalOutput[loop], temperatureArray[loop]);```
another alternative is to use a debugger and step through to make sure you don't index out of the array bounds. or just print temp1 and temp2 and inspect to make sure they are not out of bounds

`fflush(stdin);`