• 0

[C++] newbie help


Question

Hey there!

I am new on this forum and I have started with c++ some weeks ago, although I've had previous programming experiance.

I am trying to make a console application that gives out the average of the users prefered numbers, which I have so far found a way. But the current problem/issue I am having is that I dont know what code I need or how I should modify my code to enable its user to decide how manny numbers he wishes to find the average of, right now I can only choose between 1 and 2. In other words, how do I add 3 and 4?

here is the code, sorry im a noob =)

/*

A simple program to help finding the average of numbers

*/

#include <iostream>

#include <cmath>

using namespace std;

double dnumber1 = 0.0;

double dnumber2 = 0.0;

double daverage = 0.0;

int main()

{

cout << "Average finder v.1.0 - LimePatch" << endl << endl;

int x;

cout << "Please enter how manny numbers you wish to find the average of" << endl << endl;

cin >> x;

cout << " " << endl;

if (x <= 1)

{

cout << "You need more than one number to find the average" << endl << endl;

}

else if (x <= 2)

{

cout << "Okay, please list your " << x << " numbers" << endl << endl;

cin >> dnumber1;

cin >> dnumber2;

cout << " " << endl;

daverage = (dnumber1 + dnumber2) / 2;

cout << "The average of your number is " << daverage << endl;

}

system("pause");

return 0;

}

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

if (x &lt;= 1)
{
cout &lt;&lt; "You need more than one number to find the average" &lt;&lt; endl &lt;&lt; endl;
}

else if (x &lt;= 2)
{
cout &lt;&lt; "Okay, please list your " &lt;&lt; x &lt;&lt; " numbers" &lt;&lt; endl &lt;&lt; endl;
cin &gt;&gt; dnumber1;
cin &gt;&gt; dnumber2;
cout &lt;&lt; " " &lt;&lt; endl;
daverage = (dnumber1 + dnumber2) / 2;
cout &lt;&lt; "The average of your number is " &lt;&lt; daverage &lt;&lt; endl;
}
}

You want to change this bit to use a for loop. for loops will allow you to do things a given number of times.

Average = total sum / number of elements. So, you'll need to keep a running total as well.

int dtotal = 0;
for (int i = 0; i &lt; x; ++i) {
    cin &gt;&gt; dnumber;
    dtotal += dnumber;
}
daverage = dtotal / x;
cout &lt;&lt; "The average of your number is " &lt;&lt; daverage &lt;&lt; endl;

Link to comment
Share on other sites

  • 0

if (x &lt;= 1)
{
cout &lt;&lt; "You need more than one number to find the average" &lt;&lt; endl &lt;&lt; endl;
}

else if (x &lt;= 2)
{
cout &lt;&lt; "Okay, please list your " &lt;&lt; x &lt;&lt; " numbers" &lt;&lt; endl &lt;&lt; endl;
cin &gt;&gt; dnumber1;
cin &gt;&gt; dnumber2;
cout &lt;&lt; " " &lt;&lt; endl;
daverage = (dnumber1 + dnumber2) / 2;
cout &lt;&lt; "The average of your number is " &lt;&lt; daverage &lt;&lt; endl;
}
}

You want to change this bit to use a for loop. for loops will allow you to do things a given number of times.

Average = total sum / number of elements. So, you'll need to keep a running total as well.

int dtotal = 0;
for (int i = 0; i &lt; x; ++i) {
    cin &gt;&gt; dnumber;
    dtotal += dnumber;
}
daverage = dtotal / x;
cout &lt;&lt; "The average of your number is " &lt;&lt; daverage &lt;&lt; endl;

thank you that was very helpful :=)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.