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

Use C++ and Visual Studio Write a program in a cpp file called EvenNumber_LastNa

ID: 3753916 • Letter: U

Question

Use C++ and Visual Studio

Write a program in a cpp file called EvenNumber_LastNameFirstName.cpp that read in only even number. The program should use an int array to store 3 even number. If the number is not even, the number should not be stored in the array. Once the array contains 3 even numbers, sort the numbers and output the numbers to screen and to a text file called even_LastNameFirstName.txt Please enter a number: 23 23 is not an even number Please enter a number: 12 lease enter a number :34 5 is not an even number Please enter a number6 Here are the 3 even numbers: 6 12 34

Explanation / Answer

#include <iostream>
using namespace std;

int main () {

int arr[ 3 ]; // arr is an array of 3 integers
int tmp;

// initialize elements of array arr to 0
for ( int i = 0; i < 3; i++ ) {
LOOP:
cout <<"Please Enter a Number ";
cin >> tmp;
if( tmp % 2 == 0){
arr[i] = tmp;
}else{
cout<<tmp<<" is not a Even Number"<<endl;
goto LOOP;
}
}

for (int i = 0; i < 3; ++i) {
for ( int j = i + 1; j < 3; ++j) {
if (arr[i] > arr[j]) {

int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}

// open a file in write mode.
ofstream outfile;
outfile.open("even_LastNameFirstName.txt");

cout<<"Here are 3 Even Number : ";
// output each array element's value
for ( int j = 0; j < 3; j++ ) {
cout<< arr[j]<<" ";
outfile <<arr[j]<<endl;
}
// close the opened file.
outfile.close();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote