Skip to content

Week 1: Structures, Arrays, Strings, and Word Frequency


Q1. Student Marks Using Structure

Objective: Store details of n students, calculate average marks, and display students scoring above 75%.

#include <stdio.h>
#include <string.h>

struct Student {
    int roll_no;
    char name[50];
    float marks[5];
};

int main() {
    int n;
    printf("Enter number of students: ");
    scanf("%d", &n);

    struct Student students[n];

    // Input student details
    for (int i = 0; i < n; i++) {
        printf("Enter details for Student %d:\n", i + 1);
        printf("Roll No: ");
        scanf("%d", &students[i].roll_no);

        printf("Name: ");
        scanf("%s", students[i].name);

        printf("Marks: ");
        for (int j = 0; j < 5; j++) {
            scanf("%f", &students[i].marks[j]);
        }
    }

    // Display students with average > 75
    printf("\nStudents scoring above 75%%:\n");
    for (int i = 0; i < n; i++) {
        float total = 0;
        for (int j = 0; j < 5; j++)
            total += students[i].marks[j];

        float average = total / 5;
        if (average > 75.0) {
            printf("Roll No: %d, Name: %s, Average Marks: %.2f\n", 
                    students[i].roll_no, students[i].name, average);
        }
    }

    return 0;
}

Q2. Remove Duplicates from Array

Objective: Input n elements in an array and remove duplicates.

#include <stdio.h>

int main() {
    int n, i, j, k;
    printf("Enter number of elements: ");
    scanf("%d", &n);

    int arr[n];
    printf("Enter elements: ");
    for(i = 0; i < n; i++) scanf("%d", &arr[i]);

    // Remove duplicates
    for(i = 0; i < n; i++) {
        for(j = i + 1; j < n; ) {
            if(arr[i] == arr[j]) {
                for(k = j; k < n - 1; k++) arr[k] = arr[k + 1];
                n--;
            } else j++;
        }
    }

    printf("Array after removing duplicates: ");
    for(i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");

    return 0;
}

Q3. Patient Records Using Structure

Objective: Store patient details including admission date and search patients by month & year.

#include <stdio.h>
#include <string.h>

#define MAX_PATIENTS 100
#define MAX_NAME_LEN 100

struct Date {
    int day;
    int month;
    int year;
};

struct Patient {
    int id;
    char name[MAX_NAME_LEN];
    char disease[MAX_NAME_LEN];
    struct Date admissionDate;
};

int main() {
    struct Patient patients[MAX_PATIENTS];
    int n, i, searchMonth, searchYear;

    printf("Enter number of patients: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++) {
        printf("Patient %d:\n", i + 1);
        printf("ID: "); scanf("%d", &patients[i].id);
        getchar(); // Consume leftover newline
        printf("Name: "); fgets(patients[i].name, MAX_NAME_LEN, stdin);
        patients[i].name[strcspn(patients[i].name, "\n")] = '\0';
        printf("Disease: "); fgets(patients[i].disease, MAX_NAME_LEN, stdin);
        patients[i].disease[strcspn(patients[i].disease, "\n")] = '\0';
        printf("Admission Date (dd mm yyyy): ");
        scanf("%d %d %d", &patients[i].admissionDate.day, 
                          &patients[i].admissionDate.month, 
                          &patients[i].admissionDate.year);
    }

    printf("Enter month and year to search: ");
    scanf("%d %d", &searchMonth, &searchYear);

    int found = 0;
    printf("Patients admitted in %02d/%d:\n", searchMonth, searchYear);
    for(i = 0; i < n; i++) {
        if(patients[i].admissionDate.month == searchMonth &&
           patients[i].admissionDate.year == searchYear) {
            printf("ID: %d, Name: %s, Disease: %s, Date: %02d-%02d-%04d\n",
                   patients[i].id, patients[i].name, patients[i].disease,
                   patients[i].admissionDate.day, patients[i].admissionDate.month,
                   patients[i].admissionDate.year);
            found = 1;
        }
    }

    if(!found) printf("No patients admitted in %02d/%d\n", searchMonth, searchYear);

    return 0;
}

Q4. Word Frequency in Paragraph

Objective: Count frequency of each word in a paragraph.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORDS 100
#define MAX_LENGTH 1000

int main() {
    char paragraph[MAX_LENGTH];
    char words[MAX_WORDS][50];
    int count[MAX_WORDS] = {0};
    int i, wordCount = 0;

    printf("Enter paragraph:\n");
    fgets(paragraph, MAX_LENGTH, stdin);

    // Convert to lowercase and remove punctuation
    for(i = 0; paragraph[i]; i++) {
        if (isalpha(paragraph[i]) || isspace(paragraph[i]))
            paragraph[i] = tolower(paragraph[i]);
        else
            paragraph[i] = ' ';
    }

    // Split into words
    char *word = strtok(paragraph, " ");
    while(word != NULL) {
        int found = 0;
        for(i = 0; i < wordCount; i++) {
            if(strcmp(words[i], word) == 0) {
                count[i]++;
                found = 1;
                break;
            }
        }
        if(!found) {
            strcpy(words[wordCount], word);
            count[wordCount] = 1;
            wordCount++;
        }
        word = strtok(NULL, " ");
    }

    // Print result
    printf("\nWord Frequencies:\n");
    for(i = 0; i < wordCount; i++)
        printf("%s - %d\n", words[i], count[i]);

    return 0;
}

Key Points for Week 1:

  1. Structures help organize related data (Student, Patient, Date).
  2. Arrays can be used to store multiple values; removing duplicates requires shifting elements.
  3. Strings can be manipulated using fgets, strtok, strcmp, and strcpy.
  4. Word frequency counting involves converting text to lowercase, removing punctuation, and tracking occurrences.

Comments