C++ Beginner Question txt file with a bunch of numbers. I need to find the numbe
ID: 3550798 • 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 }
have been giving two incorrect ways so far, hopefully third times a charm
Explanation / Answer
// I have also corrected your isPrime function
//tenmaxint is the new function along with swapp
// in case of doubt please comment
#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 swapp(int *a, int *b)// swaps the two numbers
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void tenmaxint(int num)
{
for(int i=0;i<10;i++)
{
if(array[i]<num) //if any element in array is less than number read from file
swapp(&array[i],&num);//swap them. remember we need to pass by reference
}
}
//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]<<' ';
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.