Counting the number of times a number appears

Hi guys,

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!

1
2
3
4
count = 0
while input.read(number):
   if number == 42:
      count += 1
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)
Consider (not tried):

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
// 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
}

Last edited on
Hello cloudwolf,

When I read your OP this is the way I see it:

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.

Andy
Hello cloudwolf,

While working on your program I had a couple of thoughts.

When using tags I found this to be very helpful: http://www.cplusplus.com/articles/z13hAqkS/

When you said:

to see how many times two specific numbers appear in the file.


Do you mean that you will be providing the numbers to look for? Or do you mean to see what numbers are repeated in the file?

As a start I did get this far:

 33  19  59  84  76   4  96   8  57  52
 15  34  41  84  33   2  31  81  32  57
  1  12  94   1 100  17  45  99  99  65
 48  40  29  83  15  21   1  38  39  32
 65  33  17  57  96  35  16  64  33  77
 42  22  65  31  20  76  76  85  46  63
 55  72  41  52  61  73  41  63  69  64
 17  75   0  75  83  26  85  56   2  74
 44  73  94  79   4  96  68  84  28  26
 36  67  40   9  32   2  64  41   6  85


 Press Enter to continue:


It just prints to the screen for now, but can easily be modified.

Andy
I got it figured out guys. Part of the problem was that I had the files in the wrong place 😫

Thanks so much for the help guys! This forum has been a great source for me so far and I’m very grateful for it!
Topic archived. No new replies allowed.