Like Tree3Likes
  • 2 Post By Salem
  • 1 Post By dmh2000

organising data in arrays

This is a discussion on organising data in arrays within the C Programming forums, part of the General Programming Boards category; Hi I was hoping I might as a few questions concerning organising data in arrays for example, say I have ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    102

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    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.
    A development process

    Small problems are easy to solve by stepwise refinement. You do something, and the next step becomes a lot clearer to you.
    Dawud.Beale and camel-man like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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.
    Last edited by Dawud.Beale; 04-26-2012 at 11:17 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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.
    Dawud.Beale likes this.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    ok i figured it.

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

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    102
    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. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Dawud.Beale View Post
    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?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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 <assert.h>' to the top of your program
    - 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.

    change your code to this:
    Code:
    #include <assert.h>
    #define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
    ...
    
     for(loop=0; loop<readings; loop++)
    {
        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

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    When you get a moment, read the FAQ about why:
    Code:
    fflush(stdin);
    is wrong and should never be used.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. way of organising you code
    By yes in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2006, 11:21 AM
  2. help needed in organising a debugging contest
    By shrijeetp in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-22-2006, 06:45 AM
  3. Arrays Data Structures
    By silicon in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2003, 01:40 PM
  4. Getting data from file using 2D arrays
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-07-2001, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21