2079 1

BIT102 ยท 2079 ยท 10 marks

List the advantages and disadvantages of global variable. Assume the two data files "numl.txt" and "num2.txt" containing integers. Merge these two files to the third file named " file3.txt". Here, the integers of " file3.t-xt" must be written in sorted order.

List the advantages and disadvantages of global variable. Assume the two data files "numl.txt" and "num2.txt" containing integers. Merge these two files to the third file named " file3.txt". Here, the integers of " file3.t-xt" must be written in sorted order.[10]

C ProgrammingChapter 10: File Input Output and PreprocessorFile creation and copying

Quick answer

A global variable is a variable declared outside all functions, making it accessible to every function in the program.

Global Variables & File Merging Program

Part A: Advantages and Disadvantages of Global Variables


Advantages

#AdvantageExplanation
1Universal AccessibilityAny function can read or modify the variable without receiving it as a parameter
2Reduced Parameter PassingFunction signatures stay simple; no need to pass shared data repeatedly
3Easy Data SharingMultiple functions can communicate through a common variable
4PersistenceThe variable retains its value for the entire lifetime of the program
5Convenient for ConstantsUseful for program-wide constants or configuration values

Disadvantages

#DisadvantageExplanation
1Uncontrolled AccessAny function can accidentally overwrite the value, causing hard-to-find bugs
2Namespace PollutionLarge programs with many globals risk naming conflicts
3Reduced ModularityFunctions depend on external state, making them difficult to reuse
4Violates Data HidingAll data is exposed, breaking the principle of encapsulation
5Difficult DebuggingTracking which function changed the value requires checking the entire program

Part B: Merge Two Files into a Third File in Sorted Order

Logic / Algorithm

1. Open num1.txt and read all integers into array a[]
2. Open num2.txt and read all integers into array b[]
3. Copy both arrays into a combined array c[]
4. Sort c[] using Bubble Sort (ascending order)
5. Open file3.txt for writing
6. Write every element of sorted c[] into file3.txt
7. Close all files

Complete C Program

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

int main()
{
    FILE *f1, *f2, *f3;
    int a[100], b[100], c[200];
    int n1 = 0, n2 = 0;
    int i, j, k = 0, temp;

    /* ---- Step 1: Read integers from num1.txt ---- */
    f1 = fopen("num1.txt", "r");
    if (f1 == NULL)
    {
        printf("Error: Cannot open num1.txt\n");
        exit(1);
    }
    while (fscanf(f1, "%d", &a[n1]) != EOF)
    {
        n1++;
    }
    fclose(f1);

    /* ---- Step 2: Read integers from num2.txt ---- */
    f2 = fopen("num2.txt", "r");
    if (f2 == NULL)
    {
        printf("Error: Cannot open num2.txt\n");
        exit(1);
    }
    while (fscanf(f2, "%d", &b[n2]) != EOF)
    {
        n2++;
    }
    fclose(f2);

    /* ---- Step 3: Combine both arrays into c[] ---- */
    for (i = 0; i < n1; i++)
        c[k++] = a[i];

    for (i = 0; i < n2; i++)
        c[k++] = b[i];

    /* ---- Step 4: Sort c[] using Bubble Sort ---- */
    for (i = 0; i < k - 1; i++)
    {
        for (j = 0; j < k - i - 1; j++)
        {
            if (c[j] > c[j + 1])
            {
                /* Swap */
                temp    = c[j];
                c[j]    = c[j + 1];
                c[j + 1] = temp;
            }
        }
    }

    /* ---- Step 5: Write sorted integers to file3.txt ---- */
    f3 = fopen("file3.txt", "w");
    if (f3 == NULL)
    {
        printf("Error: Cannot open file3.txt\n");
        exit(1);
    }
    for (i = 0; i < k; i++)
    {
        fprintf(f3, "%d\n", c[i]);
    }
    fclose(f3);

    printf("Merged and sorted successfully into file3.txt\n");
    return 0;
}

Dry Run Example

num1.txt contains: 5 2 8 num2.txt contains: 3 1 7

StepArray State
After readinga[] = {5, 2, 8}, b[] = {3, 1, 7}
After combiningc[] = {5, 2, 8, 3, 1, 7}
After Bubble Sortc[] = {1, 2, 3, 5, 7, 8}
Written to file3.txt1 2 3 5 7 8 (one per line)

Key File Handling Functions Used

FunctionPurpose
fopen(name, "r")Open file for reading
fopen(name, "w")Open file for writing
fscanf(fp, "%d", &x)Read an integer from file
fprintf(fp, "%d\n", x)Write an integer to file
fclose(fp)Close the file and flush buffer
exit(1)Terminate program on error

Conclusion

Global variables simplify data sharing but reduce modularity and security, so they should be used carefully. The file merging program demonstrates practical use of fopen(), fscanf(), fprintf() and fclose(), while the bubble sort applied to the combined array guarantees that every integer read from num1.txt and num2.txt is written into file3.txt in ascending order.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.