This C program reads an array of integers from user...

August 26, 2025 at 08:20 AM

#include <stdio.h> #define NMAX 10 int input(int *a, int *n); void output(int *a, int n); void squaring(int *a, int n); int main() { int n, data[NMAX]; // Мы передаем адрес переменной n if (input(data, &n) == 0) { squaring(data, n); output(data, n); } else { printf("n/a"); } return 0; } int input(int *a, int *n) { // Тут мы фиксируем & чтобы считать n if (scanf("%d", n) != 1 || *n <= 0 || *n > NMAX) { return 1; } for (int *p = a; p - a < *n; p++) { if (scanf("%d", p) != 1) { return 1; } } return 0; } void output(int *a, int n) { for (int *p = a; p - a < n; p++) { printf("%d", *p); if (p - a < n - 1) { printf(" "); } } printf("\n"); } void squaring(int *a, int n) { for (int *p = a; p - a < n; p++) { *p = *p * *p; } }

This C program reads an array of integers from user input, squares each element of the array, and then prints the modified array. Here is a detailed explanation of what each part of the code does:


Code Analysis:

  1. Constants and Function Declarations:

    • #define NMAX 10: Defines the maximum number of elements (10) that the array can store.
    • Declares three functions:
      • input: Reads the size of the array and its elements from the user.
      • output: Prints the array elements.
      • squaring: Squares each element of the array.
  2. main Function:

    • Defines an integer array data with a maximum size of NMAX and an integer variable n to store the number of valid elements in the array.
    • Calls the input function to read n (number of elements) and the corresponding elements into the data array.
      • If the input is invalid (e.g., n is not within range or invalid values are provided for the array), input returns 1, and the program outputs n/a.
    • If the input is valid, it calls:
      • squaring: Squares all the elements of the data array in place.
      • output: Outputs the modified (squared) array.
  3. input Function:

    • Reads a single integer n (length of the array) using scanf. It performs several checks:
      • If reading fails or if the value of n is less than or equal to 0 or exceeds NMAX, it returns 1 (indicating invalid input).
    • Then, it attempts to read n integers into the array a.
      • If any input fails while reading the n integers, it also returns 1.
    • If all input is valid, the function returns 0 (success).
  4. output Function:

    • Prints the first n elements of the integer array a, separated by spaces.
    • Adds a newline at the end of the output.
  5. squaring Function:

    • Iterates over the first n elements of the array a and replaces each element with its square.

Example Execution:

Input:

3
1 2 3

Explanation:

  • n = 3, which means the array has 3 elements.
  • The array data is initialized with 1, 2, 3.

Step-by-step execution:

  1. Validate n = 3 (valid as it is ≤ 10 and > 0).
  2. Read the array values: 1, 2, 3.
  3. Squaring:
    • Replace data[0] = 1 * 1 = 1
    • Replace data[1] = 2 * 2 = 4
    • Replace data[2] = 3 * 3 = 9
  4. Output: 1 4 9.

Output:

1 4 9

Invalid Input Example:

Input:

0

Explanation:

  • n = 0, which is invalid (not greater than 0).
  • The program outputs n/a.

Output:

n/a

Summary:

The program:

  1. Reads an integer n and then n elements into an array.
  2. Squares each element of the array.
  3. Outputs the squared values, separated by spaces.
  4. Validates input for correctness (n within valid range and valid integers for elements). Outputs n/a if the input is invalid.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node