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

C++ Beginner Question what is happening is I am importing a txt file with a bunc

ID: 3550749 • Letter: C

Question

C++ Beginner Question


what is happening is I am importing a txt file with a bunch of numbers. I need to find the number of prime, even , odd, and top ten.

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.

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.

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
// SORT FUNCTION TO GET TOP TEN.
void sort_top_ten(int array[],int count)
{
//start with the array and the value you want to insert test that value against each position of numbers.txt
for (int i = 1; i < count; i++)
{
int tmp = array[i];
int j;
//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
for (j = i; j > 0 && tmp > array[j-1]; j--)
array[j] = array[j-1];
array[j] = tmp;
}
}
int main()
{
ifstream infile("input.txt");
if(!infile)
{
cout <<"Unable to open file numbers.txt. so Exiting from program." << endl;
return 0;
}
//So, to start out with, if the array has been initialized to all zeros,
int array[100] = {0};
int count = 0;
while(!infile.eof())
{
infile >> array[count++];
}
//call sort function to get top ten.
sort_top_ten(array,count);
for(int i=0; i<10; i++)
cout << array[i] <<" ";
//system("pause");
return 0;
}