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: 3550799 • 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 but get error of   segmentaion fault (core dumped)

  1 #include<iostream>
  2 #include<fstream>
  3 #include<cmath>
  4
  5 using namespace std;
  6
  7 long long array[10]={0},tmp[]={0},cnt=0;
  8 int num = 0;
  9 int j = 0;
10 long long temp = 0;
11 int i;
12 /* Functions that determine if numbers are  prime, odd, even, and
13    gives the top 10 largest values.
14 */
15 void addToLargest(){
16   for(i=0;i<cnt;i++){
17   for(j=i;j<cnt;j++){
18     if(tmp[i] < tmp[j]){
19     temp = tmp[i];
20     tmp[i]=tmp[j];
21     tmp[j]=temp;
22   }
23   }
24   }
25
26   cout<<"Ten largest numbers: "<<endl;
27   for (i = 0; i < 10; i++){
28    cout<<tmp[i]<<tmp[i++]<<endl;
29   }
30 }
31 //Prime
36   }
37   for(num=3; num <= mysqrt; num += 2) {
38     if (pn%num==0) return(false);
39     }
40     return(true);
41
42 }
43 bool isOdd (int on) {
44   return(on % 2 != 0);
45 }
46 bool isEven (int en) {
47   return(en % 2 == 0);
48   }
51   that include the prime numbers, evens , and odds.
52 */
53 int main() {
54 //Opens file input.txt  
55   ifstream inFile;
56   inFile.open("input.txt");
57 //Initialize Variables  
58   int totalsize = 0;
59   int count = -1;
60   double en = 0;
61   int on = -1;
62   int pn = -1;
63   int num = 0;
64 //Creates and opens primes.txt, evens.txt, and odds.txt
65   ofstream primes;
66   primes.open("primes.txt");
67   ofstream evens;
68   evens.open("evens.txt");
69   ofstream odds;
70   odds.open("odds.txt");
71
72   while (totalsize !=-1) {
73     inFile>>totalsize;
74     count ++;
75     tmp[cnt++]=totalsize;
76     if (isEven (totalsize)) {
77       en++;
78       evens<<totalsize<<endl;
79     }
80     if (isOdd (totalsize)){
81       on++;
82       odds<<totalsize<<endl;
83     }
84     if (isPrime(totalsize)) {
85       pn++;
86       primes<<totalsize<<endl;
87     }
88
89   }
90   addToLargest();
91
92   inFile.close();
93   inFile.close();
94   cout<<"Numbers read in: "<<count<<endl;
95   cout<<"Even Numbers: "<<en<<endl;
96   cout<<"Odds Numbers: "<<on<<endl;
97   cout<<"Prime Numbers: "<<pn<<endl;
98
99   }



have been giving two incorrect ways so far, hopefully third times a charm

Explanation / Answer

#include<iostream>

#include<fstream>

#include<cmath>


using namespace std;


int a = 0;

int array[10]={0};

int num = 0;

int j = 0;

/* Functions that determine if numbers are prime, odd, even, and

gives the top 10 largest values.

*/



//Prime

bool isPrime(int pn)

{

int mysqrt;

mysqrt= (int)sqrt(pn);

if (pn>1)

{

for(num=2; num <= mysqrt; num += 1)

{

if (pn%num==0) return(false);

}

return(true);

}

else

return(false);

}

bool isOdd (int on) {

return(on % 2 != 0);

}

bool isEven (int en) {

return(en % 2 == 0);

}

void tenmaxint(int num)

{

int temp;

for(int i=0;i<10;i++)

{

if(array[i]<num) //if any element in array is less than number read from file swap them

{

temp= array[i]; //store array[i] in a temp

array[i]=num; // overwrite array[i] with num

num=temp; // copy temp to num . this swaps the two numbers

}

}

}



//that include the prime numbers, evens , and odds.

void 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");

ofstream max;

max.open("max.txt");


while (totalsize !=-1)

{

inFile>>totalsize;

count ++;

if (isEven (totalsize)) {

en++;

evens<<totalsize<<endl;

}

if (isOdd (totalsize)){

on++;

odds<<totalsize<<endl;

}

if (isPrime(totalsize))

{

pn++;

primes<<totalsize<<endl;

}

tenmaxint(totalsize);

}

for(int i=0;i<10;i++)

max<<array[i]<<' ';

}