C ++奇数平均值

时间:2016-11-21 06:02:49

标签: c++

ODDAVG - 编写程序以接受键盘上的整数,并找到10个奇数的平均值。如果输入了一个偶数,请忽略计算中的数字,然后打印消息“只请奇数。”我想要这个程序的帮助我尝试了一个多小时?

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

void Pause()    
{ // function to freeze the screen, waiting for a `keypress`

     string junk;
     cout << "\n\n Press enter to continue...";
      cin.ignore();
     getline (cin,junk);
  // void functions do not return a value
}



int main()

{
     int even=0;
     int odd=0;
     int number=0;
     int eventotal = 0; 
     int oddtotal = 0;
     int counter = 0;
     bool done;
     int i;

   // get `inpot`
      cout << "Please enter 10 odd integers: " << endl << endl;
      cin >> number;
      cout << endl;      


   // display results

      if ((number%2 == 0) && (number > 0))
         {
         cout << "odd numbers only please." << endl;
         }

      else if ((number > 0) && (i++))
        {  
            odd++;
            oddtotal = oddtotal +number;

        }

     while (number!=0)(!done);
          counter ==10;
          counter++;


    int oddavg ;// to store the average of odd numbers  
        if(oddtotal!=0)
          {

              oddavg = oddtotal/ odd;   
          }
          cout << " The average is: " << oddavg << endl; 


 // freeze screen
     Pause();
     return (0);
}

2 个答案:

答案 0 :(得分:2)

不要只是复制代码,尝试理解它。即使您只是想通过课程,“快捷方式”也无济于事。你将独自进入决赛,并在长期的职业发展中落后。是的,即使你的专业不是计算机科学(根据这里的经验)。

一些有用的资源:

C++ functions

C++ loops

C++ recursion

最后一个递归,基本上是指使用自身的功能,是您问题的关键。

验证数字是奇数的函数需要不断调用自身,以便模拟你想要的“暂停”。对于您的目的,实际停顿是现实。

话虽如此,请在下面分析您的问题的工作代码,它完全符合您的要求:

#include <iostream>
#include <string>

using namespace std;

const int NUMBER_COUNT = 10;   // pre-defined numbers to input
int checkOdd(int input, int count);

int main()
{

int oddNums[10];
double avg, sum = 0;

// Instructions
cout << "Enter 10 numbers, Odd Numbers Only: "<<endl;

// Input numbers
for (int count = 0; count < NUMBER_COUNT; count ++)
{
    cout << "Odd Number #"<<count+1<<": ";
    int input;
    cin >> input;
    oddNums[count] = checkOdd(input, count+1);
}

for (int count = 0; count < NUMBER_COUNT; count++)
{
    sum += oddNums[count];
}

// Average
avg = sum / NUMBER_COUNT;

// output average
cout << "Average of Odd numbers: "<< avg <<endl;

return 0;
}  


int checkOdd(int input, int count) {

    if(input % 2== 0) {
        int newAnswer;
        cout << input<<" is not an odd number, try again!!"<<endl<<"Odd Number #"<<count<<": ";  
        cin >> newAnswer;
        return checkOdd(newAnswer,count);
    }
    else{
        return input;
    }
}

干杯,upvote&amp;如果有帮助,请选择答案。

答案 1 :(得分:0)

老实说,一小时对于编程任务来说并不算什么。有时你会完成它并花一个小时来找到一个简单的bug。

至于回答你的问题,我真的相信你拥有的代码比现在的代码更难。

这是我对一个简单问题的简单描述。

#include <iostream>
#include <string>
using namespace std;

int main(){
    double sum_odd = 0;
    int input = 0;
    int count = 0;
    double ans = 0;

    //loops until 10 odd numbers have been entered
    while (count < 10){
        cout << "Input 10 odd numbers: ";
        cin >> input; //assuming user is putting in integers
        while(input % 2 == 0){
            cout << "You have input an even number, please enter an odd number";
            cin >> input;
        }
        sum_odd += input;
            count++;
    }

    //find the average of the 10 numbers
    ans = sum_odd / 10.0;
    cout << ans;
    return 0;
}