How to put random characters into a multi dimentional array in C - c

I'm trying to build a board of 4X4 and I'm trying to use the rand function to put random characters in the board.
I need 8 pairs of characters and I don't want to have more than one pair of the same char.
how should I do it?.. I tried a lot of variations without success.
Please help.

Not sure my answer is what you exactly want. I hope it will be helpful.
It looks like your question is more like a algorithm issue. Let's say you are trying to find 8 unique random character pairs and each pair contains two different characters.
Then you can do as following:
Get all possible characters you may use, for instance A ~ Z.
Create one array and its value is a unique character pair which has two characters you want to use. You can use a nested loop to do it.
Record how many elements you have in the array. Assume the value is N.
Use function rand() and number N to get one random number r1.
Pick up the value at position r1 of array and put it into your board.
Switch this element with the last element of array.
Use function rand() and number N-1 to get one random numbe r2. Then do step 5, 6 again.
Do it as step 4 to step 7 to get all 8 pair you want.
If you just want to get 16 unique characters, then just ignore step 2 but keep an array which has all possible characters.
If you want to some weird character, such as '$', '%', etc, then use ASC values.

Related

Find a fixed length path of adjacent 2D array elements

I have a m x m two-dimensional array and I want to randomly select a sequence of n elements. The elements have to be adjacent (not diagonally). What is a good approach here? I though about a depth-first search from a random starting point but that seemed a little bit overkill for such a simple problem.
If I get this right, you are looking for sequence like continuous numbers ?
When i simplyfy this:
9 4 3
0 7 2
5 6 1
So when the 1 is selected, you'd like to have path from 1 to 4 right ? I personally think that Depth-First search would be the best choice. It's not that hard, it's actually pretty simple. Imagine you select number 2. You'll remember position of number 2 and then you can look for lowest numbers until there are any. When you are done with this part, you just do the same for higher numbers.
You have two stacks one for possible ways and another one for final path.
When going through the array, you are just poping from possibilities and pushing right ones into the final stack.
The best approach would be finding the lowest possible number without saving anything and then just looking for higher numbers and storing them so at the end you'll get stack from the highest number to the lowest.
If I get that wrong and you mean just like selecting elements that are "touching" like (from my table) 9 0 7 6, which means that the content doesn't matter, then you can do it simple by picking one number, storing all possibilities (every element around it) and then pick random number from 0 to size of that stored values. When you select one, you remove it from these stored values but you keep them. Then you run this on the new element and you just add these new elements to stored elements so the random will always pick surrounding around these selected numbers.

32-bit x86 Assembly- Reversing Order of Array through Rotates/Shifts

I googled this question for the past 2 hours and couldn't find exactly what's needed so I figured I'd ask for help.
So..
We have 2 arrays. Both have a length of 10. The first one stores your student/teacher/work ID number with a prefix of 0. For example: 0,1,2,3,4,5,6,7,8,9
The goal of this program is to reverse the order by using shifts and rotates and store them in the second array.For example: 9,8,7,6,5,4,3,2,1,0
I can't seem to figure out the algorithm; I've been staring at the screen for too long.
The other requirements are to sum the 2 values from each array and show the memory locations, but I think that won't cause me much trouble.
Precise Constraints:
Draft a program that adds two BCD numbers (10-digits each)
The first BCD number is stored in array1 and the reversed order will be stored in array2.
Use shifts/rotates using array1 to fill array2
Display contents of the memory locations in question
Add both values by using BCD arithmetic
Store the sum in a variable named Result
Those were the requirements. No other information was given.
Thank you

accessing rows/columns from txt file table in C

I need your help or a little advice with my problem. For example, have a table looking like this:
blahbla 4 5 7 44
lololol 8 7 8 45
kokooko 1 2 3 4
These table has 3 lines and 4 columns, but the number of lines and columns may vary. I need to read values from this table (it is no problem with fopen) but the problem is that i dont know how to access concrete values from this table. For example if I want to printf values only from first line, or only from third column, what am I supposed to do? give me please some advice without using malloc, thanks.
Find matrix size
At first you should find the matrix size. Rows will be easy, you only have to count number of lines (I'm assuming that you are reading data from .txt file). Next thing is columns. If all rows will have the same number of elements, you can iterate over the string that is the first line and check if elements of the string are letters (for example with isalpha function). If element is a letter, then you can increment the number of columns - if not, then it will be space or number, if it is a space then you should increment number of columns variable, and if it is not then you have to skip to the next element.
If the number of elements in rows of your table may vary, then you should iterate over every line and find the number of columns like in previous case, finding the maximum.
Create matrix and copy data
Now, when you have size of your matrix you can allocate it like this: string table[numberOfRows][numberOfColumns] - then you should once again iterate over the data in txt file, checking if the element in line is alpha or is space. If it is a space and the next character is numeric(you can check it with isdigit and isspace), then you should make some string variable that you will be concatenating until it hits another space or end of line - then you assign it to matrix. If it is letter then it will be easy, just assign it to proper place in the matrix. Remeber that you will have to fill additional elements in the matrix with something like "0" (of course only if the rows can have different number of elements).
Retrieve data
You can retrieve data only from column or a row, simply create an array that has size of your table columns or rows, then you can for example iterate over your table and check if the row(column) is the one that is interesting you. And if it is, assign it to your array. Then you can print it with another loop.

Continuously Numbers Insert to Array Labview

I having this number generator from value 4 to 4.999 each time, I wan to make it into 2 index array such as each time the number generates the first number will store in the first index array, while the second number will store in the second index array, then the third number will store back into the first index array and repeat continuously. So lastly I able to ramp the higher value in the array index to the lower value of array index and display in numbers of samples.
I have a vi here: http://i.stack.imgur.com/902X7.png which I don't what was the correct placement. Anyone can provide me any advices?
Please let me know if you don't understand my question..
Regardless of whether you're placing data in two separate arrays or just interleaving the data, you could perform a modulo operation to swap the index or the array. Wire the Loop iteration terminal of your loop to the Quotient & Remainder function, and divide by two. Every time the remainder is zero, your index is an even number, every time it's 1, it's an odd number. You can use those results directly as the index if you're using a single, two dimensional array, or you can use it to select between two different arrays by either using a case structure, or by converting the result into a boolean value and using it as the input to a Select function.

How to use arithmetic operators to get cell location

I'm trying to perform math within an Excel formula to specify the end of an array. Here is a simple version of what I want to do:
A B C
1 =COUNTA(A1:A5)-1 =SUM(A1:A(1+B1))
2
3
4
5
The first column is my data array. The second column counts the number of entries in that array (so if it isn't full, it returns a value less than 5). The third column sums the array starting with the first value and ending with the last entered value. Obviously with the SUM function it doesn't matter if there are zeroes, but I am trying to use the MATCH function and I don't want it to return a zero just because there are blank entries in the array I'm looking up.
So I want to modify the information within the SUM function to produce a variable array length based on the return value of B1. I hope this is clear. Thanks for any help!
One way is to use INDEX like this
=SUM(A1:INDEX(A:A,B1+1))
you can use the range defined by
A1:INDEX(A:A,B1+1)
in other functions like MATCH
Like this?
=SUM(INDIRECT("A1:A"&(1+B1)))

Resources