Program 63 - CSU1128 - CSE 2026 - Shoolini University

Program to demonstrate the array of structures in C by the values entered by the user

Explanation

The program demonstrates the use of an array of structures in C by defining a structure for a point in 2D space and allowing the user to enter values for multiple points.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Structure Definition

The program defines a structure class_63_Point to represent a point in 2D space:

  • struct class_63_Point: Defines the structure with two integer fields x and y.
  • int x;: Represents the x-coordinate of the point.
  • int y;: Represents the y-coordinate of the point.

Variable Declaration

The main() function is defined, and a variable to hold the size of the array is declared:

  • int size;: Variable to hold the size of the array of structures.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the size of the array and the values for each point:

  • printf("Enter the size of the array: ");: Prompts the user to enter the size of the array.
  • scanf("%d", &size);: Reads the size entered by the user and stores it in size.

Array Declaration

The program declares an array of class_63_Point structures with the size entered by the user:

  • struct class_63_Point points[size];: Declares an array of class_63_Point structures.

Data Input

The program reads the data for each point in the array from the user:

  • for (int i = 0; i < size; i++): Loops through each element of the array.
  • printf("Enter data for point %d: ", i);: Prompts the user to enter the coordinates of the point.
  • scanf("%d%d", &points[i].x, &points[i].y);: Reads the coordinates entered by the user and stores them in the respective fields of the structure.

Output Result

The program prints the values of the fields of the structures in the array:

  • for (int i = 0; i < size; i++): Loops through each element of the array.
  • printf("Point %d: (%d, %d)\n", i, points[i].x, points[i].y);: Prints the coordinates of each point in the array.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
// Define a structure for a point in 2D space
struct class_63_Point
{
  int x;
  int y;
};
int main()
{
  printf("\n\n Program to demonstrate the array of structures in C by the values entered by the user. \n\n");

  // Declare a variable to hold the size of the array
  int size;

  // Read the size of the array from the user
  printf("Enter the size of the array: ");
  scanf("%d", &size);

  // Declare an array of Point structures with the size entered by the user
  struct class_63_Point points[size];

  // Read the data for the array from the user
  for (int i = 0; i < size; i++)
  {
    printf("Enter data for point %d: ", i);
    scanf("%d%d", &points[i].x, &points[i].y);
  }

  // Print the values of the fields of the structures in the array
  for (int i = 0; i < size; i++)
  {
    printf("Point %d: (%d, %d)\n", i, points[i].x, points[i].y);
  }

  return 0;
}
                

Output

Program to demonstrate the array of structures in C by the values entered by the user.

Enter the size of the array: 5
Enter data for point 0: 23 54
Enter data for point 1: 12 45
Enter data for point 2: 45 76
Enter data for point 3: 25 65
Enter data for point 4: 34 86
Point 0: (23, 54)
Point 1: (12, 45)
Point 2: (45, 76)
Point 3: (25, 65)
Point 4: (34, 86)