Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Beginner Question txt file with a bunch of numbers. I need to find the numbe

ID: 3550783 • Letter: C

Question


                    C++ Beginner Question                 

                

                    
                

                                     txt file with a bunch of numbers. I need to find the number of prime, even , odd, and top ten. and must be called from a function
                    
                    not sure how to go about doing this. I have figured out how to get numbers that were prime, even, and odd but not to comfortable with array or swap commands to                     find the top ten largest numbers.


end number of file is -1
                    
                    Heres the requirements
                    
                    start with the array and the value you want to insert
                    test that value against each position of numbers.txt
                    if it is greater than the value at a certain position
                    swap the value being tested with the value currently at that position
                    continue testing against every position, but with the value that was swapped out
                    
                    So, to start out with, if the array has been initialized to all zeros, and you are given the value 5. You will test 5 against the first element (index 0), see                     that it is greater, and swap the values. Now you will test the value you swapped out (0) against the rest of the indices. Since it will never be greater than                     any of them, that value is left out of the array going forward.


here is what i have so far

1 #include<iostream>
  2 #include<fstream>
  3 #include<cmath>
  4
  5 using namespace std;
  6

  7 int a = 0;
  8 int array[10]={0};
  9 int num = 0;
10 int j = 0;
11 /* Functions that determine if numbers are  prime, odd, even, and
12    gives the top 10 largest values.
13 */
14
15  
16 //Prime
21   }
22   for(num=3; num <= mysqrt; num += 2) {
23     if (pn%num==0) return(false);
24     }
25     return(true);
26   
27 }
28 bool isOdd (int on) {
29   return(on % 2 != 0);
30 }
31 bool isEven (int en) {
32   return(en % 2 == 0);
33   }
36   that include the prime numbers, evens , and odds.
37 */
38 int main() {
39 //Opens file input.txt  
40   ifstream inFile;
41   inFile.open("input.txt");
42 //Initialize Variables  
43   int totalsize = 0;
44   int count = -1;
45   double en = 0;
46   int on = -1;
47   int pn = -1;
48   int num = 0;
49 //Creates and opens primes.txt, evens.txt, and odds.txt
50   ofstream primes;
51   primes.open("primes.txt");
52   ofstream evens;
53   evens.open("evens.txt");
54   ofstream odds;
55   odds.open("odds.txt");
56
57   while (totalsize !=-1) {
58     inFile>>totalsize;
59     count ++;
60     if (isEven (totalsize)) {
61       en++;
62       evens<<totalsize<<endl;
63     }
64     if (isOdd (totalsize)){
65       on++;
66       odds<<totalsize<<endl;
67     }  
68     if (isPrime(totalsize)) {
69       pn++;
70       primes<<totalsize<<endl;
71     }
72   }

Explanation / Answer

We will create a second array and store all the numbers from input.txt in the created array. When we have the numbers we can sort the array in descending order.So,the top ten will give the largest 10 numbers.I have created a function largestten() for this purpose.I have highlighted the important changes.


#include<iostream>
#include<fstream>
#include<cmath>

using namespace std;

int a = 0;
int array[10]={0},tmp[100]={0},cnt=0;//cnt for new array indexing
int num = 0;
int j = 0;
/* Functions that determine if numbers are prime, odd, even, and
gives the top 10 largest values.
*/


//Prime
}
for(num=3; num <= mysqrt; num += 2) {
if (pn%num==0) return(false);
}
return(true);

}
bool isOdd (int on) {
return(on % 2 != 0);
}
bool isEven (int en) {
return(en % 2 == 0);
}

void largestten()

{

for(i=0;i<cnt;i++){
for(j=i;j<cnt;j++){
if(tmp[i] < tmp[j]){
temp=tmp[i];
tmp[i]=tmp[j];
tmp[j]=temp;
}
}
}
cout<<"Ten largest numbers are : ";
for(i=0;i<10;i++){
printf(" %d",tmp[i]);
}

}
that include the prime numbers, evens , and odds.
*/
int main() {
//Opens file input.txt
ifstream inFile;
inFile.open("input.txt");
//Initialize Variables
int totalsize = 0;
int count = -1;
double en = 0;
int on = -1;
int pn = -1;
int num = 0;
//Creates and opens primes.txt, evens.txt, and odds.txt
ofstream primes;
primes.open("primes.txt");
ofstream evens;
evens.open("evens.txt");
ofstream odds;
odds.open("odds.txt");

while (totalsize !=-1) {
inFile>>totalsize;
count ++;

tmp[cnt++]=totalsize;
if (isEven (totalsize)) {
en++;
evens<<totalsize<<endl;
}
if (isOdd (totalsize)){
on++;
odds<<totalsize<<endl;
}
if (isPrime(totalsize)) {
pn++;
primes<<totalsize<<endl;
}

largestten();
}