A Little Automation for Physics Class LOL. 一点点物理课上的自动化(笑)

I am honestly too lazy to input hundreds of values into the calculator and calculate every their average & uncertainty, so here’s me automating this process.

实在是懒得去把数值一个一个输进计算器里然后计算平均值和偏差值了……所以写了一小段代码来自动化这个过程。

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main(){
    string val;
    vector<double> valVctr;
    cin.ignore(1);
    do{
        getline(cin, val);
        if(val.size() == 0){
            break;
        }
        valVctr.push_back(atof(val.c_str()));
    }while(true);

    cout << "A total of " << valVctr.size() << " values have been entered." << endl;

    double sum = 0;

    for(unsigned i = 0; i < valVctr.size(); i++){
        sum += valVctr.at(i);
    }

    double average = sum/valVctr.size();

    cout << "The average value is: " << average << endl;
    sum = 0;
    for(unsigned i = 0; i < valVctr.size(); i++){
        sum += pow(average-valVctr.at(i), 2);
    }
    cout << "The uncertainty is: \u00B1" << sqrt(sum/(valVctr.size()-1)) << endl;
}

Comments

  1. Jay
    3 years ago
    2021-6-08 11:33:49

    oh yes i love doing homework by not doing homework

  2. xiang-
    1 year ago
    2023-5-02 20:17:43

    酷,我目前也在大学里学c++

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next