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

This is my code but is not printing like it suppose too but I cannot use arrays

ID: 3637679 • Letter: T

Question

This is my code but is not printing like it suppose too but I cannot use arrays we have not cover them can I do it without using arrays? I also did it by using a pseudocode and please look at the program because I have questions

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main(void)

{
//Declaration of variables
char key = 'Y';
int index = 0;
int newValue = 0;
int smallest = 0;
int largest = 0;
int numberOfEvens = 0;
int numberOfOdds = 0;
int total = 0;
int adjacentValue = 0;

while ((key != 'N') && (key != 'n'))//it creates a loop to let the user to continue inputing integer numbers
{
cout << " Please Enter a Number: ";//this statement is executed while the condition is true and go inside of loop
cin >> newValue;

index++;
if (index == 1) //the if statement is made to go inside of the loop
{
// Initialize all variables
smallest = newValue;
largest = newValue;
numberOfEvens = ((newValue % 2) == 0) ? 1 : 0;// Checking for the even number. WHAT DOES HI MEANS WITH ? 1:0 WHAT DOES THIS DO?
numberOfOdds = ((newValue % 2) == 1) ? 1 : 0;//Checking for the odd number.
total = newValue;
adjacentValue = newValue;
}
else
{
if (newValue < smallest)//Checking for the smallest value.
{
smallest = newValue;
}
else if (newValue > largest)//Checking for the largest value.
{
largest = newValue;
}
else if ( newValue % 2 == 0)// Checking for the even number.
{
numberOfEvens++;
}
else if ( newValue % 2 == 1)//Checking for the odd number.
{
numberOfOdds++;
}

total = total + newValue;//Calculating the total


if ( newValue == adjacentValue )//Checking for adjacent
{
newValue = adjacentValue;
}
else
{
adjacentValue = newValue;
}

}

cout << "Do you want enter more values [Y/N]" << endl;
cin >> key;
}

cout << "Smallest: " << smallest << endl;
cout << "Largest: " << largest << endl;
cout << "Number of Evens: " << numberOfEvens << endl;
cout << "Number of Odds: " << numberOfOdds << endl;
cout << "Total: " << total << endl;
cout << "Adjacent Duplicate: " << newValue << endl;
cout << "Press any key to quit" << endl;
cin >> key;

return 0;
}


THE WAY IT SUPPOSE TO PRINT BUT IS NOT DOING THAT
Example:
–Input Number Sequence:
–Smallest: 1
–Largest: 90
–# of Evens: 6
–# of Odds: 5
–Total:
–Adjacent Duplicates: 4 78 9
1 5 8 90 4 4 78 78 3 9 9 (THIS IS THE SEQUENCE)

MY CODE ONLY COUNTS THE EVENS ONCE SO IT ONLY SHOW 4 EVENS WHEN THEY ARE 6 AND IT DOES NOT PRINT OUT THE ADJACENT DUPLICATES?

PLEASE HELP THANK YOU

Explanation / Answer

Note this part of your code else if (newValue > largest)//Checking for the largest value. { largest = newValue; } else if ( newValue % 2 == 0)// Checking for the even number. { numberOfEvens++; } Look what happens. If newValue is an even number but greater than largest, then largest = newValue as desired. However the fact that you included "else if" means that if the condition is meet, the other "else if"s will not be triggered. Solution: Erase the "else if" before the (newValue%2==0) that way the program will always check whether a number is even or not. (You should also do that for number of odds also)

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