Like Tree1Likes
  • 1 Post By jimblumberg

How do I assign values to elements of a 2D array?

This is a discussion on How do I assign values to elements of a 2D array? within the C Programming forums, part of the General Programming Boards category; I'm currently making a code that finds that mean, median and mode of a set of numbers given by the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    How do I assign values to elements of a 2D array?

    I'm currently making a code that finds that mean, median and mode of a set of numbers given by the user.

    My mean and median functions work just fine but I'm having problems with the mode.

    Here are my main and mode functions:
    Code:
    int main()
    {
        int n=0;
        int i=0;
        int choice=0;
    
        printf("Please indicate how many numbers you wish to enter: ");
        scanf("%d", &n);
    
        float numbers[n];
    
        printf("Enter numbers: \n");
    
        for(i=0; i<n; i++)
        {
            scanf("%f", &numbers[i]);
        }
    
        order(numbers, n);
    
        do
        {
            printf("Enter action:\n1. Get Mean\n2. Get Median\n3. Get Mode\n4. Exit\n");
            scanf("%d", &choice);
    
            switch(choice)
            {
                case 1:
                    getMean(numbers, n);
                    break;
    
                case 2:
                    getMedian(numbers, n);
                    break;
    
                case 3:
                    getMode(numbers, n);
                    break;
    
                case 4:
                    break;
    
                default:
                    printf("Option does not exist!\n");
                    break;
            }
        }while(choice!=4);
    
        return 0;
    }
    
    int getMode(float numbers[], int n)
    {
        int i;
        int j;
        int count = 1;
        int comp[n][n];
    
        for(i=0; i <= n; i++)
        {
            for(j=1; (i+j)<=n; j++)
            {
                if(number[i] == numbers[i+j])
                {
                    count++;
                }
    
                if((i+j) == n)
                {
                    comp[i] = count;
                    count = 1;    
                }
            }
        }
    
        
    
        return 0;
    }

    I'm planning on making my variable comp[] (which stores the count of frequency of numbers) a 2D array so that I can put the numbers beside it's frequency, but I'm not sure how I can assign the values to their respective position.

    Help anyone?
    Thanks!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Karyumi View Post
    I'm planning on making my variable comp[] (which stores the count of frequency of numbers) a 2D array so that I can put the numbers beside it's frequency, but I'm not sure how I can assign the values to their respective position.
    For counting the frequency of each number you don't need a 2D array.

    I suppose you want something like this:
    Code:
    comp[1.5] = 3
    comp[2.0] = 1
    comp[4.5] = 11
    But that's not possible in C because array indices must be of integer type.

    You have to either use two different arrays (one for the float, the other for the frequency) connected with the index (the index of the float number in your numbers array is the same as the index of the corresponding frequency in the comp array) or create your own data structure where you can store float number and frequency together.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    If you really need a 2-D array, the way you could do it is have an array that is 2 columns wide and N rows long(best with c99 as you could use Variable length arrays rather than having to preset your size). Have one column list the number, and the other the frequency.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Codegeek892 View Post
    Have one column list the number, and the other the frequency.
    Do you mean something like
    Code:
    comp[0][0] = 12.34;  // floating point number
    comp[0][1] = 4;         // frequency
    ?

    That doesn't work in C because you can't mix types in an array.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    It will work if comp is an array of floating point numbers. However I would recommend the use of a structure to hold the information.

    Code:
    struct comp
    {
       int number;
       double frequency;
    };
    Jim
    AndiPersti likes this.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    39
    @jimblumberg: if the numbers are read in as floats, then shouldn't promotions promote the integers to floating numbers? Also, it would be possible to rewrite the getmode function to put the frequencies in float format.


    Nevertheless, I do agree that a data structure is the cleaner approach.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    @jimblumberg: if the numbers are read in as floats, then shouldn't promotions promote the integers to floating numbers?
    That is why I said it will work if your array is a floating point type.
    Also, it would be possible to rewrite the getmode function to put the frequencies in float format.
    Probably. It's your function you should be able to modify it for your needs.

    There is a possible problem with your code:
    Code:
    printf("Please indicate how many numbers you wish to enter: ");
        scanf("%d", &n);
     
        float numbers[n];
    Unless you are using a C compiler that is supporting the the C99 C standard using a non-constant value when defining an array is not allowed. Your compiler may support this, but I don't recommend using compiler specific extensions when learning the language.

    Jim

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by jimblumberg View Post
    Unless you are using a C compiler that is supporting the the C99 C standard using a non-constant value when defining an array is not allowed. Your compiler may support this, but I don't recommend using compiler specific extensions when learning the language.
    It's not a compiler specific extension if it's part of the C99 standard. Besides, C99 became a standard 13 years ago, and many compilers probably supported variable-length arrays years before that (though at the time they were extensions to the then-standard language definition). I personally wouldn't use a variable-length array in this situation without doing some bounds-checking on the length first.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    It's not a compiler specific extension if it's part of the C99 standard. Besides, C99 became a standard 13 years ago, and many compilers probably supported variable-length arrays years before that (though at the time they were extensions to the then-standard language definition).
    I said if the OP is using a compiler that was not C99 compliant it could be a problem. For example, all versions of the Microsoft compiler are not C99 compliant, but Microsoft may provide an extension that will allow this behavior. Microsoft only supports C90, and they have said they will probably never support C99 much less the current standard C11.

    Jim

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by jimblumberg View Post
    I said if the OP is using a compiler that was not C99 compliant it could be a problem. For example, all versions of the Microsoft compiler are not C99 compliant, but Microsoft may provide an extension that will allow this behavior. Microsoft only supports C90, and they have said they will probably never support C99 much less the current standard C11.

    Jim
    Ah, I see what you're saying. In that case it is a compiler extension, since the standard that it supports doesn't have variable-length arrays. I didn't really know what standard their compiler claimed to comply to; I don't follow outdated technology like Microsoft very closely these days to know much about their software, though I am quite aware that they tend not to follow standards very well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assign values to array of structures
    By rlesko in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2010, 03:31 PM
  2. Assign values to a set of array of structures
    By glucosonte in forum C Programming
    Replies: 1
    Last Post: 08-26-2009, 09:10 AM
  3. Assign an arrays values to another array
    By laczfinador in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 08:46 AM
  4. Assign value of multiple char array elements to int
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 05:31 AM
  5. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 01:02 AM

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