StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #22 (U3): Reverse an Array In-Place (two pointers)
Download
  1. C Programming
  2. C Programming Practice
Program #21 (U3): Largest and Second Largest (one pass) : Program #23 (U3): Array Statistics via Functions
C Programming Practice

Task: Read N and the array, then swap elements from both ends moving inward. Print the reversed array.

 
#include <stdio.h>

 
void reverse(int a[], int n) {

    int i = 0, j = n - 1, t;

    while (i < j) { t = a[i]; a[i] = a[j]; a[j] = t; ++i; --j; }

}

 
int main(void) {

    int N, a[200];

 
    printf("Enter N (1..200): ");

    if (scanf("%d", &N) != 1 || N <= 0 || N > 200) { printf("Invalid N.\n"); return 0; }

 
    printf("Enter %d integers: ", N);

    for (int i = 0; i < N; ++i) scanf("%d", &a[i]);

 
    reverse(a, N);

 
    printf("Reversed: ");

    for (int i = 0; i < N; ++i) printf("%d ", a[i]);

    printf("\n");

 
    return 0;

}

 

 

Program #21 (U3): Largest and Second Largest (one pass) Program #23 (U3): Array Statistics via Functions
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website