Vous êtes sur la page 1sur 3

Hello guys Im George and I have been working on a project 2 weeks now!

I tried to figure out exactly how to write the code, so I spent many hours to understand
which is the best way to write it. Dont panic you neednt to write the hole code for me- So
lets start with the basics:
Language the project written in : C
Knowledges you need to have : Pointers (critical), working with file commands (easy) and
Scanf library (to understand what I wrote in my code).
So the problem is the following:
I have to read a txt file that contain only numbers.
I use only the first 2 lines of the file! In the first line I have only one number that show us
how many numbers exists in the second line.
Eg
In.txt
9
9 285 5 6 198419 9 2 7 5
As you see in the above example in the second line I use any number I want. The first line
show us that in the second line exists 9 numbers. The second line just have as many
numbers as the first line says.
The first restriction I need to take is that exactly.
1) The file must contain only 2 lines of number
2) The first line must have only 1 number
3) The second line must have as many numbers as the first line says
If any of these restrictions dont applied then in each situation the program will return 0 and
will print me a warning message.
So now its time for my code! (that isnt very stable..)
1) For the first restriction I couldnt figure out how to solve it.
2) Neither for the second part.
3) But for the third restriction I wrote a code that reads the first number of the file and
then check the other numbers to see if there are as many as I want to, otherwise it
returns me an error.
----code here---
So the problem is exactly the 1
st
and 2
nd
restriction.
Lets say that in my file a have all the numbers in the first line (wrong!)
Or it could be every number in one line (wrong again!)
I think I could manage to give you a point of what I want.
And now the 2
nd
hard part!
Remember in the previous step that the file contain only one number in the first line?
Well we want to use that number to know how many numbers we will put in pointers. I
mean that all the numbers in the second line should be putted in one pointer! Difficult to
understand?
Well because I dont know every time how many pointers I need this is the use of the first
number. To tell us how many pointers we need to create.
*Why pointers? Well the second line can have 1.000.000 numebers! You can
understand the big size of the file!
This is that I dont know how to made! Not how to put the nums in a pointer but because
the numbers are changed , cause the file can changed, I cant write the code.
And the last part
We read two numbers. 9, 6. If 9>6 then we add it to one integer. Then we check the next
number with the number in the integer ( 9). If the number > the 9 then in the integer we
rewrite the int to the number is greater than the last. And so one

Lets say we have that nums.
90 100 90 9 14 3
First check 14 and 3.
14>3 so we write the num 14 to an int called a
Then check the number 9 with 14
9<14 we dont do anything. Just process to next number
Which is 90. And we check it with the int a (14)
90>14 so we write the num 90 to int a
And proceed
The next number will check the int a which have the value 90
100>90 so we write in int a the number 100
And so on.. It can be 10000 checks
But we want another piece of code that checks how many numbers is greater than previous.
If we write the int a 3 times [in that example we wrote 3 numbers ( 14, 90, 100) ] in the end
of the procedure we will save that int in a text file only!.
It could be 9 or 50 or even 10000 if every number is greater than the previous.

Here I post the code. You are free to test it on your own to understand how the scan work
--code

The problem now is that I dont know if I should use the same code for pointers, cause I
dont used pointers to read numbers.
Why all that problem?
I tough that I need pointers to run the last procedure of scanning faster. Do you think if we
had 100.000 numbers how many time it would need to make the procedure and save the
last int in a file? I want to make all that in !--------1 second-------!
It cant be so quickly but we want the quicler result possible!

Vous aimerez peut-être aussi