Pretty New To C++ [Solved]

Posts 1–8 of 8 · Page 1 of 1
Pretty New To C++ [Solved]
so i have a project: Programming Project #2
Positive Odd Integers
Write a C++ program that can read in a set of positive integers, then it will calculate and display the
average value of all the entered odd integers (i.e. the even integers will not be counted).
The program will then display the largest and smallest integers that were selected among the entered
odd integers.
Allow your program continues to run until the user wanted to quit.
Required output:
Number of odd integers entered:
Average value:
Largest odd integer:
Smallest odd integer:

/*
*/

#include <iostream>
using namespace std;

int main ()
{
int Integercounter;
int largest;
int smallest;
int total;
int integer;
double average;

total = 0;
Integercounter = 0;

cout << "enter a positive integer or -1 to quit: ";
cin >> integer;
while ( integer != -1)
{
total = total + integer;
Integercounter = Integercounter + 1;
cout << "enter a positive integer or -1 to quit ";
cin >> integer;
}
if (Integercounter !=0)
{

average = static_cast< double >( total ) / Integercounter;

cout << "\nNumber of " <<Integercounter << "odd integers entered: "
<< total << endl;

cout << "average value: " << average << endl;
}
else
cout << "no integers were entered" << endl;
return 0;
}


so my current issue is how do i get the program to add all integers and continue to run the program if the user wishes to continue using it
[highlight=c++]void main()
{

int curNumber =0;
int smallest =1;
int largest = 1;
int total = 0;
int entered=0;
int average=0;

cout << "Enter a postive number to calculate average. Enter -1 to Exit: ";
cin >> curNumber;
while( curNumber!=-1)
{
if(curNumber % 2 != 0) //If current number is not completely divisible by zero, then it's an odd number. % operator is used to get the remainder of two numbers.
{
total+=curNumber; //Add the current number to total variable.
entered+=1; //Keep track of total odd numbers.
if (curNumber > largest )//If current number is larger than the last largest number;
largest=curNumber;//Assign the current number to the largest variable.
if (curNumber < smallest)//If current number is smaller than the last smaller number;
smallest = curNumber;//Assign the current number to the smallest variable.
average=total/entered; //Compute the average by dividing the sum of all odd numbers over total odd numbers entered.
}
//Since we are in a loop, ask for a positive number again.
cout << "Enter a postive number to calculate average. Enter -1 to Exit: ";
//Assign the new number to curNumber.
cin >> curNumber;
}
//Display the info...
cout << "You entered " << entered << " positive odd integers." << endl;
cout << "Smallest odd integer was " << smallest << endl << "Largest odd integer was " << largest << endl;
cout << "Average of entered odd integers: " << average;
cout << endl << "Press Enter to exit...";
_getch();
}[/highlight]
^ your homework is done sir.
^ Icwutudiddere ;P
hi thank you very much for your help i really appreciate it.. is it okay if i add you on aim perhaps you could help me in the distant future?

oh btw when i run the program and enter -1 it adds it as an odd integer and it doesnt allow the user to repeat the program if he wishes to
Quote Originally Posted by yair View Post
hi thank you very much for your help i really appreciate it.. is it okay if i add you on aim perhaps you could help me in the distant future?

oh btw when i run the program and enter -1 it adds it as an odd integer and it doesnt allow the user to repeat the program if he wishes to
Sure and NP.

+ I thought you wanted to end the program when user enters -1.

Anyways, I am sure you know how to accomplish that.

/Marked Solved.
ummm whats your aim account...if you could just message the name
Quote Originally Posted by yair View Post
ummm whats your aim account...if you could just message the name
I have windows live account (MSN). PM'd you.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags

None

Talk with us