Thread: Seeking some clarifications on these codes.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Seeking some clarifications on these codes.

    I have the following three short codes with their outputs. I am seeking clarifications as to how the output was come to. Any explanations are welcome. Thanks in advance.

    Code:
    include <iostream>
    using namespace std;
    int main()
    {
    char str[] = "ABKALM!OV";
    for (int m=1; str[m]; m=m+2)
    cout << str[m];
    return 0;
    
    }
    Output: BAMO
    Code:
    include <iostream>
    using namespace std;
    int main()
    {
    int arr[] = {1,2,3,4,5};
    for (int m=4; m; m=m-1)
    arr[m] = arr[m-1] + m;
    for (int m=0; m<5; m=m+1)
    cout << arr[m];
    
    }
    Output: 12468

    Code:
    include <iostream>
    using namespace std;
    int main()
    {
    char arr[]="UND";
    try {
    for (int m=0; arr[m]; m=m+1) {
    cout << arr[m];
    if (arr[m]=='N')
    throw 3;
    cout << arr[m+2];
    }
    }
    catch (…) {
    cout << "HOPPLA";
    }
    cout << arr;
    return 0;
    }
    Output: UDNHOPPLAUND
    Last edited by Dontgiveup; 04-02-2009 at 05:31 PM.

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    You need a stop condition for your "for" loops. Instead of
    Code:
    for (int m=1; str[m]; m=m+2)
    it should be
    Code:
    for (int m=1; m<9; m=m+2)
    Also, if you use character arrays like that, you're either going to have to count the number of characters each time you use it or use the "strlen" function. Instead, use "string".
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    string str = "ABKALM!OV";
    for (int m=0; m < str.size(); m=m+2)
    cout << str[m];
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    I just wanted to know how that ouput was arrived at. I mean I am just seeking explanations as to what the codes mean that puts out those outputs.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    If someone explains it to you, you'll be deprived of the satisfying experience of figuring it out for yourself. Do you have a debugger? If so, run it on these programs, examining variables as you step through the code. That will help you understand how the output was produced. If you don't have a debugger, get paper and pencil, write down the variables, and, as you step through the code, write down the values of the variables as they change. The first one is pretty easy. Do you see the letters of the output "BAMO" in "ABKALM!OV"? Do you see how the for loop selects those letters to be sent to cout?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NewEgg Promo Code(s)
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-15-2006, 08:16 PM
  2. Escape Codes
    By renurv in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2006, 02:01 PM
  3. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 03:09 PM
  4. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 08:47 AM