Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:02:50+00:00 2026-06-04T06:02:50+00:00

For some reason I was getting a dramatically incorrect answer for a problem, so

  • 0

For some reason I was getting a dramatically incorrect answer for a problem, so I put in this printf to try and debug.

for (s = 0; s <= 100; s++) {
    for (t = 0; t <= 100; t++) {
        printf("At (%f,%f), spl = %f\n", s, t, spl(loc_data, s, t)); */
            if (spl(loc_data, s, t) > 80) { 
                     p++;
            }
        }  
}

If I omit the printf line, I get an incorrect answer. I think that it has something to do with memory allocation or uninitialised variables, but this is beyond my abilities. Any help would be appreciated.

Whole code:

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

typedef struct {
double x;
double y;
double W;
} data_t;

double spl(data_t *, double, double);

int main(int argc, char **argv) {

data_t loc_data[1000];

double spl0, p = 0, pp;

int i = 0, j = 0;

double s, t;

while (scanf("%lf %lf %lf", &loc_data[i].x, &loc_data[i].y, &loc_data[i].W) == 3) {
    i++;
}

printf("\nStage 1\n=======\n");
printf("Number of sound sources: %d\n", i);
for (j = 0; j < i; j++) {
    printf("%.1f meters east, %.1f meters north, power %1.5f Watts\n", 
    loc_data[j].x, loc_data[j].y, loc_data[j].W);
}






printf("\nStage 2\n=======\n");
spl0 = spl(loc_data, 0, 0);
printf("SPL at (0.0,0.0): %.1f dB\n", spl0);



printf("\nStage 3\n=======\n");


for (s = 0; s <= 100; s++) {
    for (t = 0; t <= 100; t++) {
        printf("At (%f,%f), spl = %f\n", s, t, spl(loc_data, s, t)); 
        if (spl(loc_data, s, t) > 80) { 
            p++;
        }
    }
}
pp = p / 102.01;

printf("Points sampled: 10201\nAbove 80.0 dB: %.1f%%\n", pp);

return 0;
}

double spl(data_t *loc_data, double pointx, double pointy) {
int i = 0;
double r_sq, powi, spli, spl;
while (loc_data[i].W != 0) {
    r_sq = pow(loc_data[i].x - pointx,2) + pow(loc_data[i].y - pointy,2);
    powi = 10*log10(loc_data[i].W / pow(10,-12));
    spli = powi + 10*log10((2 / (4 * M_PI * r_sq)) + (4 / (2.5 * M_PI * r_sq)));
    spl = 10*log10(pow(10, spl/10) + pow(10, spli/10));
    i++;
}
return spl;
}

Apologies for the poor formatting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:02:51+00:00Added an answer on June 4, 2026 at 6:02 am

    You”re using a variable (spl) before it’s initialized inthe function spl():

    double spl(data_t *loc_data, double pointx, double pointy) {
    int i = 0;
    double r_sq, powi, spli, spl;
    while (loc_data[i].W != 0) {
        r_sq = pow(loc_data[i].x - pointx,2) + pow(loc_data[i].y - pointy,2);
        powi = 10*log10(loc_data[i].W / pow(10,-12));
        spli = powi + 10*log10((2 / (4 * M_PI * r_sq)) + (4 / (2.5 * M_PI * r_sq)));
        spl = 10*log10(pow(10, spl/10) + pow(10, spli/10));
        //                     ^^^
        i++;
    }
    return spl;
    }
    

    Calling printf() is probably influencing the value the variable happens to have.

    Also, you read in a number of entries into the loc_data array, but don’t pass that information to the spl() function. In spl() you treat the array entry with field W as the ‘sentinel’ – the end of the array. Is it a given that the last entry in the input data will have a zero value? If so, you should probably let us know, and probably check for that when done reading the input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting undefined for some reason when I try to return the html via
I have a problem with getting my pager-function to work. For some reason it
I'm not sure what's changed, but for some reason I'm getting a problem with
Here's my code: x=0; for i=1:100 x=x+sqrt(((1/((((2*i)-2)^2)*(((2*i)+2)^2)))*16)+8); end For some reason I keep getting
For some reason I am getting an error this error: mysql_fetch_array() expects parameter 1
For some reason I'm getting a module object is not callable error on this
This is the json object: {Packet:{Response:Handshake,Code:lmhn,UID:4e1d137caaed7}} For some reason I am getting a syntax
For some reason i keep getting the following error when i try to set
k this is probably something very easy but for some reason i am getting
For some reason I'm getting an error: statement with no effect on this statement.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.