A text contains a set of natural numbers from 1 to 69. Five numbers are randomly
ID: 3722853 • Letter: A
Question
A text contains a set of natural numbers from 1 to 69. Five numbers are randomly selected. The numbers are in Ranum.txt file. They are set of 50 natural numbers. Study these numbers carefully. Ensure you understand the basis of difference between the numbers.
You are required to write a c++ program to open and read the numbers in the file. (10marks)
Your program should display the next set of 10 likely natural numbers in a new text file called Ranum1.txt based on the given 50 set of numbers and in the same format. It should start with a message: “The next set of 10 likely numbers are: ” (70marks)
Display of correct numbers (40marks)
Comments on your codes (30marks)
Ranum.txt file
28 - 36 - 41 - 51 - 58
3 - 9 - 16 - 56 - 60
1 - 3 - 13 - 15 - 44
1 - 20 - 61 - 64 - 69
9 - 35 - 37 - 50 - 63
2 - 24 - 28 - 51 - 58
25 - 36 - 37 - 55 - 60
19 - 20 - 50 - 55 - 62
28 - 30 - 32 - 36 - 58
24 - 26 - 28 - 59 - 63
8 - 13 - 27 - 53 - 54
35 - 37 - 46 - 51 - 61
17 - 28 - 31 - 32 - 39
23 - 32 - 44 - 48 - 50
4 - 6 - 16 - 30 - 56
12 - 14 - 20 - 21 - 34
12 - 14 - 26 - 48 - 51
3 - 6 - 19 - 26 - 44
27 - 35 - 38 - 57 - 66
18 - 22 - 29 - 54 - 57
14 - 41 - 42 - 45 - 69
30 - 49 - 54 - 66 - 69
32 - 37 - 56 - 66 - 69
1 - 3 - 13 - 19 - 69
10 - 49 - 61 - 63 - 65
22 - 23 - 62 - 63 - 66
8 - 12 - 25 - 41 - 64
8 - 10 - 21 - 23 - 25
24 - 45 - 55 - 56 - 57
39 - 48 - 53 - 67 - 68
17 - 18 - 24 - 25 - 31
17 - 24 - 35 - 57 - 63
6 - 20 - 29 - 57 - 59
8 - 14 - 32 - 58 - 67
6 - 21 - 41 - 52 - 62
19 - 28 - 43 - 67 - 69
7 - 15 - 32 - 38 - 66
6 - 7 - 16 - 23 - 26
17 - 19 - 39 - 43 - 68
9 - 15 - 43 - 60 - 64
20 - 24 - 26 - 35 - 49
12 - 30 - 36 - 47 - 62
11 - 21 - 28 - 33 - 45
1 - 16 - 54 - 63 - 69
1 - 28 - 40 - 45 - 48
7 - 19 - 21 - 42 - 69
5 - 32 - 44 - 53 - 60
50 - 51 - 59 - 61 - 63
9 - 40 - 63 - 64 - 66
1 - 2 - 18 - 23 - 61
28 - 36 - 41 - 51 - 58
3 - 9 - 16 - 56 - 60
1 - 3 - 13 - 15 - 44
1 - 20 - 61 - 64 - 69
9 - 35 - 37 - 50 - 63
2 - 24 - 28 - 51 - 58
25 - 36 - 37 - 55 - 60
19 - 20 - 50 - 55 - 62
28 - 30 - 32 - 36 - 58
24 - 26 - 28 - 59 - 63
8 - 13 - 27 - 53 - 54
35 - 37 - 46 - 51 - 61
17 - 28 - 31 - 32 - 39
23 - 32 - 44 - 48 - 50
4 - 6 - 16 - 30 - 56
12 - 14 - 20 - 21 - 34
12 - 14 - 26 - 48 - 51
3 - 6 - 19 - 26 - 44
27 - 35 - 38 - 57 - 66
18 - 22 - 29 - 54 - 57
14 - 41 - 42 - 45 - 69
30 - 49 - 54 - 66 - 69
32 - 37 - 56 - 66 - 69
1 - 3 - 13 - 19 - 69
10 - 49 - 61 - 63 - 65
22 - 23 - 62 - 63 - 66
8 - 12 - 25 - 41 - 64
8 - 10 - 21 - 23 - 25
24 - 45 - 55 - 56 - 57
39 - 48 - 53 - 67 - 68
17 - 18 - 24 - 25 - 31
17 - 24 - 35 - 57 - 63
6 - 20 - 29 - 57 - 59
8 - 14 - 32 - 58 - 67
6 - 21 - 41 - 52 - 62
19 - 28 - 43 - 67 - 69
7 - 15 - 32 - 38 - 66
6 - 7 - 16 - 23 - 26
17 - 19 - 39 - 43 - 68
9 - 15 - 43 - 60 - 64
20 - 24 - 26 - 35 - 49
12 - 30 - 36 - 47 - 62
11 - 21 - 28 - 33 - 45
1 - 16 - 54 - 63 - 69
1 - 28 - 40 - 45 - 48
7 - 19 - 21 - 42 - 69
5 - 32 - 44 - 53 - 60
50 - 51 - 59 - 61 - 63
9 - 40 - 63 - 64 - 66
1 - 2 - 18 - 23 - 61
Explanation / Answer
Here is the code. The pattern of five numbers are increasing order. See the comments in code. Create an input file "Ranum.txt" and keep it in the same directory with the code. The program will create an output file "Ranum1.txt" wich will contain 10 set of numbers
/***********************/
//============================================================================
// Name : FileOperation.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main() {
ifstream ifile; //input file object
ifile.open("Ranum.txt"); //open input file
int array[69]; //a local array to hold 69 values
char buf[100];
int i=0;
while(ifile.getline(buf,100)) //read 1 line till the end of file
{
char* ptr=strtok(buf," "); //tokenise the buffer in space and new line
while(ptr)
{
array[i]=atoi(ptr); //fill the array
ptr=strtok(NULL," ");
i++;
}
}
/*
for(int i=0;i<69;i++) //print the array
{
cout<<array[i]<<" ";
}
*/
ifile.close(); //close the input file
ofstream ofile("Ranum1.txt"); //create an output file
ofile<<"The next set of 10 likely numbers are ";
cout<<"The next set of 10 likely numbers are"<<endl;
srand(time(0)); //srand to initialise random
int rnum;
int counter;
int Rarray[5]; //create a local array
int fran;
for(int i=0;i<10;i++){
counter=0;
fran=rand()%69; //create the first random number
Rarray[0]=array[fran]; //initialise first array number
ofile<<Rarray[counter]<<" "; //write into file
while(counter<5){
rnum=rand()%69;
if((rnum>fran) || (rnum==68)){ //check next random number greater than previous one
counter++;
fran=rnum;
Rarray[counter]=array[fran]; //fill the array
ofile<<Rarray[counter]<<" "; //write into file
}
}
ofile<<" "; //write a new line after every five numbers
for(int i=0;i<5;i++)
{
cout<<Rarray[i]<<" "; //print the array
}
cout<<endl;
}
ofile.close();
return 0;
}
/***********************/input file "Ranum.txt
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 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69
/*****************/output file sample
The next set of 10 likely numbers are
38 66 68 69 69 69
34 65 66 69 69 69
59 63 69 69 69 69
40 54 59 61 68 69
35 54 58 62 67 69
22 40 53 63 68 69
7 46 50 63 69 69
11 59 63 64 67 68
2 20 69 69 69 69
31 32 55 62 65 67
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.