Cropping a .ppm file in C - c

I'm working on a C program that crops .ppm files from a starting point pixel (x,y) (top left corner of cropped image) to an end point pixel (x+w,x+h)(bottom left corner of cropped image).
The data in .ppm files is of the following format:
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
Is there a simple way, wich avoids the use of 2 dimensional arrays, to do this using scanf()?

One easy way would be to simply keep track of your pixel coordinate as you read the file in. If you're currently in the crop rectangle, store the pixel; otherwise, skip it.
If you want to get more fancy: figure out the byte offset for the start of each row, seek to it, then read in the whole row.

Warning, some pnm files are in binary mode (they differ by magic number in the beginning of the file contents).
Maybe lookup the sources of pnmcrop would help?

Related

Join four columns into one according to each row

A B C D
E F G H
I J K L
M N O P
If I chose to join the columns I would ={A1:A;B1:B;C1:C;D1:D} but it would look like this:
A
E
I
M
B
F
J
N
... and so on
I would like it to look like this:
A
B
C
D
E
F
G
... and so on
How to proceed in this case?
Note: It may happen that some of the columns are not complete in data, some may have more values than the others, but I still want to continue following this same pattern. Example:
A B D
E G H
I J K L
M N O P
Result:
A
B
D
E
G
H
... and so on
use:
=TRANSPOSE(QUERY(TRANSPOSE(A:D),, 9^9))
then:
=TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(A:D),,9^9)),,9^9), " "))

The number of different minimal cover possible are?

Consider R(A,B,C,D,E,F,G) be a relational schema with the following functional dependencies:
AC->G, D->EG, BC->D, CG->BD, ACD->B, CE->AG. The number of different minimal cover possible are___________?
Actually in this question we were supposed to find all the possible minimal covers. I used this video
So using that theory i tried doing this question but end up getting only 2 minimal covers and then answer given in my text book is 4 .
The minimal covers I got are:
1)
D->E,D->G,BC->D,CG->D,AC->B(#),CE->A
2)
AC->G,D->E,D->G,BC->D,CG->D,CD->B(#),
CE->A
Actually the video gives only standard procedure to FIND a minimal cover. but the problem is a bit tricky as it asks about how MANY minimal covers we can find. I am applying the algorithm right...the only issue is that I am unable to find how many more minimal covers can be possible for the given set of FD's
A common algorithm to produce a minimal cover consists of three steps:
Split the right part, producing FDs with only one attribute on the right part.
For each left part with more than one attribute, try to remove each attribute in turn and see if the remaining can still determine the right part. In this case, eliminate the attribute from the left part.
For each remaining dependency, try to see if it can be eliminated.
In your case the first step produces:
F = { A C → G
A C D → B
B C → D
C G → B
C G → D
C E → A
C E → G
D → E
D → G }
In the second step, we must check the first seven dependencies. For each dependency A1A2...An -> B we try to eliminate in turn each Ai and see
if B is included in the closure of the remaining attributes (the closure taken with respect to F). In this case we have two possibilities: from ACD -> B we can eliminate either A or D. So we have now two different set of dependencies:
F1 = { A C → G
C D → B
B C → D
C G → B
C G → D
C E → A
C E → G
D → E
D → G }
and
F2 = { A C → G
A C → B
B C → D
C G → B
C G → D
C E → A
C E → G
D → E
D → G }
Now we can apply the last step: for each dependency X -> A we can see if A is included in the closure of X, X+ with respect to all the other dependencies. In this case, we can eliminate that dependency.
The result will depend, in general, from the order in which we apply those checks.
Here there are four different canonical covers:
G1 = { A C → G
B C → D
C G → B
C E → A
D → E
D → G }
G2 = { A C → G
B C → D
C G → D
C E → A
C D → B
D → E
D → G }
G3 = { A C → B
B C → D
C G → B
C E → A
D → E
D → G }
G4 = { A C → B
B C → D
C G → D
C E → A
D → E
D → G }
Note: it is not clear to me if there are other canonical covers.

How to get character like go down stairs in Google Sheets

I want to get a character from string but the path is like going downstairs in Google Sheets
The string is like:
y C I 6 8 V 5
~5 Z n I L w f
V ~s i w J d _
o R ~4 2 i v f
9 ^ j ~h r u #
Z y Q 7 ~1 u a
T t z u _ ~! Q
G Y n r * t ~^
J A l v F j d
a 2 l - y O B
h B w % n a 4
M t _ P D W a
And expect the output is:
5 s 4 h 1 ! ^
I put ~ in front of the character that demo the path I want to get.
The logic is I will find the first column which cell is 5 and then get start from that cell.
Example sheet link
https://docs.google.com/spreadsheets/d/1UQEGEl_rqAMFePDAGoueTI47xF8T_DyrvJ3de5pkLRA/edit#gid=306981885
I tried auto-fill but it could only incrementally either row or column. And I hope to incremental both row and column.
={INDIRECT("A"&MATCH(5, A1:A14, 0)),
INDIRECT("B"&MATCH(5, A1:A14, 0)+1),
INDIRECT("C"&MATCH(5, A1:A14, 0)+2),
INDIRECT("D"&MATCH(5, A1:A14, 0)+3),
INDIRECT("E"&MATCH(5, A1:A14, 0)+4),
INDIRECT("F"&MATCH(5, A1:A14, 0)+5),
INDIRECT("G"&MATCH(5, A1:A14, 0)+6)}
for a large scale scenario you can use this one and drag it to the right:
=INDIRECT(ADDRESS(MATCH(5, $A1:$A14, 0)+COLUMN()-1, COLUMN(), 4))
You can also do it with an array formula if you want to:
=ArrayFormula(hlookup(column(A2:G13),{column(A2:G13);A2:G13},match(5,A2:A13,0)+column(A2:G13)))

Batch insert heading/newline to ASCII file if value of column changes

I have a file similar to this:
A B C
D E C
F G C
A B X
F G X
A B Q
D E Q
Thats what I am looking for
> C
A B C
D E C
F G C
> X
A B X
F G X
> Q
A B Q
D E Q
So far I have a kind of complicated work-around.
Using AWK to add a empty line.
awk -v i=3 "NR>0 && $i!=p { print "A" }{ p=$i } 1" file.txt
I dont manage to add a ">" directly with awk since its a newline value. Instead of the "A", awk is outputting a empty line. Not really sure why..
Using then
sed -e "s/^$/>/" file.txt
I manage to insert a ">" to the empty line but the heading behind is still missing.
sed is for doing s/old/new, that is all. What you are attempting to do is not just s/old/new so you shouldn't be considering using sed, just use awk:
$ awk '$3!=p{print ">", $3; p=$3} 1' file
> C
A B C
D E C
F G C
> X
A B X
F G X
> Q
A B Q
D E Q
awk solution. Assuming that your input file is sorted:
awk '!a[$NF]++{ print ">",$NF }1' file
The output:
> C
A B C
D E C
F G C
> X
A B X
F G X
> Q
A B Q
D E Q
Could you please try following also and let me know if this helps you.
awk 'NR==1{print ">",$3 RS $0;prev=$3;next} prev!=$3{print ">",$3};1; {prev=$3}' Input_file
Output will be as follows.
> C
A B C
D E C
F G C
> X
A B X
F G X
> Q
A B Q
D E Q

naming array from an array in GAWK

I have a file with repeating elements. I would like to assign records to an array until the file repeats, at which point I want to create a new array to assign the records to. I would like to do this an arbitrary amount of times.
for example.
$ cat repeat.txt
a
b
c
d
e
f
g
a
b
c
d
e
f
g
a
b
c
d
e
f
g
I want the output to be something like this
0 a a a
1 b b b
2 c c c
3 d d d
4 e e e
5 f f f
6 g g g
right now I am doing this with this hideous code.
awk 'BEGIN{n=0;z=0}
$1~"a" {n=0;z++}
z==1{a[n]=$0}
z==2{b[n]=$0}
z==3{c[n]=$0}
z==4{d[n]=$0}
z==5{e[n]=$0}
z==6{f[n]=$0}
{n++}
END{for (i in a)
print i,a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],k[i],j[i]}'
repeat.txt
I would like the assignment of new arrays to be automatic.
I attempted this by the following
echo "abcdefghijklmopqrstuvwxyz" > alphabet.txt
awk 'BEGIN{N=0}
NR==FNR{FS=""}
NR==FNR{for (zz=0;zz<=NF;zz++) a[zz]=$zz; next}
NR!=FNR{FS="\t"}
NR!=FNR{if ($0~a) N++; (a[N])[N]=$0}
END{for (I in (a[N])) print I,(a[N])[I]}' alphabet.txt repeat.txt
but this didn't work because you can't do multidimensional arrays like this in gawk. I can't think of another way to do this.

Resources