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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:01:53+00:00 2026-05-26T21:01:53+00:00

For some reason my outer for loop does not seem to be doing anything,

  • 0

For some reason my outer for loop does not seem to be doing anything, I have checked all the paranthesis and everything looks ok but it is still not looping.
With this program I want to sum 50 random numbers (the numbers can either be 1 or -1…it’s for a computational physics problem) and print the magnitude, which the program does. BUT I want to go further and do this 10 times, and calculate the average magnitude.
I know what I need to do I’m just having a problem with this loop.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h> //Neeed this to seed the random generator with time, or else it      will always generate the same numbers.

//This is a program to calculate the magnitude of the displacement of a particle after    random collisions.
#define RAND_MAX 1
int main()
{   
    //using ints because the particle can only move one unit on x-axis at a time.
    int i, x, displacement, sum = 0, avg;
    int total_disp=0, mag_disp;
    srand(time(NULL));


    //Each collision causes the particle to advance or retreat by one unit on the x axis.

    for(i=0; i<10; i++)
    {   
        for (i=0; i<50; i++)    //for 50 collisions
        {

            x = rand() % 2;  //generates random numbers between 0 and 1.
            if (x==0)   //if x is 0 then it was a displacement in the minus x direction
            {
                displacement = -1;
            }
            else {  //if x is 1 it is a displacement in the minus x direction
                displacement = 1;
            }
            printf("the disp is : %d\n", displacement);
            total_disp = total_disp + displacement;     //sum the displacements

        }

        if (total_disp < 0) {
            mag_disp = total_disp * -1;
        }

        else{ mag_disp = total_disp; }

        printf("The total displacement is: %d\n", mag_disp);
        sum = sum + mag_disp;   //sum of the displacement magnitudes, there should be ten of them in this case
        avg = sum / i; //average displacement is the sum of all the magnitudes divded by the number of times this is performed.
        printf("The average displacement for i = %d particles is: %d", i, avg);
    }
    return 0;
}
  • 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-26T21:01:54+00:00Added an answer on May 26, 2026 at 9:01 pm

    You cannot use the same iteration variable in both loops.

    for(i=0; i<10; i++)
        for (i=0; i<50; i++)
    

    increments i to 50 in the first iteration of the outer loop.

    for(i=0; i<10; i++)
        for (j=0; j<50; j++)
    

    will work.

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

Sidebar

Related Questions

For some reason, Section 1 works but Section 2 does not. When run in
For some reason i can't seem to get this right ok i have 2
For some reason, it seems that the outer block doesn't seem to update recursively,
For some reason I never see this done. Is there a reason why not?
For some reason the Windows command prompt is special in that you have to
I have been working on this for the past 5 hours. For some reason
I hope I'm not too vague with this question. For some reason I've developed
for some reason my loading gif is not showing when I make my ajax
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its

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.