Given N and Standard Deviation , find N elements | CPP program to find n elements ; function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void series ( int n , int d ) { if ( d == 0 ) { for ( int i = 0 ; i < n ; i ++ ) cout << "0 ▁ " ; cout << endl ; return ; } if ( n % 2 == 0 ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << pow ( -1 , i ) * d << " ▁ " ; } cout << endl ; } else { float m = n ; float r = ( m / ( m - 1 ) ) ; float g = ( float ) ( d * ( float ) sqrtf ( r ) ) ; cout << "0 ▁ " ; for ( int i = 1 ; i < n ; i ++ ) { cout << pow ( -1 , i ) * g << " ▁ " ; } cout << endl ; } } int main ( ) { int n = 3 , d = 3 ; series ( n , d ) ; return 0 ; }