網頁

2016年10月21日 星期五

Ex7 C++寫入csv檔

請用C++亂數~N(70,10)產生100個隨機亂數

寫進csv檔

20 則留言:

  1. #include<fstream>
    #include <iostream>
    #include<cstdlib>
    #include<random>
    #include<ctime>
    #include<cmath>
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution<double> x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  2. #include<iostream>
    #include<cstdlib>
    #include<random>
    #include<ctime>
    #include<fstream>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> x(70, 10);
    int a[1000];
    double s = 0;
    for (int i = 0; i < 100; i++){
    a[i] = x(gen);
    cout << a[i] << "\t";
    fout << a[i]<<",\n";
    s = a[i]+s;
    if (i % 10 == 9)cout << endl;
    }
    fout.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  3. #include&lt;fstream&gt;
    #include &lt;iostream&gt;
    #include&lt;cstdlib&gt;
    #include&lt;random&gt;
    #include&lt;ctime&gt;
    #include&lt;cmath&gt;
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution&lt;double&gt; x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i &lt; 100; i++){
    a[i] = (int)(round(x(gen)));
    fout &lt;&lt; a[i] &lt;&lt; ",";
    fout &lt;&lt; "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  4. #include&lt;fstream&gt;
    #include &lt;iostream&gt;
    #include&lt;cstdlib&gt;
    #include&lt;random&gt;
    #include&lt;ctime&gt;
    #include&lt;cmath&gt;
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution&lt;double&gt; x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i &lt; 100; i++){
    a[i] = (int)(round(x(gen)));
    fout &lt;&lt; a[i] &lt;&lt; ",";
    fout &lt;&lt; "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  5. #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    fout << "hello, 1,2,3\n";
    fout << "hello, 2,2,4\n";
    fout.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  6. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv",ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;

    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);

    fout << (clock()-seed)/1000.0 <<"sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  7. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;
    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);
    fout << (clock() - seed) / 1000.0 << "sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  8. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;
    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);
    fout << (clock() - seed) / 1000.0 << "sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  9. B10233169 陳振萱2016年10月21日 下午5:19

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    #include <cmath>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    cout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)cout << endl;
    }
    sum = sum / 1000.0;
    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);
    cout << (clock() - seed) / 1000.0 << "sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  10. #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<random>
    #include<ctime>
    using namespace std;
    int main(){
    fstream fout;
    fout.open("test.csv", ios::out | ios::trunc);
    int test = clock();
    mt19937_64 gen(test);
    normal_distribution<double>dis(70, 10);
    int a[100];
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(dis(gen)));
    fout << a[i] << "\n";
    }
    fout.close();
    fout << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  11. #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  12. #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  13. #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv",ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;

    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);

    fout << (clock()-seed)/1000.0 <<"sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  14. #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv",ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;

    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);

    fout << (clock()-seed)/1000.0 <<"sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  15. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <random>
    #include <ctime>
    #include <cmath>
    using namespace std;

    int main(){
    fstream fout;
    fout.open("test.csv",::ios::out|ios::trunc);
    int seed=clock();
    mt19937_64 gen(seed);
    normal_distribution<double>dis(70,10);
    double a[100];
    double sum=0;
    for(int i=0;i<100;i++){
    a[i]=(int)(round(dis(gen)));
    fout<< a[i];
    sum+=a[i];
    if(i%10==9)fout <<endl;
    fout.close();
    }
    system("pause");
    return 0;
    }

    回覆刪除
  16. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv",ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;

    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);

    fout << (clock()-seed)/1000.0 <<"sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  17. B10233082 唐偉強2016年12月16日 下午5:21

    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  18. #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <random>
    #include <ctime>
    using namespace std;
    int main()
    {
    fstream fout;
    fout.open("test.csv",ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);
    normal_distribution<double> X(70, 10);
    double a[1000];
    double sum = 0;
    double sum2 = 0;
    for (int i = 0; i < 1000; i++){
    a[i] = X(gen);
    fout << a[i] << "\t";
    sum += a[i];

    if (i % 10 == 9)fout << endl;
    }
    sum = sum / 1000.0;

    for (int i = 0; i < 1000; i++){
    sum2 += (a[i] * a[i]) - (sum * sum);
    }
    sum2 = sum2 / 1000;
    double s = sqrt(sum2);

    fout << (clock()-seed)/1000.0 <<"sec." << endl;
    system("Pause");
    return 0;
    }

    回覆刪除
  19. #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除
  20. #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main(){
    fstream fout;
    fout.open("text.csv", ios::out | ios::trunc);
    int seed = clock();
    mt19937_64 gen(seed);

    normal_distribution x(70, 10);
    int a[100];
    double sum = 0;
    double s = 0;
    double sum2;
    for (int i = 0; i < 100; i++){
    a[i] = (int)(round(x(gen)));
    fout << a[i] << ",";
    fout << "\n";
    }
    fout.close();
    system("Pause");
    return (0);
    }

    回覆刪除

HTML 編輯器