1.4converting a C Program to C++

C Program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIDES 6
#define R_SIDE (rand() % SIDES + 1) 
int main(void)
{
    int n_dice = 2;
    int d1, d2, trials, j;
    int* outcomes =calloc (sizeof(int), n_dice * SIDES +1); 
    srand(clock());
    printf ("\nEnter number of trials: ");
    scanf("%d", &trials);
    for (j = 0; j < trials; ++j)
        outcomes[(d1 = R_SIDE) +( d2 = R_SIDE)]++;
    printf( "probability\n");
    for (j = 2; j < n_dice * SIDES + 1; ++j)
        printf( "j = %d p = %lf\n" , j , (double)(outcomes[j])/trials);
} 

C++ Improvements

-NB some of these are adopted in modern C
-Different libraries most old C libraries still
-available – usually as c....
-Inline - replaces code macro – benefit?
-Rest of line comments

C++ Program

#include <iostream> //drops .h still available
#include <cstdlib>
#include <ctime>
using namespace std;
const int sides = 6; //replaces many sharp defines
inline int r_sides(){ return (rand() % sides + 1); } 
int main(void)
{
    const int n_dice = 2;
    int d1, d2;
    srand(clock()); //why?
    cout << "\nEnter number of trials: ";
    int trials;
    cin >> trials; //compare to scanf
    int* outcomes = new int[n_dice * sides +1]; TYPE* s; 
     for (int j = 0; j < trials; ++j)
        outcomes[(d1 = r_sides()) +( d2 = r_sides())]++;
    cout << "probability\n";
    for (int j = 2; j < n_dice * sides + 1; ++j)
        cout << "j = " << j << " p = "<< 
        static_cast<double>(outcomes[j])/trials<< endl;
} 

C Program

#include <stdio.h>
#include <math.h>
double fcn(double x)
{ return x * x - 1; }
double fcn2(double x)
{ return x * x * x * x * x - exp( x ) -2; }
double root_find(
    double f(double), /* function with root */
    double a, double b, /* interval (a, b) with root */
    double eps, /* accuracy */
    double *root /* root --output*/
)
{
    if ( b - a < eps){
        *root = (b + a)/2.0;
    return f(*root);
}
else if ( f(a)* f( (b + a)/2) <= 0)
    return root_find(f, a, (b+a)/2, eps, root);
else
    return root_find(f, (b+a)/2, b, eps, root);
}
int main(void)
{
    double x;
    printf("Root Finder\n");
    root_find(fcn, 0.0, 4.0, 0.00001, &x);
    printf("root is at %g with residual %g \n", x, fcn(x));
    root_find(fcn2, 0.0, 14.0, 0.00001, &x);
    printf("root is at %g\n", x);
    return 0;
} 

C++ Program:

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


inline double fcn(double x)
{
        return x * x -1;
}

inline double fcn2(double x)
{
    return x * x * x * x * x - exp(x) - 2;
}

double root_find(
    double f(double),/*function with root*/
    double a, double b,/*interval (a, b) with root*/
    double eps,/*accuracy*/
    double *root
    )
{
    if(b - a < eps){
        *root = (b + a)/2.0;
        return f(*root);
    }
    else if(f(a)*f((b + a)/2 <= 0))
        return root_find(f, a, (b + a)/2, eps, root);
    else
        return root_find(f, (b + a)/2, b, eps, root);
}

int main()
{
    double x;

    cout << "Root Finder" << endl;
    root_find(fcn, 0.0, 4.0, 0.000001, &x);
    cout << "root is at " << x << "with residual " << fcn(x) << endl;
    root_find(fcn2, 0.0, 14.0, 0.000001, &x);
    cout << "root is at " << x << endl;
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • “他肯定已經(jīng)明白他失去了原來(lái)那個(gè)溫暖的世界,已經(jīng)為懷有一個(gè)夢(mèng)想太久而付出高昂的代價(jià)。他肯定抬起過(guò)頭,透過(guò)那些可怕的...
    下雪天閱讀 612評(píng)論 1 6
  • 今天無(wú)事,看了很久都沒(méi)看的恐怖片,果然還是泰國(guó)的恐怖片拍的好。不像美國(guó)那么血腥,不像韓國(guó)那么滲人,也不像日本的那么...
    殘章斷簡(jiǎn)閱讀 227評(píng)論 0 2
  • 在日常生活的點(diǎn)滴中,總有那么一個(gè)瞬間,很精彩,讓我們心動(dòng)…… 有了這份感悟,把“瞬間”放在中心位置,開(kāi)始思維導(dǎo)圖。...
    云兒飄是我閱讀 1,288評(píng)論 0 2
  • 我的小男神終于睡著啦~ 隔著黑夜我也能感覺(jué)到他又長(zhǎng)又卷的睫毛,他的爸爸媽媽可都沒(méi)這個(gè)基因,忽閃忽閃著圓圓的眼睛,拉...
    將心向月閱讀 256評(píng)論 0 0
  • 濤哥一家人
    a8ee5ba56e4c閱讀 230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容