Can you please help me? (Frequency/Interval)
Can you please help me? (Frequency/Interval)
hello! i am a noob to C programming. I have been searching over the net for the pas two weeks, but in vain. Let me describe my situation. I would like to create a C Console application which will calculate the frequency and interval of marks for students. More description:
1. The user should enter the NUMBER of students.
2. Then user shall be able to enter MARKS for each student.
If there are 10 students, the user will enter 10 marks.
here is an example:
no. of students=10
marks: 46 37 28 87 76 83 76 38 76 37
The program must now calculate the frequency of each mark and the interval which exists between identical marks.
for the above example:
Mark Frequency
46 1
37 2
28 1
87 1
76 3
83 1
38 1
The interval between number 37 is 7 and interval between number 76 is 1.
CAN ANYBODY PLEASE HELP ME???
26 Oct 06, 5:07AM
Hi, I think I could be able to help, but I need some further information first.
What do you mean by the interval between two identical marks? Could you please give another example?
In your example you had these marks
46 37 28 87 76 83 76 38 76 37
The way I see it, the interval between two identical marks is the number of marks which are found between these identical marks. For example in the case of 37, these marks were found: 28 87 76 83 76 38 76, which total a number of 7 as you said. However for 76, they were: 83 and then 38, so each time a total number of 1, as you said. But what would happen in the following situation (considering 76 as identical marks):
76 83 87 76 38 76
In this case the number of marks between two identical marks is not the same. Should we consider the maximum in this case, so the answer for 76 would be 2?
Looking forward to your reply.
26 Oct 06, 1:10PM
if you could help me to make it print 2 lines, one for the first interval (2) and the other one for second interval (1) for the mark 76, it would be better...
26 Oct 06, 9:15PM
I think I've just found a solution to your problem. In what follows I will list the whole code with comments to make it easier to follow.
#include <stdio.h> int main() { // input no. of students int n, i; printf("no. of students = "); scanf("%d", &n); // allocate an array for exactly "n" marks int *marks = new int[n]; // input marks printf("marks: "); for (i = 0; i < n; i++) scanf("%d", &marks[i]); // compute frequencies int freq[101] = {0}; for (i = 0; i < n; i++) freq[marks[i]]++; // display non-zero frequencies printf("\nMark Frequency\n\n"); for (i = 0; i < 101; i++) if (freq[i]) printf("%d %d\n", i, freq[i]); // display all intervals between identical marks printf("\nIntervals\n\n"); int pos[101] = {0}; for (i = 0; i < n; i++) { if (pos[marks[i]]) printf("%d %d\n", marks[i], i - pos[marks[i]]); pos[marks[i]] = i + 1; } printf("\n"); // free memory used by the marks[] array delete[] marks; return 0; }
I've tried inputing your example and here is what has been displayed (notice that the frequencies are displayed considering the ascending order of the marks).
no. of students = 10 marks: 46 37 28 87 76 83 76 38 76 37 Mark Frequency 28 1 37 2 38 1 46 1 76 3 83 1 87 1 Intervals 76 1 76 1 37 7
The algorithm I've used is very quick even for a large number of marks. It goes two times through the elements of the marks array - one time to compute the frequencies and the other to find the intervals. Thus the algorithm has linear complexity. As you may have noticed I've used two additional arrays "freq" and "pos" of 101 elements, which help at minimising the total running time.
Please let me know if you understand how the algorithm works or not. I am ready to give you any further information concerning the algorithm or the C implementation.26 Oct 06, 11:59PM
i can understand almost everything in it (after all, comments are in english), but what do you mena by the "101-elements"???
Seems that you are good to programming... thanks mate!
**I still haven't compiled it. Thouhgt of replying and then do so.
27 Oct 06, 12:28AM
:S after i tried to compile, i got some errors :S
10 C:\Dev-Cpp\Freq\Freq.c `new' undeclared (first use in this function) 10 C:\Dev-Cpp\Freq\Freq.c syntax error before "int" 40 C:\Dev-Cpp\Freq\Freq.c `delete' undeclared (first use in this function) 40 C:\Dev-Cpp\Freq\Freq.c syntax error before ']' token
27 Oct 06, 6:43AM
Hi again and sorry for not being too consistent with the code. I have mixed C with C++ and you have saved the file using a .C extension which enabled the compiler to compile the code in plain C mode. In this mode the new and delete operators are not recognized, therefore you have received those errors. There are two solutions to this:
1. Rename the file into one with a .cpp extension and then compile again, it should work well
2. Use the following version which is now written entirely in plain C mode, and save it using a .c extension.
#include <stdio.h> #include <stdlib.h> int main() { // input no. of students int n, i; printf("no. of students = "); scanf("%d", &n); // allocate an array for exactly "n" marks int *marks = (int*) malloc(n * sizeof(int)); // input marks printf("marks: "); for (i = 0; i < n; i++) scanf("%d", &marks[i]); // compute frequencies int freq[101] = {0}; for (i = 0; i < n; i++) freq[marks[i]]++; // display non-zero frequencies printf("\nMark Frequency\n\n"); for (i = 0; i < 101; i++) if (freq[i]) printf("%d %d\n", i, freq[i]); // display all intervals between identical marks printf("\nIntervals\n\n"); int pos[101] = {0}; for (i = 0; i < n; i++) { if (pos[marks[i]]) printf("%d %d\n", marks[i], i - pos[marks[i]]); pos[marks[i]] = i + 1; } printf("\n"); // free memory used by the marks[] array free(marks); return 0; }
27 Oct 06, 7:16AM
I am now going to explain the logic behind the algorithm and the 101-elements arrays.
So first I read the user data and store it like this: n = the number of marks, marks[] = a dynamically allocated array containing all marks. Now I have declared the freq[] array containing 101 elements, with this rule: freq[k] = the frequency of mark k. I had to use 101 elements since the marks could go from 0 to 100, which total 101 possible marks. By declaring the freq[] array this way
int freq[101] = {0};all its elements are first assigned a value of 0. What I do next is browse through the marks array and for each of its elements I add up 1 to their corresponding element in the freq[] array. Then I display the marks and their corresponding frequencies by browsing through the freq[] array and only considering the non-zero elements. Next I declare the pos[] array and assign to each of its elements a value of 0. Again I browse through the marks[] array and for each mark I store its position in the marks[] array into its corresponding element in the pos[] array. There are two possibilities - either it is the first time I store the position for a particular mark, in which case I do not display anything, or it is the second time and therefore I display the difference between the new position and the previously stored position for that mark. I conclude that it is the first time whenever the corresponding element in the pos[] array for that mark is 0, so no position has been assigned to it. Please let me know if you have further questions on the algorithm or get any more compiler errors.
Login