C Programming: How To Make Tables in C Programming Language

In the C programming language, creating a table entails structuring and storing data. Arrays, structures, or multidimensional arrays can all be used to represent a table. Examples of how to construct and manage tables in C are provided below.


C Programming: How To Make Tables in C Programming Language

C Programming

1. Using a Multidimensional Array

In C, a 2D array with rows and columns is a popular approach to represent a table.

Example: Simple Numeric Table

#include <stdio.h>

int main() {
    // Define a 3x3 table (2D array)
    int table[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Print the table
    printf("Table:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", table[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Table:
1 2 3
4 5 6
7 8 9

2. Using an Array of Structures

For more complex tables with different data types (e.g., a student record table), you can use structures.

Example: Student Records Table

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

// Define a structure for a student
struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    // Array of structures
    struct Student students[3];

    // Initialize the student data
    strcpy(students[0].name, "Alice");
    students[0].age = 20;
    students[0].grade = 88.5;

    strcpy(students[1].name, "Bob");
    students[1].age = 22;
    students[1].grade = 91.0;

    strcpy(students[2].name, "Charlie");
    students[2].age = 19;
    students[2].grade = 85.0;

    // Print the student table
    printf("Student Table:\n");
    printf("Name\t\tAge\tGrade\n");
    for (int i = 0; i < 3; i++) {
        printf("%s\t%d\t%.2f\n", students[i].name, students[i].age, students[i].grade);
    }

    return 0;
}

Output:

Student Table:
Name        Age    Grade
Alice       20     88.50
Bob         22     91.00
Charlie     19     85.00

3. Dynamic Table Creation

Use calloc() or malloc() for dynamic memory allocation if the table size is uncertain at build time.

Example: Dynamic Table with User Input

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

int main() {
    int rows, cols;

    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    // Dynamically allocate memory for a 2D array
    int **table = (int **)malloc(rows * sizeof(int *));
    for (int i = 0; i < rows; i++) {
        table[i] = (int *)malloc(cols * sizeof(int));
    }

    // Populate the table
    printf("Enter table elements:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &table[i][j]);
        }
    }

    // Print the table
    printf("Table:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", table[i][j]);
        }
        printf("\n");
    }

    // Free dynamically allocated memory
    for (int i = 0; i < rows; i++) {
        free(table[i]);
    }
    free(table);

    return 0;
}

Sample Input:

Enter number of rows: 2
Enter number of columns: 3
Enter table elements:
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6

Output:

Table:
1 2 3
4 5 6

Key Takeaways:

  1. For uniform data types in fixed-size tables, use multidimensional arrays.
  2. For complicated tables with several data kinds, use structures.
  3. For variable, user-specified table sizes, employ dynamic memory allocation.

Depending on your application's needs and complexity, each technique offers a means to construct and maintain tables in C.

Post a Comment

Previous Post Next Post