Hi! I'm new here.

I was just wondering if there's someone who could help me in solving this problem.

Given a sequence of n numbers, compute the running average of the sequence. For example, given the sequence 1 3 5 7 9 will produce the output:
1 1
3 2
5 3
7 4
9 5

here are my questions

I know I should be using a while statement here but my problem is how I would implement the running average.


Thanks.

Edited 7 Years Ago by rico.tee: n/a

Think about how you would find the running average on a piece of paper, then apply it to code. Seeing as you're required to do this with while loops, I don't see much harm in showing you how to do it with for loops.

#include <stdio.h>

int main() {
  const int sequence[] = {1, 3, 5, 7, 9};
  const int numCount = sizeof(sequence) / sizeof(int);
  
  int i, j;
  int average;

  for (i = 0; i < numCount; ++i) {
    average = 0;

    // Add numbers from sequence up to i
    for (j = i; j >= 0; --j) {
      average += sequence[j];
    }

    // Divide by how many numbers we added up
    average /= (i+1);

    printf( "%d %d\n", sequence[i], average );
  }

  getchar();
  return 0;
}

It's up to you to understand the code, and change it to work with while loops instead.

Hope this helps.

Oh Thank you so much! I now have something to start solving it.

I have one more question though, is it possible to use a "while" statement instead of a "for"? I was trying to make a program for this with a "while" statement and I was having run-time errors.

Thank you so much again!


Think about how you would find the running average on a piece of paper, then apply it to code. Seeing as you're required to do this with while loops, I don't see much harm in showing you how to do it with for loops.

#include <stdio.h>

int main() {
  const int sequence[] = {1, 3, 5, 7, 9};
  const int numCount = sizeof(sequence) / sizeof(int);
  
  int i, j;
  int average;

  for (i = 0; i < numCount; ++i) {
    average = 0;

    // Add numbers from sequence up to i
    for (j = i; j >= 0; --j) {
      average += sequence[j];
    }

    // Divide by how many numbers we added up
    average /= (i+1);

    printf( "%d %d\n", sequence[i], average );
  }

  getchar();
  return 0;
}

It's up to you to understand the code, and change it to work with while loops instead.

Hope this helps.

I told you that was up to you. If i did that, you wouldn't have done anything. In fact, most members would probably think i've already given you too much code there.

I told you that was up to you. If i did that, you wouldn't have done anything. In fact, most members would probably think i've already given you too much code there.

YUP.

but to answer the OP's second question:

any loop can that can be done with a for statement, can also be done with a while statement. and vice versa. the details are different but the result will be the same.

Comments
answered my question clearly.

Oh I see. Thanks for answering my question. i under stand now.

YUP.

but to answer the OP's second question:

any loop can that can be done with a for statement, can also be done with a while statement. and vice versa. the details are different but the result will be the same.

any loop can that can be done with a for statement, can also be done with a while statement. and vice versa. the details are different but the result will be the same.

That reminds me of an old post. But I seem to have left out recursion. :P

(And it looked better in the original colors.)

This article has been dead for over six months. Start a new discussion instead.