I have this assignment due in 3 days. Trust me, I've been trying and trying to code it, but to no avail. So I apologize for not putting any code in here. There just isn't one in my book. Should I use arrays for this? Just to clarify that I have never actually worked with arrays before, but from my college classes, I remember some kind of correlation between arrays and rows/columns depending if they are one-dimensional or two-dimensional.
If anyone can help me out here, I would greatly appreciate it. This is the last assignment I have in terms of file handling in C++ that I haven't solved.
Create a program which will write numbers in a file Numbers.txt in this way:
Firstly the user enters a number of rows n to a file (number of rows has to be a natural number).
The file has to be consisted of the set number of rows and the rows cannot be empty.
Then, the numbers are written in a file in this way:
- Even numbers are written in even rows
- Odd numbers are written in odd rows
- In each row, we are looking at 10 numbers, e.g. the span of allowed numbers in the nth row is [10n + 1, 10n + 10], n = 0, 1,..., n
- In the first row is 1 - 10, in the second 11 - 20,...
While solving, it is required to use loops for generating numbers.
it really helps to see even your wrong code. Please paste it.
whoever wrote that was smokin' the good stuff though. Let me translate:
get n from the user.
write odd numbers to odd rows, 10 random numbers.
write even numbers to even rows, 10 random numbers.
note that in c++ for integers you can use bit logic to ensure even and odd.
x |= 1 makes x an odd number. if x is 100, it makes it 101 -- the idea is just to change the last bit which represents 1 in binary, all odd values have this set, all even have it cleared. If its already odd, nothing happens.
see if you can find a bit logic to set them even, using that idea. In binary, one way to do it, is and it with a 11111111111111111111111111110 type number, or in hex, FFFFF...FFFE but you need to match the bits/bytes to the sizeof() your integer to do it that way. I can't think of a clever way to do it for any sized int with only 1 operation right this second.
you don't need arrays.
you can just write directly to the file
for all the rows
file << randomnumber;
why don't you need arrays, then?
if you needed to keep all the values and use them again, for example if you made 2 3x3 matrix arrays and had to print the result of them multiplied together, you need to store all the values for both arrays (or get really, really funky in the code) because you need them to do the multiply and summations. You are not asked to reuse the values again, though, just to shovel them into the file (or say, onto the screen). If you just need to print a 10x10 array of random numbers on the screen, you can print them one by one and throw them away, right? Same thing to a file!