Thread: explaination of code

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Question explaination of code

    hi everyone

    this is the code i dont understand and the output is 24.... so can someone show me working and example of the process..thank you

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    int main()
    {
    const int MAX = 6;
    int arr[MAX];
    int count;
    int temp;
    for(count = 0; count<MAX; count++)
    arr[count] = count+1;
    for(count = 0; count<(MAX/2); count++)
    {
    temp = arr[count];
    arr[count] = arr[MAX - (count + 1)];
    arr[MAX - (count + 1)] = temp;
    }
    for(count = 0; count<MAX ; count++)
    temp = temp + arr[count];
    cout<<temp<<" ";
    system("pause");
    return 0;
    }//end main

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    67 posts and no ever told you to indent your code properly?

    so can someone show me working and example of the process..thank you
    Get a pencil and paper. Across the top of the paper write the headings:
    Code:
    MAX    count    temp   arr[MAX]: arr[0]  arr[1]..  .arr[MAX-1]
    Then, step through the code and write down the corresponding values under the headings. When a variable is assigned a new value, cross out the old value and write down the new value underneath it.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The beauty of programming is just like any spoken language, if you learn the vocabulary and grammar syntax, you can read through it character by character, line by line.

    'sept there is alot more skipping around.... ok maybe this is somewhat untrue, but in the case of your code, it is.
    Sent from my iPad®

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps learning to indent code would help you understand what bits of code get executed inside which loops.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If you use a Microsoft Compiler, it indents for you. In the case of Dev C++, the older versions especialy, it has no identation help at all.

    A good example of good indent is as follows:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // main function
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// declare two integer variables using one line
    
    	int length, height;
    
    	// assign lengh and height values
    
    	height = 7;
    	length = 10;
    
    	// display result to screen by multiplying the variables after the cout statement
    
    	cout << "Total area is: " << length * height << endl;
    
    	getchar();	// freeze output
    
    	return 0;	// indicate sucsessful termination
    }

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    You should place a breakpoint in your code then step through it one step at a time and see which variables get which value on the bottom, or at least Visual Studio does it.

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by swgh
    If you use a Microsoft Compiler, it indents for you. In the case of Dev C++, the older versions especialy, it has no identation help at all.
    No, that's the editor that's doing the indentation.
    C++ compilers (like all compilers except Python AFAIK) actually remove all indentation and other formating

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    What I would do (and do do when debugging code that isn't responding how I would like it) is cout all the variables as they are modified. Add a cout at the start, then right after when ever anything is changed, that way you can see how the numbers are changing... one thing to remember is to show the names too like
    Code:
    cout<<"var count: " << count << endl;
    but that is just my suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM