File handling assignment I can't even start coding

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!
Last edited on
seems to be working and what you want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <Windows.h>
#include<string>
#include<fstream>
#include<iostream>

using namespace 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';
	}
Last edited on
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <random>
#include <fstream>
#include <limits>
#include <string>
#include <cctype>

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || !std::isspace(std::cin.peek()))) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	const size_t perRow {10};

	std::mt19937 rng(std::random_device {}());
	std::ofstream ofs("random.txt");

	if (!ofs)
		return (std::cout << "Cannot open file\n"), 1;

	const auto rows {getInt("Enter number of required rows: ")};

	for (int r = 0; r < rows; ++r) {
		for (int c = 0; c < perRow; ) {
			const auto n {rng()};

			if (((r + 1) & 1) == (n & 1)) {
				ofs << n << ' ';
				++c;
			}
		}
		ofs << '\n';
	}
}



Enter number of required rows: 5

3835594885 3667447547 2746201799 2888987179 3004818011 578122559 223418985 2583939925 2890414047 393265191
572595126 2665377712 1330884278 1973180714 2193607912 1404541766 123246062 2223440514 3380009010 2389821510
1265394161 3453443281 925225713 116051293 3606192049 791367077 4058730839 3826301179 4097473563 1700577417
420553504 4237523530 3011614788 1331671282 2863714548 950092630 1371707912 1357828778 2718512874 834984836
3933463693 752948367 874178185 2367409855 371627153 550207179 4036745017 2655019479 2388349331 3360158711

Last edited on
The numbers are not random. Nowhere in the OP is there anything that talks about random numbers.

So markyrock's second code fragment is what you want.
I read Jonnin's post which mentions random numbers......

OK, without the numbers being random, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <limits>
#include <cctype>

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || !std::isspace(std::cin.peek()))) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	const size_t perRow {10};

	std::ofstream ofs("numbers.txt");

	if (!ofs)
		return (std::cout << "Cannot open file\n"), 1;

	const auto rows {getInt("Enter number of required rows: ")};

	for (int i = 0; i < rows; ++i) {
		for (int j = i * perRow + (i + 1) % 2, l = perRow * (i + 1); j < l; j += 2)
			ofs << j << ' ';

		ofs << '\n';
	}
}



Enter number of required rows: 10


1 3 5 7 9
10 12 14 16 18
21 23 25 27 29
30 32 34 36 38
41 43 45 47 49
50 52 54 56 58
61 63 65 67 69
70 72 74 76 78
81 83 85 87 89
90 92 94 96 98


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
   ostream &out = cout;
// ofstream out( "output.txt" );

   int n;
   cout << "How many rows? ";   cin >> n;
   for ( int i = 1; i <= 10 * n; i += 2 )
   {
      out << setw( 3 ) << i + i / 10 % 2 << ' ';
      if ( i % 10 == 9 ) out << '\n';
   }
}
How many rows? 12
  1   3   5   7   9 
 12  14  16  18  20 
 21  23  25  27  29 
 32  34  36  38  40 
 41  43  45  47  49 
 52  54  56  58  60 
 61  63  65  67  69 
 72  74  76  78  80 
 81  83  85  87  89 
 92  94  96  98 100 
101 103 105 107 109 
112 114 116 118 120 

Last edited on
you are correct, I missed:
n = 0, 1,..., n
so yes, its that simple lol.
Topic archived. No new replies allowed.