I'm in a basic C++ programming class and I'm working on a homework assignment but it's asking me to do something that I'm not sure we have covered yet. It has me read numbers from a text file, print them out in a certain format (a 10x10 grid) and now wants me to print those numbers to a separate file. Part of that asks me to write a code that reads all of the numbers in the first text file (one random number was printed to each line for 100 lines) to see how many times two specific numbers appear in the file.
I'm not asking for the complete answer, just if someone could point me in the right direction. We have covered loops and are now in a chapter that covers files but we have not covered arrays or anything like that. I apologize if my question is too vague for an answer. Thanks in advance!
I'm in a basic C++ programming class and I'm working on a homework assignment but it's asking me to do something that I'm not sure we have covered yet.
It has me
1. read numbers from a text file,
2. print them out in a certain format (a 10x10 grid) and now wants me to
3. print those numbers to a separate file.
Part of that asks me to write a code that
4.reads all of the numbers in the first text file
(one random number was printed to each line for 100 lines)
to
5. see how many times two specific numbers appear in the file.
I'm not asking for the complete answer, just if someone could point me in the right direction. We have covered loops and are now in a chapter that covers files but we have not covered arrays or anything like that. I apologize if my question is too vague for an answer. Thanks in advance!
An additional way to tackle this is to analyse the question. It's up to you how you do that but the way I show gives you 5 steps you can concentrate on and tackle one at a time - it's called a 'divide and conquer' approach and you can combine that with loops and your newly found knowledge of opening, reading, writing and closing files, possibly generating a hundred random numbers unless that's given to you. (edit: which on reflection it is by the sound of it)
// open a file for input
ifstream ifs("file name");
// test if file opened ok
if (!ifs.is_open()) {
std::cout << "Cannot open input file\n";
return 1;
}
// open file for output
ofstream ofs("output name");
// test if output file opened ok
if (!ofs.is_open()) {
std::cout << "Cannot open output file\n";
return 2;
}
// Define 100 element array
int array[100] {};
// Read numbers into array
for (int cnt = 0; cnt < 100 && (ifs >> array[cnt]); ++cnt);
// Print array as 10x10
// Also print to output file
for (int a = 0; a < 100; ++a) {
cout << array[a] << ((a % 10) ? ' ' : '\n');
ofs << array[a] << ' ';
// Add code here to count how many times specific numbers appear
}
I'm in a basic C++ programming class and I'm working on a homework assignment but it's asking me to do something that
I'm not sure we have covered yet.
It has me read numbers from a text file, print them out in a certain format (a 10x10 grid)
and now wants me to print those numbers to a separate file.
1. Part of that asks me to write a code that reads all of the numbers in the first text file (one random number was printed to
each line for 100 lines) to see how many times two specific numbers appear in the file.
1a. reads all of the numbers in the first text file.
1a1. open input file.
1a2. check that is is open.
1a3. read 1 number to start with.
1a4. Print it out.
1b1. When it prints 1 number to the screen add a for loop to check multiple reads.
1b2. Add code to get it to print 10 numbers per line.
I'm not asking for the complete answer, just if someone could point me in the right direction.
We have covered loops and are now in a chapter that covers files but we have not covered arrays or anything like that.
I apologize if my question is too vague for an answer. Thanks in advance!
DO NOT try to write the whole program at 1 time. I do not believe you are ready for that yet. Work in small steps, write some code, compile and test it to see what it is doing. First you have to read the file to have something to work with. Then post your code for any additional suggestions others may have.
It is very helpful if you post the input file, so everyone can see what you are working with and use the same information.
First get the file open and reading followed by printing out what you have read. This code can easily be modified later.