4 Replies - 388 Views - Last Post: 09 March 2012 - 06:46 AM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Questions about arrays in C

Posted 08 March 2012 - 02:53 PM

1) I have a several questions about arrays in C. I need to create an array, but the size of the array should be inputted from the keyboard. How can I do this? I have two methods below, are any of them correct? If neither are correct how can I achieve it?

Method 1:

int bits;

printf("How many bits do you want to transmit?: ");
scanf("%d", &bits);

int input[bits];



Method 2:

int bits;

printf("How many bits do you want to transmit?: ");
scanf("%d", &bits);

int* input;
input = malloc(sizeof(int) * bits)

if(input == NULL)
{
    fprintf(stderr, "Could allocate that much memory!");
    return 1;
}



2) My second question is that user needs to enter 0s and 1s as an input. If the array is big, the user will get bored of entering them, so I try to use an alternative method instead. Is there a possibility for me to enter 0s and 1s to that array with some random order? If yes, how? On the other hand, if I have a pattern of 16 bits, I mean 16 elements of some random order of 0s and 1s, and if the user had entered 64 bits, so an array of 64 elements will be created. Then, can I use my existing random 16 element pattern, and enter it 4 times in the 64 element array?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Questions about arrays in C

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Questions about arrays in C

Posted 08 March 2012 - 03:02 PM

1. Method 2.
2. Use a random number algorithm? Put some thought into it!
Was This Post Helpful? 1
  • +
  • -

#3 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Questions about arrays in C

Posted 08 March 2012 - 03:41 PM

For your first question I'd say it depends on how much knowledge you have. As dynamic allocation needs to be freed and be aware of memory leeks, although in simple programs there wouldn't be much of a problem, and it's good to get the hang of it as it is a great tool.


For your second question, as Jack posted you need to put some thought into making your own way of randomizing.
Was This Post Helpful? 0
  • +
  • -

#4 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Questions about arrays in C

Posted 09 March 2012 - 06:31 AM

I thought about random number generator, but I don't know how can I achieve it in general. Also the thing is that the random generated number should be either 0 or 1. As I said each element of the array will be either a 0 or 1. Someone can help me with this?
Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Questions about arrays in C

Posted 09 March 2012 - 06:46 AM

1) Both methods are fine. On the first method you allocate on the stack a variable length array, on the second one it's allocated on the heap. For Method 1 you don't have to free the memory like Method 2, but you have to make sure that your compiler supports C99 standard.

2) Why not rand() % 2 ?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1