Thread: for loop again and array values, need help

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    for loop again and array values, need help

    can someone help me out, im stuck. what im trying to do is square each value that the array holds, but my loop only squares the array index, not the value that it holds. any suggestions?

    j[0] =4
    j[1] =2

    my loop only squares 0^2 =0 and 1^2 =1, instead of 4^2 and 2^2
    Code:
    for (j=0;j<count;j++)
        {
            numbers[j]= pow(j,2);
        }
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    what you are doing in your code is sending down , j ( which is the index number) to the power function.

    Code:
    for (j=0;j<count;j++)  //j is used to traverse the array
        {
            numbers[j]= pow(j,2); //over here you are squaring the index.
        }
    Just change your code to this , and it will do what you want

    Code:
    for (j=0;j<count;j++)
        {
            numbers[j]= pow(numbers[j],2);
        }

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    hey thanks for the help so close yet i couldnt see it
    When no one helps you out. Call google();

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    he he , no problem

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    i need some help on this again, even though the above code is correct and it squares the values, when i run the program it displays the values squared, i want the values to be displayed as they are in the file. for example
    x[0] = 3
    x[1]= 2

    the program should output:
    3
    2

    the sum of these values when squared is: 14.
    this is what i have so far.
    Code:
    for(j=0;j<count; j++)
    	  {
    		  numbers[j]= pow(numbers[j],2);
    	  }
    	  
    		for(j=0; j<count;j++)
    		{
    			sumofsqrs+=numbers[j];
    		}
    
    
    	return sumofsqrs;
    the code does work correctly for squaring and then adding the squares but the values are displayed squared, the program should display the original values in the array not the square of the values. any ideas where the bug is?
    When no one helps you out. Call google();

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Well, you changed the values of your array when squaring them. Your array contains only squared values now. You need to access the file again...
    I hope I understand correctly what you mean.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by InvariantLoop
    Code:
    for(j=0;j<count; j++)
          {
              numbers[j]= pow(numbers[j],2);
          }
          
            for(j=0; j<count;j++)
            {
                sumofsqrs+=numbers[j];
            }
    
    
        return sumofsqrs;
    the code does work correctly for squaring and then adding the squares but the values are displayed squared, the program should display the original values in the array not the square of the values. any ideas where the bug is?
    Yes -- where you modify the array. (Worth another look?)

    [edit]D'oh! Too many open tabs.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    thanks again Dave i looked at your previous post again, it seems to work as i want it now.
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed