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 6920367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:08:15+00:00 2026-05-27T10:08:15+00:00

Ok so I get this code to do the averaging : (written in C

  • 0

Ok so I get this code to do the averaging : (written in C )

.
.
        int sum[3];
        int j;
        int avg;
      for(;;) //infinite loop
      {
       for(j=0;j<3;j++){
        i = ReadSensor(); // function that keeps saving sensor values as int i
        sum[j]=i;
        }
       avg=sum[0]+sum[1]+sum[2]+sum[3]; 
       printf("Sonar: %d \r \n", avg >> 2);
      }
.
.

Is this correct ? im shifting by 2 to divide by avg / 2^(2) which is 4
The problem is im expecting a value of about 15, however I get about 8–9 .. Im not sure why this is happening ?

Basically the sensor’s readings fluctuate between 15-17, I want to get an average instead of printing noise values. Is my code correct ? Then why do I get wrong outputs !?

  • 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-05-27T10:08:16+00:00Added an answer on May 27, 2026 at 10:08 am

    The number of values read from sensor is required twice. First, to control the number of iterations of for loop. Second, as the divisor of sum. Introduce a variable (say, N) to capture that.

    Also, the division by shifting does not sound right, because that restricts the number of readings from the sensor to power of two.

    enum { N = 4 };
    
    sum = 0;
    for( j = 0; j < N; j++) {
       i = ReadSensor(); // function that keeps saving sensor values as int i
       sum += i;
    }
    avg = sum / N; 
    printf( "Sonar average: %d\n", avg );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm unable to get this code to work. Any idea how to change that?
In this code I need to get the DOM object that is .tabs the
I am trying to ultimately get this code to work: function Timer(callback, delay) {
I am trying to get this code to show only fields that have values,
I have this code & I've tired using JQuery's appendTo() function to get this
I am trying to get this code to make a square that will follow
If I run this Code: function sendRequest() { var req = new XMLHttpRequest(); req.open(GET,
I am trying to get this code working -- var loader; $(function() { loader
I've been trying to get this code to work for hours! All I need
hI, I'm trying to get this code from Larry Nyhoff's book to compile in

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.