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.
Problem:
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
for(10 times)
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!
#include <Windows.h>
#include<string>
#include<fstream>
#include<iostream>
usingnamespace std;
int main(){
HANDLE fhnd = CreateFile(TEXT("Numbers.txt"), GENERIC_ALL,FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (fhnd == INVALID_HANDLE_VALUE) { cout << "failed to create file"; return 0; }
CloseHandle(fhnd);
fstream fs;
fs.open("Numbers.txt",fstream::out);
srand(time(0));
int n;
cin >> n;
for (int i = 0; i < n; i++) {
bool even = !(i % 2);
while (1) {
int r = rand() % ( ( i + 1 ) * 10) + 1;
if (r % 2 == even && r > i*10) {
fs << to_string(r) << '\n';
break;
}
}
}
fs.close();
}
edit made a few small changes. I think this is right.
For the record I took the problem as requiring one number between the range
Even on Even, odd on odd. No extra lines, no empty lines.
I guess it could be interpreted this way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (int i = 0; i < n; i++) {
bool even = !(i % 2);
for (int j = i*10; j < 10+(i*10); j++) {
if (j % 2 == even) {
fs << j << " ";
}
}
fs << '\n';
}
or with 10x random numbers...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (int i = 0; i < n; i++) {
bool even = !(i % 2);
int j = 0;
while (j < 10) {
int r = rand() % ((i+1)*10)+1;
if (r % 2 == even && r > i * 10) {
fs << r << " ";
j++;
}
}
fs << '\n';
}