LearnFrenzy
Sign in Take a free test

C Programming · Topic 8 of 23

Arrays

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Programs & Examples

Arrays · Study material

Transpose of a matrix

Swap rows and columns: element m[r][c] goes to t[c][r]. A 2 × 3 matrix becomes 3 × 2.

#include <stdio.h>

int main(void) {
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int t[3][2];

    for (int r = 0; r < 2; r++)
        for (int c = 0; c < 3; c++)
            t[c][r] = m[r][c];

    for (int r = 0; r < 3; r++)
        printf("%d %d\n", t[r][0], t[r][1]);
    return 0;
}

Output

1 4
2 5
3 6

Insertion sort

Take each element and slide it left into its place among the already-sorted ones. Fast for small or nearly-sorted arrays, and stable.

#include <stdio.h>

void insertion_sort(int *a, int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i], j = i - 1;
        while (j >= 0 && a[j] > key) {      /* shift bigger values one place right */
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;
    }
}

int main(void) {
    int a[] = {29, 10, 14, 37, 13};
    insertion_sort(a, 5);
    for (int i = 0; i < 5; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}

Output

10 13 14 29 37

Sorting with qsort() and a comparator

The standard library sorts any array; you supply a function that compares two elements. Returning (a > b) - (a < b) avoids the overflow that a - b causes for large values.

#include <stdio.h>
#include <stdlib.h>

static int by_value(const void *x, const void *y) {
    int a = *(const int *) x, b = *(const int *) y;
    return (a > b) - (a < b);               /* never "a - b": that can overflow */
}

int main(void) {
    int a[] = {42, -7, 19, 0, 3};
    qsort(a, 5, sizeof a[0], by_value);
    for (int i = 0; i < 5; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}

Output

-7 0 3 19 42

A 2D array on the heap with one malloc()

A pointer to a row (int (*m)[cols]) lets one malloc() hold the whole matrix while you still write m[r][c]. One block is faster and easier to free than an array of row pointers.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int rows = 3, cols = 4;
    int (*m)[cols] = malloc(rows * sizeof *m);   /* one block, used as m[r][c] */
    if (m == NULL)
        return 1;

    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            m[r][c] = r * cols + c;

    printf("m[2][3] = %d, block = %zu bytes\n", m[2][3], rows * sizeof *m);
    free(m);                                      /* one malloc, one free */
    return 0;
}

Output

m[2][3] = 11, block = 48 bytes

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links