Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

behavior - how to fix undefined behaviour in c++

my team is required to design and develop a program using C++ language. Your C++ program should capable to perform the following tasks: Find the mean and standard deviation of up to 100 numbers. here is my coding

#include <iostream>  
#include <cmath>

using namespace std;
float findStandardDeviation(float data[], int count);
int min_value (float data[], int count);
int max_value (float data[], int count);
int occurrences_min (float data[],int min,int count);
int occurrences_max (float data[],int max,int count);
int main() 
{
    int i, count, sum;
    int noOfinput=0;
    float mean;
    float data[100];

    cout << "Enter number you want to calculate up to 100 numbers.
";
    cin >> count;
     
    cout << "Enter " << count << " number
";
    // Read "count" elements from user
    for(i = 0; i < count; i++) 
    {
        noOfinput++;
        cout<<"Input #"<<noOfinput<<" : ";
        cin >> data[i];
    }
     
    sum = 0;
    // Find sum of all array elements
    for(i = 0; i < count; i++) 
    {
        sum += data[i];
    }
 
    mean = (float)sum / count;
    cout << "Mean = " << mean<<endl;
    cout << "Standard Deviation = " << findStandardDeviation(data, count);
    cout << min_value (data,count);
    cout << max_value (data,count);
    
    return 0;
} 
 
  float findStandardDeviation(float data[], int count) 
  {
    float sum = 0.0, sDeviation = 0.0, mean;
    int i;
 
    for(i = 0; i < count; i++) 
    {
        sum += data[i];
    }
    // Calculating mean 
    mean = sum/count;
 
    for(i = 0; i < count; ++i) 
    {
        sDeviation += pow(data[i] - mean, 2);
    }
 
    return sqrt(sDeviation/count);
  }
int min_value(float data[], int count)
{
    int min = data[0];
    for(int i=0; i<count; i++)
    {
        if(data[i]< min)
        {
            min = data[i];
        }
    }
    cout<<"
Min value is "<<min<<endl;
    cout<<"Number of occurrences of min : "<<occurrences_min(data,min,count)<<endl;
}
int occurrences_min(float data[],int min,int count)
{
    int result = 0;
    for(int i=0; i<count; i++)
      if(min==data[i]) 
         result++;
    return result;
}
int max_value(float data[], int count)
{
    int max = data [0];
    for(int i = 0; i<count; i++)
    {
        if(data[i]>max)
        {
            max=data[i];
        }
    }
    cout<<"Max value is "<<max<<endl;
    cout<<"Number of occurrences of max :"<<occurrences_max(data,max,count)<<endl;
}
int occurrences_max(float data[], int max, int count)
{
    int result = 0;
    for(int i=0; i<count; i++)
      if(max==data[i])
         result++;
    return result;
}
question from:https://stackoverflow.com/questions/65862849/how-to-fix-undefined-behaviour-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...