Thread: enumerated types

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    enumerated types

    What difference does this two way of using enum make(both give same output)? I dont understand why do we use enum to declare day1.

    Code:
    #include <stdio.h>
    
    
    main()
    {
    	enum day{zero, one, two, three};
    	enum day day1; 
                    day1 = one;
    	printf("%d ",day1);
    
    	return 0;
    }

    Code:
    #include <stdio.h>
    
    main()
    {
    	enum Day {zero, one, two, three};
    	int day1;
    	day1 = one;
    	printf("%d ",day1);
    
    	return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Enums are solely to make your code clearer to read, but don't make a difference from a user standpoint.

    If you had an enum of (Monday, Tuesday, Wednesday, etc) and set day1=Monday, then when you or someone else read your code it would be obvious what you are doing to day1. But if you used integers and set int day1=1 then it becomes confusing and one would have to read the rest of your code to understand what 1 meant, maybe its Sunday, maybe its the number of days since something happened, etc. But in the end, when you print day1, yes either way it will print as an int.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    find max number in array

    i cant understand the following recursive function. Can someone explain how it works? It's used to find a max number in an array. However, i cant visualise how the line for "temp2" works. Does the function updates temp1 to the largest integer in the array compared each time?

    Code:
    int findmax(int array[],int n)
    {
    int temp1;temp2;
    
    if (n==1)
    return array[0];
    else {
    temp1 = findmax(&array[0],n/2);
    temp2 = findmax(&array[n/2],n-n/2);
    return temp1>temp2?temp1;temp2;
    }
    }

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    	enum Day {zero, one, two, three};
    	int day1;
    	day1 = one;
    PJYelton, I think he was asking why you anyone should bother declaring a variable of type Day when you can just declare it as an int and use the enum values anyways.

    I actually don't know for sure, but I imagine maybe it's because if you do this:
    Code:
    enum Day {zero, one, two, three};
    enum Hour {one, zero, three, two};
    int variable = one; //What gets assigned to variable?
    Just a guess, since I never use enums anyways
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    i get it...
    Code:
    enum Day {zero, one, two, three} day1;
    enum Hour {one, zero, three, two} hour1;
    day1 = zero;
    hour1= zero;
    so day1 will have value of 0 while hour1 will have value of 1, right?
    Beside that, i hope someone can help me on the recursive function. Thanks for the help so far.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, I misunderstood what you were asking. I don't use enums that often, but I'm 99% sure that
    Code:
    enum Day {zero, one, two, three} day1;
    enum Hour {one, zero, three, two} hour1;
    is illegal and won't compile, the same enum value can't be used more than once. As for why you would declare a variable as an enum instead of an int, same as I said above, code readability. If you declare day1 as an enum of type Day, then there is no doubt for anyone who reads your code (including you three months from now when you forget what is going on in the code) exactly what you are doing and what day1 is meant for. Declare it as an int and it can get confusing. In a small program it isn't likely to be a problem, but imagine a 10,000 line program. So in the end it doesn't matter, but declaring it as an enum just makes it easier on the eyes and the mind. Plus, I wouldn't be completely surprised if some old compilers gave you errors if you tried this.

    As for the recursive function, temp1 looks for the max in the first half of the array, temp2 looks at the second half of the array, and the function returns whichever is greater. The best advice would be to use a paper and pencil, create a small unsorted array of like say 10 integers, and just go step by step through the funciton keeping track of all variables and recursion levels. This will help you to understand it far better than anything we could explain.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Namespaces can be a solution:
    Code:
    #include <iostream>
    using namespace std;
    
    namespace Day
    {
    	enum Day {zero, one, two, three} Day1;
    }
    
    namespace Hour
    {
    	enum Hour {one, zero, three, two} Hour1;
    }
    
    int main()
    {
    	Day::Day1 = Day::one;
    	Hour::Hour1 = Hour::one;
    	cout << Day::Day1 << " " << Hour::Hour1 << endl;
    
     	return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with enumerated data types.
    By And3rs in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2008, 10:06 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Replies: 1
    Last Post: 02-06-2003, 03:33 PM
  4. Overloading Enumerated Types?
    By Witch_King in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2001, 04:57 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM