Thread: Reading and writing top arrays

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Reading and writing top arrays

    Hi Guys, I haven't used C for a while so I am abiot rusty, so I need to revert back to basics.

    I want to create a logic gate sim in C and run it throug the CMD prompt.

    The user will select the gate that they want, then they will be asked how many inputs for that gate(some gates can have no more than 1 input as some have to have at least 2). how do I get that value get entered from the scanf to create an array of that size, i.e, AND gate selected with 4 inputs, so therefore the array will be 4 large.

    Secondly, the user will then be asked to set the values for the input(1's 0r 0's). how do I get these values using the scanf to populate the array? That is what I am having trouble with.

    Then gate selected will then run a for loop on the array checking to see what it contains and then output the correct response (0 or 1). The last thing I want to know is how to Get the output value to be put into an array, where the size has been specified by the user.

    e.g. User selects AND gate with 4 inputs, an array is created the size of 4. The user is asked to specify the size of the array for the output. One the gate has worked through the array the output value will be duplicated for every element in the output array.

    I hope this makes sense, I have never being very good at explaining things, general pointers or code snippet would be great.

    Thankyou in advance.

    Archie

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You don't really need a variable sized array just use part of a big one,
    or you can use malloc to allocate an array of variable size and declare a pointer to it.

    Code:
      int *buffer;
    
      printf ("How big do you want the gate? ");
      scanf ("%d", &i);
    
      buffer = (int *) malloc (i);
    You might want to make it multi dimensional ad have an output array too.


    [CODE]

    To put a value on pin 3 for example

    Code:
    
    buffer[2]=1 //or 0 if off

    That might help.

    Or basically have one column reserved for the output so you would make it

    a (n+1) by (2 to the power of n) array.

    eg n=3 gives 4 X 8 array.

    a b c |output for AND gate.
    ----------
    0 0 0 |0
    0 0 1 |0
    0 1 0 |0
    0 1 1 |0
    1 0 0 |0
    1 0 1 |0
    1 1 0 |0
    1 1 1 |1
    Last edited by esbo; 02-05-2009 at 10:59 AM.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Response

    Hey, thanks for the response. I'll test what you posted in my code hopefully tomorrow. Just a quick question, no I can't remeber if I heard this right or not; but if you use an array thats apart of a big one, do the elements that do not get used default to 0?

    If so won't that affect the the logic I code for the gates?

    Thanks again.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can set the other elements to any value you want, of course.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by qwertysingh View Post
    Hey, thanks for the response. I'll test what you posted in my code hopefully tomorrow. Just a quick question, no I can't remeber if I heard this right or not; but if you use an array thats apart of a big one, do the elements that do not get used default to 0?

    If so won't that affect the the logic I code for the gates?

    Thanks again.
    If you declare an array globally (outside of main or any function) all the values wil initially be zero. After that the safest thing to do is to set any element you intend to use to either 0 or 1 as appropiate.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by esbo
    If you declare an array globally (outside of main or any function) all the values wil initially be zero.
    Better yet, define the array as a local variable and initialise all its elements to zero, e.g.,
    Code:
    int values[MAX_INPUT_NUM] = {0};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Pipe: writing and reading.
    By apacz in forum C Programming
    Replies: 0
    Last Post: 06-07-2006, 11:12 AM
  3. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  4. reading arrays from files
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 01:23 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM