This is an example of obtaining the mean and standard deviation at once, but when you proceed with the build, two errors appear. LNK2005 main is already defined. LNK1169 Multiple Defined Symbols.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#include <stdio.h> 
#include <math.h> 

#define NUMMAX 10 

void get_data();
void calc_avgstd(double* pavg, double* pstdev);

double num[NUMMAX]; 
int n;

void main()
{
    double avg, stdev;
    get_data();
    calc_avgstd(&avg, &stdev);

printf("\nMean = %6.2lf\n", avg); 
printf("standard deviation = %6.2lf\n\n", stdev); 
}

void get_data() {
    printf ("Please enter up to 10 real numbers."\n");
    printf("To exit input, enter a number of 0 or less.\n\n");
    n = 0;
    while (n < 10)
    {
        printf ("number %d: ", n + 1";
        scanf_s("%lf", &num[n]);
        if (num[n] > 0.0)

            n += 1;
        else
            break;
    }
}


void calc_avgstd(double* pavg, double* pstdev)
{
    double sum, ssum, mean, variance, std;
    int k;

    sum = ssum = 0.0;
    for (k = 0; k < n; k++)
    {
        sum += num[k];
        ssum += (num[k] * num[k]);
    }
    mean = sum / (double)n;
    variance = ssum / (double)n - mean * mean;
    std = sqrt(variance);

    *pavg = mean;
    *pstdev = std;
}

c

2022-09-20 15:07

1 Answers

One project must have only one main function.

In addition to the cpp file containing the code of the question, another cpp file is included in the project, and there will be a main function in it. Maybe it's a code that I wrote for another example or homework.

If you only leave a cpp file containing the code of the question, remove the existing file from the project and build it, the error will be resolved.


2022-09-20 15:07

If you have any answers or tips


© 2022 pinfo. All rights reserved.