0

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 by rico.tee: n/a

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Dave Sinkula
Featured Replies
  • 1

    [QUOTE=William Hemsworth;1068503]I told you that was up to you. If i did that, you wouldn't have done [b]anything[/b]. In fact, most members would probably think i've already given you too much code there.[/QUOTE] YUP. but to answer the OP's second question: any loop can that can be done with a … Read More

  • [QUOTE=jephthah;1068614]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.[/QUOTE]That reminds me of [URL="http://www.daniweb.com/forums/post94143.html#post94143"]an old post[/URL]. But I seem to have left out recursion. :P (And it … Read More

0

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.

0

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.

0

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.

1

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.

Votes + Comments
answered my question clearly.
0

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.

1

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.