Jump to content

[c] Output of for/while function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
{Nabla}

{Nabla}

    Newbie

  • Members
  • Pip
  • 7 posts
In c, say you have something like:

for(m=0;m<=100;m++)

{

float function=a+(m*p);

float answer=function*function;

{

where a and p are other variables that have alredy been declared, then what is the output of this loop?

Is it a 1D array from 0-100?

Basically, what I want to do is to be able to take the sum of the answers (from the variable 'answer') from m=0 to m=100, and put it in a new variable/output it.

#2
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
There is no output... lol. Your code is full of bugs as well.

#3
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Declare answer outside the loop, otherwise it will be reset with every pass of the loop. To my understanding, what you want to do is this:
float answer;
for(m = 0;m <= 100;m++){
 //calculate answer for this m, store in a variable called, 
 //for example, fun.
 answer += fun;
}
printf("The answer is %g",answer);


#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Is this question like the Gauss series sum (or perhaps in this case, product)? That is,

Quote

1+2+...+99+100=(1+100)*50=5050


#5
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
I think they mean to apply a function before summing the answers.

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
[deleted]

#7
{Nabla}

{Nabla}

    Newbie

  • Members
  • Pip
  • 7 posts
Well, I'm new to c, so please excuse the bad code. :/

Aereshaa said:

I think they mean to apply a function before summing the answers.

Yep, it's not just an arithmetic or geometric series I'm after, it's the sum of a general function f(m) from m=0 to m=n, which in this case is n=100. So If you have ever seen summation notation it would look something like:

[Apparently I can't post images or links yet. See wiki on 'summation notation']

Where in this case S=[0,100] (belonging to the integers). Although I think the method would be similar to that of an arithmetic/gemetric series anyway.

Quote

Declare answer outside the loop, otherwise it will be reset with every pass of the loop. To my understanding, what you want to do is this:

float answer;

for(m = 0;m <= 100;m++){

 //calculate answer for this m, store in a variable called, 

 //for example, fun.

 answer += fun;

}

printf("The answer is %g",answer);

OK thanks, I did think about how the variable might just be reset with every loop, but what I want is for the program to 'record' the answer from each loop, and then add them all together at the end, as above. I'm thinking the only way to do this would be to declare 101 variables, and have the loop save the answer to one of these variables on each iteration. Then I could add the variables easily. But I don't know how to do this.

Is your code printing the output for each iteration (giving a list)? I don't understand the += operation you've used with fun, do you mind explaining? Also will fun be reset once it is printed? Ifso how can I have it that instead of being reset it is saved as a seperate variable? Thanks

#8
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
float Sigma(int n)
{
   int i;
   float sum = 0;
   for (i = 0; i <= n; i++) sum += someFunction(i);
   return sum;
}

Something along those lines?? If you sit down and think about what you're doing, or even draw it out, the answer usually becomes pretty clear.

To answer your question about +=, these two would be equivalent:

sum = sum + i;
sum += i;

If you don't understand what I'm doing above, you should probably read over some basic C/C++ material for a few hours, which should clear everything up.

#9
{Nabla}

{Nabla}

    Newbie

  • Members
  • Pip
  • 7 posts
No I'm ok, I have seen that function before, just needed refreshing. 'sum += i;' just means increment 'sum' by 'i' on each iteration right?

Quote

float Sigma(int n)
{
   int i;
   float sum = 0;
   for (i = 0; i <= n; i++) sum += someFunction(i);
   return sum;
}

Can you explain what is 'float Sigma(int n) {}'? Are you declaring a float type vairable, 'Sigma'? But then what is (int n)? Declaring an integer, 'n'? But then why is it in brackets following 'Sigma'? Is it because sigma is some function of n,? wWhat function? Also what about the curly parenthesis? Isn't that used to denote the arguments of a function in c, so how can this be a function, when all you are doing is declaring a variable? As you can see I have some problems with that first line, can you please clear it up for me?

next, what about the 'for( ; ; )' where are the statements and containing parenthesis for it? should it be:

float Sigma(int n)
{
   int i;
   float sum = 0;
   for (i = 0; i <= n; i++) 
      {
        sum += someFunction(i);
        return sum;
      }
}

As far as the rest goes, I understand you have declared an integer 'i' which is equivalent to my 'm' and a float, which is initialised as 0. You are evaluating the function at i on each loop, and incrementing the variable 'sum' by this value. So basically, for each loop you have f(i+1) + f(i), which is saved to the variable 'sum'?

It would be helpful if you could explain these things for me.

Edited by {Nabla}, 18 October 2008 - 01:46 PM.


#10
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Returning after a single iteration defeats the purpose of the loop. You obviously don't know C, go read documentation and tutorials as I'm not going to explain what a function is, or how a for-loop works.

#11
{Nabla}

{Nabla}

    Newbie

  • Members
  • Pip
  • 7 posts
Yes, as I said, I'm new to c, and looking for help. My questions are clear and specific, and if you don't want to answer them, why reply? Do you think I haven't already read about for loops?

#12
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts

{Nabla} said:

Yes, as I said, I'm new to c, and looking for help. My questions are clear and specific, and if you don't want to answer them, why reply? Do you think I haven't already read about for loops?

Your questions are clear, and plentiful. We are not here to teach you C, we are here to help you solve specific problems in C or C++. I am trying to help you in the best way possible, and that is for you to cover the basics, then ask questions if something isn't working.

If you had experience in loops, you would realize how very simple this problem is. So I am restating my initial advice: read up on C.