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

1. 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. Originally Posted by Karyumi
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. 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. Originally Posted by Codegeek892
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. 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

6. @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. @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. Originally Posted by jimblumberg
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. 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. Originally Posted by jimblumberg
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.