GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2005, 10:59
eccoflame eccoflame is offline
New Member
 
Join Date: Apr 2005
Posts: 13
eccoflame is on a distinguished road

Need Help URGENT


in the attachment is the main code
need to get a random number form initcells.h
just let me post it
CPP / C++ / C Code:
#include <stdio.h>
#include "initcells.h"
#include <ctype.h>

void getNumber(int *number);
void printNum(int cells [][MAXCELLS]);

int main()
{
  int number;
  int cells[2][MAXCELLS];
  getNumber(&number);
  initCells(cells,number);
  printNum(cells);


  return 0;
}

void getNumber(int *number)
{
  /*read number & check error*/

  char temp;
  int check;

  do
  {
    printf("Please enter non-zero seed value for the automata : ");

    check = scanf ("%d",number);
    if (check != 1)
    {
       do scanf ("%c", &temp);
       while (temp != '\n');

    }
  }
  while (check != 1 || *number == 0);


}

void printNum(int cells[][MAXCELLS])
{
        int a;
        int b;
        int count=1;
        char cas;
        for(a=0;a<MAXGEN;a++)
        {
           printf("\nGen%3d ",count);
           for(b=0;b<MAXCELLS;b++)
           {
                if(cells[a][b]==0)
                {
                     printf(" ");
                }
                else if(cells[a][b]==1)
                {
                     printf(".");
                }
                else if(cells[a][b]==2)
                {
                     printf("+");
                }
                else
                {
                     printf("#");
                }
                

           }

        count++;
        }
        printf("\n");
}

CPP / C++ / C Code:
#define MAXCELLS 64
#define MAXGEN 20
#define NUMSTATE 4

void initCells(int cells[][MAXCELLS], int seed);

void initCells(int cells[][MAXCELLS], int seed)
{
   /* Randomly initialises each element in row 0 of
      the cells array with either the value 0 to
      NUMSTATE - 1

      To do this it needs a seed value which can be
      any non zero integer. This seed value is used
      in a pseudo random number generator. The
      particular generator used in this function is a
      Multiplicative Congruential Generator. Most
      random generators are of this type.

      The word pseudo is used to mean the random generator
      is not really random but just appears to be. All
      computer generated random generators have this
      property. Good generators just appear to be more
      random based upon a whole bunch of statistical
      tests.
   */

   int col;

   for (col=0; col<MAXCELLS; col++)
   {
      /* Spin the random number generator around */
      seed = (seed >> 15) + ((seed << 16) & 0x7ffffff1)
             - (seed >> 20) - ((seed << 11) & 0x7ffffff1);
      if (seed < 0) seed += 0x7ffffff1;

      /* Set cell col to 0 to NUMSTATE-1 using the random seed */
      cells[0][col] = seed % NUMSTATE;
   }
}


if the number i put in is 12 then output should be like this
Gen 1 . # .++.++ ##+##.+. #.. .## + ++ #. + # . ++#+++ +.+ . #.###
Gen 2 .... .++++++ ####+++. ....# + ++ ..+ ...+####+ +++... .#
Gen 3 ..++..++####+ ###++..++. + ++ ..++ ...++## ## +#+++... .
Gen 4 +++++++## ## ... ##++++++.. + ++..+++..+++## ####+++....
Gen 5 +####### ..+... ######++..+ +++++#++++### .... ###+++++
Gen 6 ## .....++++.. ##++++ +########## ..++.... ######
Gen 7 .......+++++##++...... ####+ ## ...++++++...
Gen 8 ....+++++++#######++++++.. ## ..........+++####+++........
Gen 9 +++++####### ######++.... ....++++++++++### ###+++++++++
Gen 10 ###### ... ##++++......++++########## ##########
Gen 11 .......+...... ####++++++++#### ....
Gen 12 ........++++++++++++.. ########## ..........++............
Gen 13 +++++++++##########++.... ....+++++++++++++++++++++++
Gen 14 ########## ##++++..............++++######################
Gen 15 ...... ####++++++++++++++++####
Gen 16 ............++++.. ################## ....................
Gen 17 +++++++++++++##++.... ....+++++++++++++++++++
Gen 18 ################++++......................++++#### ##############
Gen 19 ####++++++++++++++++++++++++####
Gen 20 .............. ########################## ................


but it came out like this
Gen 1 . # .++.++ ##+##.+. #.. .## + ++ #. + # . ++#+++ +.+ . #.###
Gen 2 #### ### ################### ### # # ## ####
Gen 3 #### .# .# ################## ############
Gen 4 ################## ############################################
Gen 5 ################################################## ##############
Gen 6 Segmentation Fault (core dumped)



i reallly need this coz it due on this friday thx
Attached Files
File Type: txt ***1.txt (1.4 KB, 5 views)
  #2  
Old 19-Apr-2005, 13:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by eccoflame
in the attachment is the main code
need to get a random number form initcells.h
j
i reallly need this coz it due on this friday thx


A few points:

First of all: did you notice that the first row has correct values? (Look again, in case you missed it.)

Second: How many rows are in the cells array?

Third: How many rows are filled in by the function initCells()?

Fourth: How many rows are printed out by the function printNum()?

Regards,

Dave

A PostScript: Where did you get the stuff that you say "should be like this". If that's what the generator actually gives, then I would say it is an extremely poor function. I would never expect the long run shown on the later rows from a uniform variate. If you (or someone) is really interested, maybe someone can do some analysis to see what's the probability that the given samples came from a uniformly distributed random variable. I'll bet it's realllllly small. On the other hand, I would expect the given algorithm to perform much better. Just thought I would mention it.

D
Last edited by davekw7x : 19-Apr-2005 at 14:04.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Communication via the rs232 interface banur22 C++ Forum 1 13-Apr-2005 12:04
Urgent Problem with Checkboxes inside a Tree Control on MSVC 6 yonoid MS Visual C++ / MFC Forum 0 10-Mar-2005 12:46
urgent help needed :c + mysql insert jack C Programming Language 1 13-Apr-2004 22:16
urgent help:string concat jack C Programming Language 3 11-Apr-2004 10:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:17.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.