i need to write a program that- allow the user to enter the number of students-allow the user to enter each mark-calculate the frequency for each mark-the interval that exists between identical marksthis is what i have donei want to know if there is a different way of doing it

#include
#define maxsize 200
int main(){
    int x,i,j;
    int marks[maxsize];
    int frequency[maxsize]; 
    int interval[maxsize];  
    printf("Enter the number of Students: ");   
    scanf("%d",&x);
    while(x

Dani AI

Generated

As pointed out, the posted snippet is incomplete and hard to follow; clear formatting and the full source are essential. The problem actually splits into two tasks: (1) count how many times each mark appears, and (2) interpret the “interval between identical marks.” A common and useful interpretation of “interval” is the distance (in input positions) between consecutive occurrences of the same mark. Two robust strategies are: use a fixed-size frequency table when the mark range is small (e.g., 0–100), or group identical values for arbitrary integers by sorting value/index pairs. The example below implements the latter: it produces a frequency for each distinct mark and the positional gaps between repeated occurrences.

#include <stdio.h>
#include <stdlib.h>

typedef struct { int val; int idx; } Mark;

int mark_cmp(const void *a, const void *b){
    const Mark *A = a; const Mark *B = b;
    return (A->val > B->val) - (A->val < B->val);
}
int int_cmp(const void *a, const void *b){
    int A = *(const int*)a; int B = *(const int*)b;
    return (A > B) - (A < B);
}

int main(void){
    int n;
    if (scanf("%d", &n) != 1 || n <= 0) return 1;
    Mark *m = malloc(n * sizeof *m);
    if (!m) return 2;
    for (int i = 0; i < n; ++i){
        if (scanf("%d", &m[i].val) != 1) { free(m); return 3; }
        m[i].idx = i;
    }
    qsort(m, n, sizeof *m, mark_cmp);
    for (int i = 0; i < n; ){
        int j = i + 1;
        while (j < n && m[j].val == m[i].val) ++j;
        int freq = j - i;
        printf("Mark %d: frequency %d\n", m[i].val, freq);
        if (freq > 1){
            int *inds = malloc(freq * sizeof *inds);
            for (int k = 0; k < freq; ++k) inds[k] = m[i+k].idx;
            qsort(inds, freq, sizeof *inds, int_cmp);
            printf("Intervals (index gaps):");
            for (int k = 0; k + 1 < freq; ++k) printf(" %d", inds[k+1] - inds[k]);
            printf("\n");
            free(inds);
        }
        i = j;
    }
    free(m);
    return 0;
}

Notes: validate scanf return values and bounds (N positive), free allocated memory, and pick the simple frequency-array method for small known ranges for better performance. If “interval” was intended to mean numeric difference between distinct marks, use simple subtraction of the distinct values instead. This approach builds on ’s suggestion to store inputs in an array while keeping the implementation clearer and safer.

Recommended Answers

All 4 Replies

This has got to be the worst job of posting I have ever seen anywhere on the web! If you won't make the effort to post something readable then nobody is going to make the effort to respond to your question. If you don't know how to use code tags please read .

Always post your message twice, maybe the second time someone will take the efford in decoding it.... ;)

#include
#define maxsize 200
int main()
{	
    int x,i,j;	
    int marks[maxsize];
    int frequency[maxsize];	
    int interval[maxsize];	
    printf("Enter the number of Students: ");	
    scanf("%d",&x);	

    while(x ??????????????? where is the rest of the program


}

[edit]Ancient Dragon: This is what your program should look like. Notice the spacing and other formatting. Also you did not post all of the program![/edit]

well to start with what other way are u talking about????

u didnt even post the entire source code.


well heres what u can do:-

enclose the scanf and the necessary statements to compute the frequency,mean etc etc in a loop with the loop counter initialized to the number of students.

then store the results in an array and print it out.


its simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.