Read input txt files and format it in c [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a input file that has different content and length each line.
Every line represents a command.
in
cr foo //create file "foo"
op foo //open it with index 1
wr 1 x 60 //write x in index 1("foo") 60 times
wr 1 y 10 //write y in index 1("foo") 60 times
sk 1 55 //such and such...
rd 1 10
dr
sv disk0.txt
in disk0.txt
op foo
rd 1 3
cr foo
cl 1
dr
I have looked at this answer, but it still not helpful for my question.
How to read specifically formatted data from a file?
I really need help of how to read this input file correctly in order to perform different commands.
The command may contain integers, or it main only have one character, I don't know how to deal with this text! Please help

Use fgets() to read till end of line.
Please make a point that fgets() comes with a new line char.
Break the line to tokens using strtok() with space as delimiter
Use the tokens accordingly.
Use strtol() to deal with integers

Related

Solution to make all array elements equal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
Can you advise the solution for this..
ARRAY EQUALITY
Problem Statement
Amy has an array A, of N integers. She wants to make all the elements of the array equal.
On each day she can select a subarray of A, with length exactly M and perform following operation: ยท
Pick any element of the selected subarray and increase or decrease it by 1. She can perform this operation any number of times she wants (possibly 0), on that day.
Find the minimum number of days required to make all the elements of the array A equal. NOTE: A subarray is the sequence of consecutive elements of the array. You are given T independent test cases.
Constraints
`
All input values are integers.
Input Format First-line contains T.
First line of each test case consists of two space separated integers N and M.
Second line of each test case consists of N space separated integers denoting the array A. Output Format Print in a newline for each test case single integer denoting the minimum number of days required to make all the elements of the array A equal.
Sample Input 1
1
5 3
1 2 2 3 1
Sample Output 1
1
I want to know the aproach to solve this problem?

C or Matlab row, column, array problem question [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
enter image description here
k=15;
for i=1:k:length(aa) % aa = 100
for j=1:k:length(bb) % bb = 200
result(j,i) = function(a, b, c, d)
end
end
I am trying to save the calculated value by extracting k by 15 steps.
If you do it this way, the next value will come out from the 1st row, 1st column, 16th row, 1st column, as shown in the picture above, and the value of 0 will come out in between.
So, I'm trying to write additional code that pulls them out in sequence like the right part of the picture...
for ii = size_aa:-1:1
if result(ii,:) == 0
result(ii,:) = [];
end
end
I tried the method of removing zero elements, but it takes too much time. Is there any other way?
Once you have your result matrix, you can use result(result == 0) = []; to remove zero elements and end up with a vector of non-zero results. Here's an example derived from the code the OP posted (I replaced function(a,b,c,d) with rand() for the sake of the example):
k=15;
for i=1:k:100
for j=1:k:200
result(j,i) = rand();
end
end
result(result == 0) = [];
Alternatively, the solution proposed by Cris Luengo generates a matrix that doesn't contain zero elements to start with; I plugged his idea in my sample code:
k=15;
for i=1:k:100
for j=1:k:200
result((j-1)/k+1, (i-1)/k+1) = rand();
end
end

How can we transform written number to number in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
For example the input is three hundred and twenty six and the output is 326.
I have already tried to write all word of specific numbers like one,two,hundred,... in an array and the same for the integers . So they will have the same index like
word[0] is "one" and integer[0] is 1
But after that nothing works .
I really recommend starting with constructing a grammar. Here is an idea on how it can be done:
T :== t | td | d | x
H :== dh | dh&T
d - digit
x - ten, eleven or twelve
t - twenty - ninety
h - hundred
& - and
T - 1-99
H - 1-999
Note that this may be slightly incorrect because English is not my native language. But it shows how to construct a grammar.
So 326 could then be constructed by H -> dh&T -> dh&td
An expanded grammar to include thousands could look like this:
A :== Ha | HaH
a - thousand
312108 could be constructed with A -> HaH -> dh&Tadh&T -> dh&xadh&d

Find the regular expressions of the following?Language of odd length and cannot contain length multiple of 3 over ๐šบ={๐’‚,๐’ƒ} [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Language of odd length and cannot contain length multiple of 3 over
๐šบ={๐’‚,๐’ƒ}
OK to have an odd length that's not a multiple of 3, we can have a "head" part that generates strings of length 6n, and then a "tail" part that generates strings of length 1 and 5 (but not 7). Any odd number that's not a multiple of 3 can be written as 6n + 1 or 6n + 5 for n >= 0. So, let's get crackin.
r = ((a + b)^6)*((a + b) + (a + b)^5)
I have used a shorthand notation here which you can omit; basically, s ^ n stands for the regular expression s repeated n times. As long as n is a definite number this can be written out, just makes the regular expression a little longer.

how to convert from cells array in matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 2 cells functions (resulted from certain code) as follows:
p =
'GO:0008150'
'GO:0016740'
'GO:0016787'
'GO:0008150'
'GO:0016740'
'GO:0016740'
'GO:0016787'
'GO:0016787'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0008150'
'GO:0008150'
'GO:0006810'
'GO:0016192'
'GO:0006810'
'GO:0005215'
c =
'GO:0016740'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0016779'
'GO:0004386'
'GO:0003774'
'GO:0016298'
'GO:0016192'
'GO:0005215'
'GO:0030533'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0003774'
'GO:0005215'
'GO:0030533'
I have a code that it works fine with single values (numericals or characters), but it doesn't work with the above cellsarrays, and the following error message appears:
??? Undefined function or method 'eq' for input arguments of type 'cell'.
If there any idea about how to convert from cells function to any kind which can accept the following code:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if a(1,:)==level1_root
level=a(2);
level=[level a(2)]
else
end
end
In your code, a is a cell array of two strings, while level1_root is a cell array with one string. You can not compare cell arrays of strings using ==. You should rather to a strcmp. For example, something like this:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if strcmp(a{1}, level1_root{:})
level=a(2);
level=[level a(2)]
else
end
end
However, I am not sure if the above code will work for you as it is. It assumes that level1_root has 1 element only. That is the case in the example arrays, but may not be in general. You will have to change the code to suit your needs.

Resources