This C program performs the following tasks: 1. **Reads 10 integers...

August 26, 2025 at 12:59 PM

#include <stdio.h> int read10(int *a) { for (int i = 0; i < 10; i++) if (scanf("%d", &a[i]) != 1) return 0; return 1; } void sort10(int *a) { for (int i = 0; i < 9; i++) for (int j = 0; j < 9 - i; j++) if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } void print10(const int *a) { for (int i = 0; i < 10; i++) { if (i) printf(" "); printf("%d", a[i]); } } int main(void) { int a[10]; if (!read10(a)) { printf("n/a"); return 0; } sort10(a); print10(a); return 0; }

This C program performs the following tasks:

  1. Reads 10 integers from the user:

    • The read10 function takes an integer array a as an argument.
    • It uses a loop to read up to 10 integers from standard input using scanf.
    • If it fails to read any of the integers (if the user enters something that isn't a valid integer), it returns 0, causing the program to print "n/a" and terminate.
  2. Sorts the 10 integers in ascending order:

    • The sort10 function is a simple implementation of the bubble sort algorithm.
    • It iterates through the array, swapping adjacent elements if the current element is greater than the next, sorting the array in ascending order.
  3. Prints the sorted 10 integers:

    • The print10 function prints the integers in the array a in a single line, separated by spaces.
  4. Main function workflow:

    • The main function declares an array a of size 10.
    • It calls read10 to fill the array with user input. If read10 fails (returns 0), the program prints "n/a" and exits.
    • It calls sort10 to sort the array in ascending order.
    • It then calls print10 to display the sorted integers.

Example Execution:

Input:

10 3 5 1 2 7 6 4 9 8

Output:

1 2 3 4 5 6 7 8 9 10

Input with invalid data (e.g., letters):

10 3 x 5 2 7 6 4 9 8

Output:

n/a
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