snippet of for loop code problem

This is a discussion on snippet of for loop code problem within the C++ Programming forums, part of the General Programming Boards category; hey, I have part of a program that has an array containing 100 items. In a secion of the array ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    snippet of for loop code problem

    hey, I have part of a program that has an array containing 100 items. In a secion of the array I need to shift everything up 1 spot and then inster a number at the intial shift spot, let me give example:

    Code:
    int data [100];
    size_t I;
    I need to shift data[50]......data[98] up one spot to locations
    data[51]....data[99]. After the shift has occured I then want to insert the number 42 in location data[50]

    I know this is a simple for loop for most, will someone show me and explain how the computer will read the for loop so I can use something similar in my program

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Use a for loop, and count backwards, from 98 to 50. Have another variables that starts at 99 and counts down to 51. Assign the value at the first number to the location of the second, decrement your variables, and move on. When the for loop has finished executing, just assign 42 to data[50].

    Don't forget that the first element of a 50 element array is array[0] and the last is array[49].

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    ok let me think that out and write it down and ill come back with what I have and hopefully it will be right, thanks sean

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Code:
    for(i=98;i>=50;i--)
      for(j=99;i>=51;i--)
    would that be the loops i use? I am not sure how to link them together so to speak, how do i assign everything that was in i, now to j?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    whoops, i meant for j to be second for loop not i :

    Code:
    for(i=98;i>=50;i--)
      for(j=99;j>=51;j--)
    would I then just simply set i = j ?

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    for(int i = 98, j = 99; i >= 50, j >=51; i--, j--)
    	 data[j] = data [i];
    edit: if your loop executes just one line of code, you don't need the curly braces {}. Ass long as you keep assignment, comparisons, and increments separated by a semicolon ; you can work on multiple variables by separating them with a comma, as I have done. Remember that the problem will change quite a bit if you're using pointers, which may be necessary depending on where you place this code in your program.
    Last edited by sean; 10-19-2004 at 02:42 PM.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Random Q: If you try to decrement a size_t integer below zero.. will the size_t variable just contain a zero..??
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    No, double for loop will be wrong because you need both i and j to decrease at the same time, whereas like this j will go down to 51, then i decreases 1, then j goes down to 51 again, etc.

    Heres how I'd do it in pseudocode (target is the index you want to stop at):
    Code:
    place value you want to place at target into array[current size+1]
    for (i=new array size; i>target; i--)
      swap array[i] with array[i-1]
    This will work as long as you don't make the array bigger than you gave it space for.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Code:
    for(i=98;i>=50;i--)
      for(j=99;j>=51;j--)
    FYI- That's a nested loop. Very handy, but not for this problem. You do NOT need two indexes (i & j).
    All you need is i and i+1. (Or i and i-1, depending on how you write your program.


    This is NOT a program fragment... just some random C++ statements for you to think about!
    Code:
    data[99] = data[98];  // Copy value from data[98] to data[99]
                             // Both now have the same value
    
    data[98] = data[97];  // Copy value from data[97] to data[98]
    data[97+1] = data[97];   // Same as above
    
    data[i+1] = data[i];    // Copy value from data[i] to data[i+1]

    When your loop is done shifting, data[50] and data[51] will have the same value. Then, you can overwrite data[50] with the new value.
    Last edited by DougDbug; 10-19-2004 at 04:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21