Computer ScienceNEB 2082

Write a C program that reads the account number, name and address of ten customers from users and displays the account number, name and address of these customers using Array and structure. [8] OR…

8

Write a C program that reads the account_number, name and address of ten customers from users and displays the account_number, name and address of these customers using Array and structure. [8] OR What are the components of a function of C ? Describe the Call - by - value and Call - by - reference and passing the function parameters. [4+4]

Answer

Model Answer for NEB 2082 (Class 12, Computer Science)

Question: Write a C program that reads the account_number, name, and address of ten customers from users and displays the account_number, name, and address of these customers using Array and structure.


Solution: C Program Using Arrays and Structures

Step 1: Define the Structure

We first define a structure Customer to store the details of each customer:

  • account_number (integer)
  • name (string)
  • address (string)

Step 2: Declare an Array of Structures

We declare an array of 10 structures (Customer cust[10]) to store details of 10 customers.

Step 3: Input Data from User

Using a loop, we read the details of each customer and store them in the array.

Step 4: Display the Data

We iterate through the array and print the stored details.


Complete C Program

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

// Define the structure for customer details
struct Customer {
    int account_number;
    char name[50];
    char address[100];
};

int main() {
    struct Customer cust[10]; // Array of 10 customers
    int i;

    // Input data for 10 customers
    printf("Enter details of 10 customers:\n");
    for (i = 0; i < 10; i++) {
        printf("\nCustomer %d:\n", i + 1);
        printf("Account Number: ");
        scanf("%d", &cust[i].account_number);
        printf("Name: ");
        scanf(" %[^\n]s", cust[i].name); // Read full name (including spaces)
        printf("Address: ");
        scanf(" %[^\n]s", cust[i].address); // Read full address (including spaces)
    }

    // Display the stored data
    printf("\n--- Customer Details ---\n");
    for (i = 0; i < 10; i++) {
        printf("\nCustomer %d:\n", i + 1);
        printf("Account Number: %d\n", cust[i].account_number);
        printf("Name: %s\n", cust[i].name);
        printf("Address: %s\n", cust[i].address);
    }

    return 0;
}

How the Program Works

  1. Structure Definition (struct Customer)

    • Stores account_number, name, and address for each customer.
  2. Array Declaration (cust[10])

    • An array of 10 structures is created to hold details of 10 customers.
  3. Input Loop (for (i = 0; i < 10; i++))

    • Uses scanf with %[^\n]s to read full names and addresses (including spaces).
    • Stores each customer’s data in cust[i].
  4. Output Loop (for (i = 0; i < 10; i++))

    • Prints the stored details in a structured format.

Sample Input & Output

Input:

Enter details of 10 customers:

Customer 1:
Account Number: 1001
Name: Ram Thapa
Address: Kathmandu

Customer 2:
Account Number: 1002
Name: Sita Gurung
Address: Pokhara
...

(Repeat for 10 customers)

Output:

--- Customer Details ---

Customer 1:
Account Number: 1001
Name: Ram Thapa
Address: Kathmandu

Customer 2:
Account Number: 1002
Name: Sita Gurung
Address: Pokhara
...

Key Concepts Used

  1. Structures (struct)

    • Groups related data (e.g., account_number, name, address) under one name (Customer).
  2. Arrays of Structures

    • cust[10] allows storing 10 customer records efficiently.
  3. Dynamic Input Handling (scanf(" %[^\n]s", ...))

    • Reads full names/addresses (including spaces) until Enter is pressed.
  4. Looping (for loop)

    • Used to input and display data for all 10 customers.

Note: This program ensures modularity (using structures) and scalability (easily extendable to more customers). The use of arrays and structures makes data management efficient.

Discussion

Loading…

More Computer Science questions

All Computer Science old questions