#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include<conio.h>

const int LOW = 1;
const int HIGH = 150;
int main()
{                   
  int grid[30][30]={0},x,j,i,die;
  int data_points, tempx, tempy;
  for(x=0;x<150;x++)
  {
    tempx = rand() % 30 + 1;
    tempy = rand() % 30 + 1;
    data_points = rand() % (HIGH - LOW + 1) + LOW;
    grid[tempx][tempy]=data_points;
    printf("%d\n",data_points);
  }
  for(i=0;i<30;i++){
    for(j=0;j<30;j++){
      printf("x=%d\t y=%d\t index=%d\n",i,j,grid[i][j]);
  }}
  getch();
}

when we run this program we get the value for the grid[j].In this as we are generating the values randomly by the rand function.So we may get more than one value at some point in the grid[j] but the values are getting overwritten.Can anyone help in implementing linked lists to this concept and trying to get more values for the grid[j].

Hoping a positive reply

I don't really understand what your doing here,
this has nothing to do with linked lists.

you need to do use a struct like

typedef struct lnode
{
int data;
struct lnode *next;
} listnode_t;

Why have you made two threads posting the exact same (badly presented) question.. its not going to make it get answered any quicker.

1. Please, use code tag for your snippet:
[code=c] your snippet here

[/code]
2. Serious (and classic) mistake:

int grid[30][30]={0},x,j,i,die;
...
tempx = rand() % 30 + 1; /* in 1..30 range */
tempy = rand() % 30 + 1;
...
grid[tempx][tempy]=data_points; /* must be in 0..29 range */

3.

Can anyone help in implementing linked lists to this concept

Linked list of what? You have a grid of integers. Do you want a grid of lists of integers?!
What for? Why randomize i and j? Assign randomized values to EVERY cell of the grid - that's the grid randomization - exactly the same in probability theory sense as overrandomizing with random (i,j,value). If you want to study "random hits with random weights", present more exact specifications.

Alas, I put my foot into this double trap...

1. Please, use code tag for your snippet:
[code=c] your snippet here

[/code]
2. Serious (and classic) mistake:

int grid[30][30]={0},x,j,i,die;
...
tempx = rand() % 30 + 1; /* in 1..30 range */
tempy = rand() % 30 + 1;
...
grid[tempx][tempy]=data_points; /* must be in 0..29 range */

3.
Linked list of what? You have a grid of integers. Do you want a grid of lists of integers?!
What for? Why randomize i and j? Assign randomized values to EVERY cell of the grid - that's the grid randomization - exactly the same in probability theory sense as overrandomizing with random (i,j,value). If you want to study "random hits with random weights", present more exact specifications.

what i need here is to create a linked list of integers i.e data_points in this case.I need to keep all the data_points in the grid[30[30] and and print out the values for a given position in the grid .
As we are generating the data_points randomly there will be 2 or more values for a certain position as both the x,y coordinates are also generated randomly.So, that is why i need to create a list for the data_points to print out all the values.

So, finally i need to print out the x,y coordinates and their respective data_values for a given position(i.e x=24 y=12 index=23,88,56) if there are 3 indexes that have come randomly for a certain position in the grid.

I don't like a chimera of a matrix and a list of integers. Better use jagged array concept.

Regrettably, it's my time to go. If this thread will be opened tomorrow (may be ;)) I should try to help you with jagged array implementation.

PS. Of course, the simplest solution is 3D array: grid + data_points values dimension. Add a matrix with current sizes for every grid cell - that's all. Memory in exchange for speed and simplicity...

Look at this dynamic grid improvisation:
Header file hitstuff:

/** @file
 *  MxN dynamic grid declarations.
 *  No warranties, no maintenance.
 */
#ifndef HITSTUFF_H
#define HITSTUFF_H
/** Initialize MxN grid module */
void hitInit(int m, int n);
/** Deallocate grid module resourses */
void hitStop();
/** Returns a number of grid rows */
int hitRows();
/** Returns a number of grid columns */
int hitCols();
/** Returns total hits counter */
int hitCounter();
/** Misc: set dyn memory increment in num of elements */
int hitStep(int step);
/** Add the next value to grid[i][j] */
void hitNext(int i, int j, int value);
/** Returns a number of hits in grid[i][j] */
int hitSize(int i, int j);
/** Returns the pointer to grid[i][j] data */
int* hitData(int i, int j);
/** Allocate 3d array for grid[i][j][k] values.
 *  Don't forget to free(this pointer).
 */
int*** hit3D();
#endif

Implementation file hitstuff.c:

/** @file
 *  Dynamic MxN grid module in C 
 *  (C++ class object surrogate).
 *  Compiled and tested in C and C++.
 */
#include "hitstuff.h"
/* Dependencies:
#include <stdlib.h>
 */
/** Grid module implementation macros */
#define IJ(i,j)  ((i)*N+(j))
#define PTR(i,j) (hitSlot+IJ(i,j))
/** Grid internal container element type */
typedef
struct Hitstr {
    int*data;
    int size;
    int curr;
} Hits;
/** Locals */
static int M, N, hitCnt;
static size_t addStep  = 8;
/** Non-initialized cell internal value */
static Hits   dummy[1] = {{&dummy[0].size,0,0}};
/** Pointer to allocated grid container */
static Hits*  hitSlot  = dummy;

/** Internal: reinitialize dummy cell */
static void hitMisc() {
    Hits* p = dummy;
    if (p->data != &p->size) {
        p->data = &p->size;
        p->curr = p->size = 0;
    }
}
/** Functional interface implementation */
int hitRows() { return M; }
int hitCols() { return N; }

int hitCounter() { return hitCnt; }

int hitStep(int step)
{
    int old = addStep;
    if (step > 0)
        addStep = step;
    return old;
}

void hitNext(int i, int j, int value)
{
    Hits* p = PTR(i,j);
    if (p->curr >= p->size) {
        p->data = p->size
        ? (int*)realloc(p->data,
                sizeof(int)*(p->size += addStep))
        : (int*)malloc(sizeof(int)*(p->size = addStep));
    }
    p->data[p->curr++] = value;
    ++hitCnt;
}

void hitStop()
{
    if (hitCnt) {
        int i, mn = M * N;
        Hits* p = hitSlot;
        for (i = 0; i < mn; ++i, ++p)
            if (p->curr)
                free(p->data);
        hitMisc();
    }
    if (hitSlot) {
        free(hitSlot);
        hitSlot = dummy;
    }
    M = N = hitCnt = 0;
}

void hitInit(int m, int n)
{
    int i, mn = m * n;
    Hits* p;

    if (hitSlot != dummy)
        hitStop();
    M = m;
    N = n;
    hitSlot = (Hits*)malloc(sizeof(*hitSlot)*mn);
    for (i = 0, p = hitSlot; i < mn; ++i, ++p) {
        p->curr = p->size = 0;
        p->data = &p->size;
    }
    hitCnt = 0;
}

int hitSize(int i, int j)
{
    return PTR(i,j)->curr;
}

int* hitData(int i, int j)
{
    return PTR(i,j)->data;
}

int*** hit3D()
{
    void** ppvoid;
    void** p;
    void** q;
    int i, j, m, n;
    
    if (hitSlot == dummy)
        m = n = 1;
    else
        m = M, n = N;

    ppvoid = p = (void**)malloc(sizeof(*ppvoid)*m*(n+1));
    for (i = 0; i < M; ++i)
    {
        ppvoid[i] = q = (p += M);
        for (j = 0; j < N; ++j)
            q[j] = hitData(i,j);
    }
    return (int***)ppvoid;
}

Test routine (grid 3x3, 9 values for every cell except diagonally uninitialized):

#include "hitstuff.h"

void TestGrid()
{
    int i, j, k;
    int*** grid;

    hitInit(3,3);
    /* Initialize grid */
    for (i = 0; i < hitRows(); ++i)
    {
        for (j = 0; j < hitCols(); ++j)
        {
            if (i != j)
            for (k = 0; k < 9; ++k)
                hitNext(i,j,(i+1)*100+(j+1)*10+k+1);
            /* ijk values for clearness */
        }
    }
    /* Make 3D jagged array with all accumulated values */
    grid = hit3D(); 
    /* Print all values */
    for (i = 0; i < hitRows(); ++i)
    {
        for (j = 0; j < hitCols(); ++j)
        {
            printf("[%d][%d]:",i+1,j+1);
            if (hitSize(i,j))
            {
                for (k = 0; k < hitSize(i,j); ++k)
                    printf(" %d",grid[i][j][k]);
            }
            else printf(" None");
            putchar('\n');
        }
    }
    /* Print number of values in the grid */
    for (i = 0; i < hitRows(); ++i)
    {
        for (j = 0; j < hitCols(); ++j)
        {
            printf(" %d",hitSize(i,j));
        }
        putchar('\n');
    }
    free(grid);
    hitStop();
}
/* Output:
[1][1]: None
[1][2]: 121 122 123 124 125 126 127 128 129
[1][3]: 131 132 133 134 135 136 137 138 139
[2][1]: 211 212 213 214 215 216 217 218 219
[2][2]: None
[2][3]: 231 232 233 234 235 236 237 238 239
[3][1]: 311 312 313 314 315 316 317 318 319
[3][2]: 321 322 323 324 325 326 327 328 329
[3][3]: None
 0 9 9
 9 0 9
 9 9 0
*/

Minimal debugging only.
You may use this code for any (non-commercial;)) purposes...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.