Question:
Variable storage in C?
maskme
2012-08-26 10:36:49 UTC
A program ‘c’ with 200 integers in the range 0 – 50 represents the scores of 200 students. It then prints
the frequency of each score above 25. What would be the best way for ‘c’ to store the frequency?

1. an array of 25 numbers
2. an array of 50 numbers
3. an array of 200 numbers
4. a dynamically allocated array of 250 numbers
Four answers:
green meklar
2012-08-26 17:51:56 UTC
>I really need a quick answer without having to ponder about any concepts.



On the contrary, it's quite clear that you DO need to ponder about the concepts, because if you understood this stuff properly you would not be asking others to answer such a simple question for you in the first place.



The answer is, of course, 3. I mean, the question basically already said it: 'The program has 200 integers', so clearly the array is going to have how many numbers? 200 of them.



That said, with all due respect, I highly recommend you start paying attention to the material and getting an understanding of basic program logic if you want to pass that course. Remember, we will not be there for you on the final exam!



If you want some info to get you started, try here:

http://www.thegeekstuff.com/2011/12/c-arrays/

http://en.wikipedia.org/wiki/Array_data_structure

http://www.exforsys.com/tutorials/c-language/c-arrays.html



Of course, if all the integers are in the range 0 to 50, you could have a more efficient solution by using an array of 200 chars, which would use the equivalent memory of only 50 ints. I don't expect you to understand that, but if you can get your act together now, someday you will. :)
justme
2012-08-27 08:23:06 UTC
The obvious answer is 1. Doesnt matter if you are looking at 200 integers or 20 million, you only care about the ones that are ABOVE 25.



Make an array of 25 ints, and whenever a score is entered, if its above 25, increment the proper int in the array.



int freq[25], i, score;



for (i = 0; i < 25; i++)

freq[i] = 0; //reset all to 0



for (i = 0; i < 200; i++){

//get the score and verify its between 0 - 50

if (score > 25) //we only want the scores ABOVE 25, so from 26 - 50

freq[score - 26]++;

}
Ciaron
2012-08-26 10:53:47 UTC
Arrays are fairly simple to declare all you need to know is that an array is indexed from 0 so the first three answers are straight forward

1) int array25 [25];

2) int array50 [50];

3) int array200 [200];

Now for dynamic storage C is a bit different as it uses pointers which many people either get or don't get but once you understand they unleash the power of C and also can be the bane if you use them badly and they can cause no end of problems (been there got the T-shirt).

The Function malloc is most commonly used to attempt to ``grab'' a continuous portion of memory. It is defined by:

void *malloc(size_t number_of_bytes)

It is good practice to use sizeof() even if you know the actual size you want -- it makes for device portable code.



sizeof can be used to find the size of any data type, variable or structure. Simply supply one of these as an argument to the function.



When you have finished using a portion of memory you should always free() it. This allows the memory freed to be aavailable again, possibly for further malloc() calls



The function free() takes a pointer as an argument and frees the memory to which the pointer refers.

as if you run the program again you might have some problems.



int *dynamic_array;

dynamic_array = (int*) malloc(250*sizeof(int)); This now takes a space of 250 integers in memory

....

free();
ʄaçade
2012-08-26 10:44:38 UTC
Do you not understand the basic material in the text book? Is this really where you are stuck?



200 students. 200 scores to be stored. 200 scores to be counted. 200 numbers. But you only have to store 50 counters. (51 actually, because the scores could be 0..50).



Please review the basic concepts of arrays.





Added: The answer is ALMOST #2. You need an array of 51 numbers.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...