File in C, read some integers and put in array - c

In C, if i want to read a list of integers, but the problem is this.
I must read the first two number and this two number are a dimension for my array.
Now, my doubt is, i read this two number, but my file will point to next int?
Example: (there are 3 array of integres in this file)
3 4
11 -1 1 -12
0 -2 12 2
-8 4 4 7
2 3
8 -8 1
6 -3 -3
3 2
1 1
3 4
-1 8
first array is:
DIMENSION [3][4]
11 -1 1 -12
0 -2 12 2
-8 4 4 7

After you open a file to read, each call to read moves the cursor, meaning after you have read the first line which contains the dimensions, your cursor will be at the start of the secind line.
To read the the arrays after getting the dimensions, I would use malloc to create the two dimensional array for the values.
After each block of array you can call free and malloc or realloc to read the next one.

Related

what is minimum number of operations required to sort an array if:-

In each operation we can either push an element to the end of the array or at the beginning of it
for example an array 3 2 5 1 4 6 would take 4 steps.
after first operation 2 3 5 1 4 6
after second operation 1 2 3 5 4 6
after third operation 2 3 1 4 6 5
after fourth operation 1 2 3 4 5 6
I think in the best case, the array is already sorted - 0 operations needed.
In the worst case, its sorted already, but in the opposite order (eg 6 5 4 3 2 1), you gonna need number of elements-1 operations.

reading a 3 dimensional array from txt file

I am having a problem storing a 3 dimensional array into a proper format.
The text file reads as follows:
4 3 5 4 3 2
3 5 4 3 2 3
1 0 4 3 2 4
2 3
1 5
1 6
3 5
the integers are standard integers and need to be arranged as they appear. So the upper part of the txt file will be stored into a 3x3 matrix with each cell containing the two integer individual values. the bottom values should be stored in a 1x4 matrix with each cell containing the 2 individual integer values.

EXCEL VBA: Removing rows from an array and adding those rows to another array

I am working in EXCEL VBA.
I have a 2 dimensional array (for this example, let's say its a 5 x 5 one-based array). This is my raw data (Array "A"):
6 7 7 8 5
9 9 9 9 7
1 3 6 9 3
7 3 2 9 9
4 9 6 5 2
I also have a separate array whose row space mirrors that of the first (e.g., a 5 x 3 one-based array). The 1st column of this array is the row number of the raw data (A). This is my meta data (Array "B"):
1 0 0
2 1 0
3 0 0
4 0 0
5 1 0
For every occurrence of "1" in the 2nd column of the meta data array (B), I need to remove the corresponding row from my raw data array (A) AND add that row to a third array (Array "C")(which will not contain any data at the beginning of this process). Therefore, in this example, I need to remove rows 2 & 5 from Array A and place them in Array C.
I also need to copy the 1st column of the Array B (the original row numbers of Array A) to both arrays A & C so that after some further processing I can re-combine the results and return the data to its original order.
I'm not sure how best to go about this. Any suggestions?
Thanks!

Algorithm for Vertex connections From List of Directed Edges

The square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that u→w is in E2 if and only if u ≠ w and there is a vertex v such that both u→v and v→w are in E2. The input file simply lists the edges in arbitrary order as ordered pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices.
*self-loops and duplicate/parallel edges are not allowed
If we look at the an example of input data:
1 6
1 4
1 3
2 4
2 8
2 6
2 5
3 5
3 2
3 6
4 7
4 5
4 6
4 8
5 1
5 8
5 7
6 3
6 4
7 5
7 4
7 6
8 1
Then the output would be:
1: 3 4 7 8 5 2 6
2: 5 6 3 4 1 8 7
3: 1 7 8 6 5 4
4: 5 6 8 7 3 1
5: 3 1 4 6
6: 2 7 5 8
7: 1 5 6 8 3 4
8: 6 4 3
I'm writing the code in C.
My thoughts are to run through the file, see how many vertices they are and then allocate an array of pointers. Proceed to go through the list again searching for just where the line has a 1 in it, then look at where those corresponding numbers lead. If its not a duplicate or the same number(1) then I'll add it to a linked list, from the array of pointers. I will do this for every number vertex number in the file.
However, I feel this is terribly inefficient, and not the best way to go about doing this. If anyone has any other suggestions I would be extremely grateful.
if I get it right, you want to build a result set for each node where all nodes with a distance of one and two for each node are stated.
therefore, one can hold the edges in an adjacency matrix of bit arrays, where a bit is one when an edge exists and zero if not.
now one can multiply this matrix with itself. in this case multiply means you can make an AND on row and column.
A small example (sorry, don't know how to insert a matrix properly):
0 1 0 0 1 0 0 0 1
0 0 1 x 0 0 1 = 1 1 0
1 1 0 1 1 0 0 1 1
This matrix contains a one for all nodes reachable in two steps. simply it's the adjacency matrix for two instead of one steps. If you now OR this matrix with your initial matrix you have a matrix which holds all paths of length one and two.
this approach has multiple advantages. at first bit operations are very fast. the cpu parallyzes your calculations and you can stop for the result matrix cell if one pair is found where the results gives one.
furthermore it is well documented how to calculate matrix multiplication in parallel.
you can easily calculate all other length of pathes. for a length k one has to calculate:
A^k = A^(k-1) * A
hope that helped

Brute-force sudoku solver: backtracking?

An implementation of a brute-force algorithm to solve Sudoku puzzles fails if a cell is discovered in which placing any of the digits 1-9 would be an illegal move.
The implementation is written in C, with the board represented by a 9x9 array. The solver counts down from 9 until a legal number's reached, and if none can be reached, it outputs a zero in its place.
A zero also represents a cell to be filled in. Here's the output (truncated) if a string of zeros (an empty board) is the input:
9 8 7 6 5 4 3 2 1
6 5 4 9 8 7 0 0 0
Those last three zeros are there because the values filled in previously aren't changing. How can I stop the solver from failing like this?
If you would currently put a zero in a spot, instead go back to the previous spot you put a number in and continue to count down till you find another value number for that spot.
For instance, in your example:
9 8 7 6 5 4 3 2 1
6 5 4 9 8 7 0 0 0
Instead of putting the zero in below the three, you would instead go back and try putting a 6 in below the 4.
don't treat every "move" like the right move. E.g. placing the last 7 seemed ok but makes it so that in the next cell no valid moves are left. So upon hitting the "no move possible" situation, go back, and try the next option. Iterate and you will have your solution.
A better way of course would be to start brute forcing for places with a small set of options left; run through all cells and start brute forcing with the cell with the least number of options left. When starting out with all-zero, you would then end up with
9 8 7 6 5 4 3 2 1
6 5 4 0 0 0 0 0 0
3 2 1 0 0 0 0 0 0
which is legal, without backtracking once.
You can do this by pushing your guesses onto a stack. Every time you end up wanting to output a zero, instead pop your last answer off the board and continue counting from it.
So if you guess 3 in (2,3) and next you're looking at (3,3) and get to zero, go back to (2,3) and try 2, then 1, then pop to before your (2,3) guess, etc.

Resources