1<!doctype html><html lang=en dir=ltr><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=robots content="index, follow"><link rel="icon shortcut" href=/favicon.ico sizes=32x32><link rel=icon href=/favicon.svg type=image/svg+xml id=favicon-svg><link rel=icon href=/favicon-16x16.png type=image/png sizes=16x16><link rel=icon href=/favicon-32x32.png type=image/png sizes=32x32><link rel=apple-touch-icon href=/apple-touch-icon.png sizes=180x180><link fetchpriority=low href=/site.webmanifest rel=manifest><title>Arrays in C Programming with Examples â Boolean World</title><meta name=description content="Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays can be to store a fixed number of items of the same data type under a single name."><link rel=canonical href=https://www.booleanworld.com/arrays-in-c/ itemprop=url><meta property="og:title" content="Arrays in C Programming with Examples"><meta property="og:description" content=" 2 Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays can be to store a fixed number of items of the same data type under a single name. 3 "><meta property="og:type" content=" 4 article 5 "><meta property="og:url" content="https://www.booleanworld.com/arrays-in-c/"><meta property="og:image" content="https://www.booleanworld.com/wp-content/uploads/2019/11/c-arrays-cover.png"><meta property="article:section" content="blog"><meta property="article:published_time" content="2019-11-19T15:54:45+00:00"><meta property="article:modified_time" content="2019-11-19T15:54:45+00:00"><meta property="og:site_name" content="Boolean World">
5<script type=application/ld+json>{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"Supriyo Biswas"},"dateModified":"2019-11-19T15:54:45Z","datePublished":"2019-11-19T15:54:45Z","description":"Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays can be to store a fixed number of items of the same data type under a single name.","headline":"Arrays in C Programming with Examples","image":"https://www.booleanworld.com/wp-content/uploads/2019/11/c-arrays-cover.png","mainEntityOfPage":{"@id":"https://www.booleanworld.com/arrays-in-c/","@type":"WebPage"},"publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"https://www.booleanworld.com/android-chrome-192x192.png"},"name":"Boolean World"}}</script>
5<meta name=twitter:card content="summary_large_image"><meta name=twitter:image content="https://www.booleanworld.com/wp-content/uploads/2019/11/c-arrays-cover.png"><meta name=twitter:title content="Arrays in C Programming with Examples"><meta name=twitter:description content="Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays can be to store a fixed number of items of the same data type under a single name."><link rel=preload href=/css/compiled/main.min.92db61eb61acb108b5e74895778f26247612d573342598900cb450f9683775c5.css as=style integrity="sha256-ktth62GssQi150iVd48mJHYS1XM0JZiQDLRQ+Wg3dcU="><link href=/css/compiled/main.min.92db61eb61acb108b5e74895778f26247612d573342598900cb450f9683775c5.css rel=stylesheet integrity="sha256-ktth62GssQi150iVd48mJHYS1XM0JZiQDLRQ+Wg3dcU="><link rel=preconnect href=https://www.googletagmanager.com crossorigin>
vendor: 63 bytes, line 5
5<script async src="https://www.googletagmanager.com/gtag/js?id=
5G-MN0E08FWBV
vendor: 11 bytes, line 5
5"></script>
5<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag("js",new Date),gtag("config","G-MN0E08FWBV")</script>
5<script src=/js/main-head.min.2375b7265ea066ecb314226ce5ff17478453df5f350d34d830b3fc96fba21672.js integrity="sha256-I3W3Jl6gZuyzFCJs5f8XR4RT3181DTTYMLP8lvuiFnI="></script>
5<link rel=stylesheet href=/css/katex.min.6aff82415cedb412f2c82c8c2c57239419d1e6b45f37df0079ef776bc0f2e08fc61f5cb62aeee1f80fb4ee99c229a86d01055999cdbd206b00a82422abdaa230.css integrity="sha512-av+CQVzttBLyyCyMLFcjlBnR5rRfN98Aee93a8Dy4I/GH1y2Ku7h+A+07pnCKahtAQVZmc29IGsAqCQiq9qiMA=="><meta property="og:locale" content="en_US">
5<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3394850521000379" crossorigin=anonymous></script>
5</head><body><a href=#content class="hx:sr-only hx:focus-visible:not-sr-only hx:focus-visible:fixed hx:focus-visible:z-50 hx:focus-visible:top-2 hx:focus-visible:left-2 hx:focus-visible:bg-primary-500 hx:focus-visible:text-white hx:focus-visible:px-4 hx:focus-visible:py-2 hx:focus-visible:rounded-md hx:focus-visible:text-sm hx:focus-visible:font-medium">Skip to content</a><div class="hextra-nav-container hx:sticky hx:top-0 hx:z-20 hx:w-full hx:bg-transparent hx:print:hidden"><div class="hextra-nav-container-blur hx:pointer-events-none hx:absolute hx:z-[-1] hx:h-full hx:w-full hx:bg-white hx:dark:bg-dark hx:shadow-[0_2px_4px_rgba(0,0,0,.02),0_1px_0_rgba(0,0,0,.06)] hx:contrast-more:shadow-[0_0_0_1px_#000] hx:dark:shadow-[0_-1px_0_rgba(255,255,255,.1)_inset] hx:contrast-more:dark:shadow-[0_0_0_1px_#fff]"></div><nav class="hextra-max-navbar-width hx:mx-auto hx:flex hx:items-center hx:justify-end hx:gap-2 hx:h-16 hx:px-6"><a class="hx:flex hx:items-center hx:hover:opacity-75 hx:ltr:mr-auto hx:rtl:ml-auto" href=/><span class="hx:mr-2 hx:font-extrabold hx:inline hx:select-none">Boolean World</span></a><div class="hextra-search-wrapper hx:relative hx:md:w-64"><div class="hx:relative hx:flex hx:items-center hx:text-gray-900 hx:contrast-more:text-gray-800 hx:dark:text-gray-300 hx:contrast-more:dark:text-gray-300"><input placeholder=Search... aria-label=Search... class="hextra-search-input hx:hextra-focus-visible hx:block hx:w-full hx:appearance-none hx:rounded-lg hx:px-3 hx:py-2 hx:transition-colors hx:text-base hx:leading-tight hx:md:text-sm hx:bg-black/[.05] hx:dark:bg-gray-50/10 hx:focus-visible:bg-white hx:dark:focus-visible:bg-dark hx:placeholder:text-gray-500 hx:dark:placeholder:text-gray-400 hx:contrast-more:border hx:contrast-more:border-current" type=search autocomplete=off spellcheck=false> 6<kbd class="hx:absolute hx:my-1.5 hx:select-none hx:ltr:right-1.5 hx:rtl:left-1.5 hx:h-5 hx:rounded-sm hx:bg-white hx:px-1.5 hx:font-mono hx:text-[10px] hx:font-medium hx:text-gray-500 hx:border hx:border-gray-200 hx:dark:border-gray-100/20 hx:dark:bg-dark/50 hx:contrast-more:border-current hx:contrast-more:text-current hx:contrast-more:dark:border-current hx:items-center hx:gap-1 hx:transition-opacity hx:pointer-events-none hx:hidden hx:sm:flex">CTRL K</kbd></div><div><ul class="hextra-search-results hextra-scrollbar hx:hidden hx:border hx:border-gray-200 hx:bg-white hx:text-gray-100 hx:dark:border-neutral-800 hx:dark:bg-neutral-900 hx:absolute hx:top-full hx:z-20 hx:mt-2 hx:overflow-auto hx:overscroll-contain hx:rounded-xl hx:py-2.5 hx:shadow-xl hx:max-h-[min(calc(50vh-11rem-env(safe-area-inset-bottom)),400px)] hx:md:max-h-[min(calc(100vh-5rem-env(safe-area-inset-bottom)),400px)] hx:inset-x-0 hx:ltr:md:left-auto hx:rtl:md:right-auto hx:contrast-more:border hx:contrast-more:border-gray-900 hx:contrast-more:dark:border-gray-50 hx:w-screen hx:min-h-[100px] hx:max-w-[min(calc(100vw-2rem),calc(100%+20rem))]" aria-label="Search results" style="transition:max-height .2s ease 0s"></ul><div class="hextra-search-status hx:sr-only" aria-live=polite role=status></div></div></div><button type=button aria-label=Menu aria-expanded=false class="hextra-hamburger-menu hx:cursor-pointer hx:-mr-2 hx:rounded-sm hx:p-2 hx:active:bg-gray-400/20 hx:md:hidden hx:hextra-focus-visible-inset"><svg height="24" fill="none" viewBox="0 0 24 24" stroke="currentColor"><g><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 8H20"/></g><g><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16H20"/></g></svg></button></nav></div><div class="hx:mx-auto hx:flex hextra-max-page-width"><aside class="hextra-sidebar-container hx:flex hx:flex-col hx:print:hidden hx:md:top-16 hx:md:shrink-0 hx:md:w-64 hx:md:self-start hx:max-md:[transform:translate3d(0,-100%,0)] hx:md:hidden"><div class="hx:px-4 hx:pt-4 hx:md:hidden"><div class="hextra-search-wrapper hx:relative hx:md:w-64"><div class="hx:relative hx:flex hx:items-center hx:text-gray-900 hx:contrast-more:text-gray-800 hx:dark:text-gray-300 hx:contrast-more:dark:text-gray-300"><input placeholder=Search... aria-label=Search... class="hextra-search-input hx:hextra-focus-visible hx:block hx:w-full hx:appearance-none hx:rounded-lg hx:px-3 hx:py-2 hx:transition-colors hx:text-base hx:leading-tight hx:md:text-sm hx:bg-black/[.05] hx:dark:bg-gray-50/10 hx:focus-visible:bg-white hx:dark:focus-visible:bg-dark hx:placeholder:text-gray-500 hx:dark:placeholder:text-gray-400 hx:contrast-more:border hx:contrast-more:border-current" type=search autocomplete=off spellcheck=false> 7<kbd class="hx:absolute hx:my-1.5 hx:select-none hx:ltr:right-1.5 hx:rtl:left-1.5 hx:h-5 hx:rounded-sm hx:bg-white hx:px-1.5 hx:font-mono hx:text-[10px] hx:font-medium hx:text-gray-500 hx:border hx:border-gray-200 hx:dark:border-gray-100/20 hx:dark:bg-dark/50 hx:contrast-more:border-current hx:contrast-more:text-current hx:contrast-more:dark:border-current hx:items-center hx:gap-1 hx:transition-opacity hx:pointer-events-none hx:hidden hx:sm:flex">CTRL K</kbd></div><div><ul class="hextra-search-results hextra-scrollbar hx:hidden hx:border hx:border-gray-200 hx:bg-white hx:text-gray-100 hx:dark:border-neutral-800 hx:dark:bg-neutral-900 hx:absolute hx:top-full hx:z-20 hx:mt-2 hx:overflow-auto hx:overscroll-contain hx:rounded-xl hx:py-2.5 hx:shadow-xl hx:max-h-[min(calc(50vh-11rem-env(safe-area-inset-bottom)),400px)] hx:md:max-h-[min(calc(100vh-5rem-env(safe-area-inset-bottom)),400px)] hx:inset-x-0 hx:ltr:md:left-auto hx:rtl:md:right-auto hx:contrast-more:border hx:contrast-more:border-gray-900 hx:contrast-more:dark:border-gray-50 hx:w-screen hx:min-h-[100px] hx:max-w-[min(calc(100vw-2rem),calc(100%+20rem))]" aria-label="Search results" style="transition:max-height .2s ease 0s"></ul>
7<div class="hextra-search-status hx:sr-only" aria-live=polite role=status></div></div></div></div><div class="hextra-scrollbar hx:overflow-y-auto hx:overflow-x-hidden hx:p-4 hx:grow hx:md:h-[calc(100vh-var(--navbar-height)-var(--menu-height))]"><ul class="hx:flex hx:flex-col hx:gap-1 hx:md:hidden"><li><div class="hextra-sidebar-item hx:group hx:relative hx:flex hx:items-center" data-active=false><a class="hx:flex hx:items-center hx:justify-between hx:gap-2 hx:grow hx:cursor-pointer hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:hextra-focus-visible-inset hx:ltr:pr-8 hx:rtl:pl-8 hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:contrast-more:border hx:contrast-more:border-transparent hx:contrast-more:text-gray-900 hx:contrast-more:hover:border-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:dark:text-gray-50 hx:contrast-more:dark:hover:border-gray-50" href=/pages/><span class="hx:min-w-0 [word-break:break-word]">Pages</span> 8</a><button type=button class="hextra-sidebar-collapsible-button hx:absolute hx:top-1/2 hx:-translate-y-1/2 hx:ltr:right-2 hx:rtl:left-2 hx:shrink-0 hx:cursor-pointer hx:p-0 hx:text-gray-500 hx:dark:text-neutral-400 hx:group-hover:text-gray-900 hx:dark:group-hover:text-gray-50 hx:group-data-[active=true]:text-primary-800 hx:group-data-[active=true]:dark:text-primary-600 hx:hextra-focus-visible-inset" aria-label="Toggle section" aria-expanded=false><svg fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true" focusable="false" class="hx:h-[18px] hx:min-w-[18px] hx:rounded-xs hx:p-0.5 hx:hover:bg-gray-800/5 hx:dark:hover:bg-gray-100/5"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" class="hx:origin-center hx:transition-transform hx:rtl:-rotate-180"/></svg></button></div><div class="hextra-sidebar-children hx:ltr:pr-0 hx:rtl:pl-0 hx:overflow-hidden"><ul class='hx:relative hx:flex hx:flex-col hx:gap-1 hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:ltr:ml-3 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:mr-3 hx:rtl:pr-3 hx:rtl:before:right-0 hx:dark:before:bg-neutral-800'><li class="hx:flex hx:flex-col"><div class="hextra-sidebar-item hx:group hx:relative hx:flex hx:items-center" data-active=false><a class="hx:flex hx:items-center hx:justify-between hx:gap-2 hx:grow hx:cursor-pointer hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:hextra-focus-visible-inset hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:contrast-more:border hx:contrast-more:border-transparent hx:contrast-more:text-gray-900 hx:contrast-more:hover:border-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:dark:text-gray-50 hx:contrast-more:dark:hover:border-gray-50" href=/legal/><span class="hx:min-w-0 [word-break:break-word]">Legal Notices</span></a></div></li></ul></div></li></ul></div></aside><nav class="hextra-toc hx:order-last hx:hidden hx:w-64 hx:shrink-0 hx:xl:block hx:print:hidden hx:px-4" aria-label="Table of contents"><div class="hextra-scrollbar hx:sticky hx:top-16 hx:overflow-y-auto hx:pr-4 hx:pt-6 hx:text-sm [hyphens:auto] hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))] hx:ltr:-mr-4 hx:rtl:-ml-4"><p class="hx:mb-4 hx:font-semibold hx:tracking-tight">On this page</p><ul><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#what-is-an-array-in-c-and-why-should-you-use-them>What is an array in C and why should you use them?</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#declaring-and-using-an-array-in-c>Declaring and using an array in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#alternative-ways-to-initialize-arrays>Alternative ways to initialize arrays</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#loopingiterating-over-an-array-in-c>Looping/iterating over an array in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#array-program-examples-in-c>Array program examples in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#reading-user-entered-numbers-into-an-array>Reading user-entered numbers into an array</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#linear-search-in-an-array>Linear search in an array</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#two-dimensional-2d-arrays-in-c>Two-dimensional (2D) arrays in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#initializing-using-and-looping-over-2d-arrays>Initializing, using and looping over 2D arrays</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#2d-array-program-examples-in-c>2D array program examples in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#reading-user-entered-numbers-into-a-2d-array>Reading user-entered numbers into a 2D array</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#finding-the-transpose-of-a-matrix>Finding the transpose of a matrix</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#adding-two-matrices>Adding two matrices</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:ltr:pl-4 hx:rtl:pr-4 hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#multiplying-two-matrices>Multiplying two matrices</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#multidimensional-arrays-in-c>Multidimensional arrays in C</a></li><li class="hx:my-2 hx:scroll-my-6 hx:scroll-py-6"><a class="hx:font-medium hx:inline-block hx:rounded-sm hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-300 hx:contrast-more:text-gray-900 hx:contrast-more:underline hx:contrast-more:dark:text-gray-50 hx:w-full hx:wrap-break-word hx:hextra-focus-visible-inset" href=#strings-in-c-arrays-of-characters>Strings in C: Arrays of Characters</a></li></ul><div class="hx:mt-8 hx:border-t hx:bg-white hx:pt-8 hx:shadow-[0_-12px_16px_white] hx:dark:bg-dark hx:dark:shadow-[0_-12px_16px_#111] hx:sticky hx:bottom-0 hx:flex hx:flex-col hx:items-start hx:gap-2 hx:pb-8 hx:border-gray-200 hx:dark:border-neutral-800 hx:contrast-more:border-t hx:contrast-more:border-neutral-400 hx:contrast-more:shadow-none hx:contrast-more:dark:border-neutral-400"><button id=backToTop tabindex=-1 class="hx:cursor-pointer hx:transition-all hx:duration-75 hx:opacity-0 hx:text-xs hx:font-medium hx:text-gray-500 hx:hover:text-gray-900 hx:dark:text-gray-400 hx:dark:hover:text-gray-100 hx:contrast-more:text-gray-800 hx:contrast-more:dark:text-gray-50"> 9<span>Scroll to top</span> 10<svg fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" focusable="false" class="hx:inline hx:ltr:ml-1 hx:rtl:mr-1 hx:h-3.5 hx:w-3.5 hx:rounded-full hx:border hx:border-gray-500 hx:hover:border-gray-900 hx:dark:border-gray-400 hx:dark:hover:border-gray-100 hx:contrast-more:border-gray-800 hx:contrast-more:dark:border-gray-50"><path stroke-linecap="round" stroke-linejoin="round" d="M4.5 15.75l7.5-7.5 7.5 7.5"/></svg></button></div></div></nav><article class="hx:w-full hx:break-words hx:flex hx:min-h-[calc(100vh-var(--navbar-height))] hx:min-w-0 hx:justify-center hx:pb-8 hx:pr-[calc(env(safe-area-inset-right)-1.5rem)]"><main id=content class="hx:w-full hx:min-w-0 hextra-max-content-width hx:px-6 hx:pt-4 hx:md:px-12"><div class="hx:mt-1.5 hx:flex hx:items-center hx:gap-1 hx:overflow-hidden hx:text-sm hx:text-gray-500 hx:dark:text-gray-400 hx:contrast-more:text-current"><div class="hx:whitespace-nowrap hx:transition-colors hx:min-w-[24px] hx:overflow-hidden hx:text-ellipsis hx:hover:text-gray-900 hx:dark:hover:text-gray-100"><a href class="hx:inline-block hx:rounded-sm hx:hextra-focus-visible-inset">Blog</a></div><svg class="hx:w-3.5 hx:shrink-0 hx:rtl:-rotate-180" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/></svg><div class="hx:whitespace-nowrap hx:transition-colors hx:font-medium hx:text-gray-700 hx:contrast-more:font-bold hx:contrast-more:text-current hx:dark:text-gray-100 hx:contrast-more:dark:text-current">Arrays in C Programming with Examples</div></div><div class="hx:flex hx:flex-col hx:sm:flex-row hx:items-start hx:sm:items-center hx:sm:justify-between hx:gap-4 hx:mt-2"><h1 class="hx:mb-0 hx:text-4xl hx:font-bold hx:tracking-tight hx:text-slate-900 hx:dark:text-slate-100">Arrays in C Programming with Examples</h1></div><div class="hx:mt-4 hx:mb-6 hx:text-gray-500 hx:dark:text-gray-400 hx:text-sm hx:flex hx:items-center hx:flex-wrap hx:gap-y-2"><span class=hx:mr-1>November 19, 2019</span><span class=hx:mx-1>·</span><span class=hx:mx-1>Supriyo Biswas</span></div><div class=content><p><img src=/wp-content/uploads/2019/11/c-arrays-cover.png alt data-zoomable loading=lazy></p><p>Arrays in C allow you to store multiple items of the same data type, such as a list of integers. Arrays form the basis for many data structures and allow you to build advanced programs.</p><p>
10In this article, we are going to discuss what an array is and how you can use them, along with examples. We will also see the concept of “strings”, which is closely related to the topic of arrays.</p><h2>What is an array in C and why should you use them?<span class="hx:absolute hx:-mt-20" id=what-is-an-array-in-c-and-why-should-you-use-them></span> 11<a href=#what-is-an-array-in-c-and-why-should-you-use-them class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>In C, an array is a way to store a fixed number of items of the same data type under a single name. Each data item of the array can be accessed by using a number called an “index” or “subscript”.</p><p>You might think, why do we need arrays to store multiple data types, when you can just declare normal variables? Let us take an example â suppose, you have been asked to write a program that takes in the temperature for the last 90 days and perform some processing on that data.</p><p>With enough time and patience, you can declare 90 integer variables (such as <code>a1</code>, <code>a2</code>, <code>a3</code> and so on) to store this information. However, processing the information in these 90 variables is nearly impossible. What if you wanted to know the average temperature between a range of days (such as the 10th and 25th day), where this range would be given by the user?</p><p>While you can’t do this with normal variables, you can use arrays to make such a program. You can store all the temperature values under a single variable like <code>a</code>, and then extract any set of values from it by using the index.</p><h2>Declaring and using an array in C<span class="hx:absolute hx:-mt-20" id=declaring-and-using-an-array-in-c></span> 12<a href=#declaring-and-using-an-array-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>To declare an array, you simply need to specify the data type of the array elements, the variable name and the array size. For example, if you want to declare an integer array with four elements, you’d use:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>4</span><span class=p>];</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>This statement allocates a contiguous block of memory for four integers and initializes all the values to 0. This is how it is laid out in memory:</p><p><img src=/wp-content/uploads/2019/11/array-init-1.png alt="Visualizing an array after it has been initalized" data-zoomable loading=lazy></p><p>Array indexes start from zero and end with (array size - 1). So for the above array, you can use the first element with <code>a[0]</code>, second element with <code>a[1]</code>, third element with <code>a[2]</code> and fourth (last) element with <code>a[3]</code>.</p><p>You can use the indexes to set or get specific values from the array. Here are a few examples:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>]</span> <span class=o>=</span> <span class=mi>10</span><span class=p>;</span> 13</span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>]</span> <span class=o>=</span> <span class=mi>20</span><span class=p>;</span> 14</span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>]</span> <span class=o>=</span> <span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>]</span> <span class=o>/</span> <span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>];</span> <span class=c1>// a[2] will be set to 20/10 = 2 15</span></span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>3</span><span class=p>]</span> <span class=o>=</span> <span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>]</span> <span class=o>-</span> <span class=mi>2</span><span class=p>;</span> <span class=c1>// a[3] will be set to 20-2 = 18 16</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:p
16ointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>After these changes, here is how the array will look like in memory:</p><p><img src=/wp-content/uploads/2019/11/Array-update.png alt="Visualizing an array after performing assignments." data-zoomable loading=lazy></p><p>Apart from the square brackets to indicate the index, array elements behave like normal variables. So, for example, you can print them by using:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=nf>printf</span><span class=p>(</span><span class=s>"%d %d %d %d</span><span class=se>\n</span><span class=s>"</span><span class=p>,</span> <span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>],</span> <span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>],</span> <span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>],</span> <span class=n>a</span><span class=p>[</span><span class=mi>3</span><span class=p>]);</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>You can see the full program in action below, or download it <a href=https://www.mycompiler.io/view/AdUNevW target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
16<script src=https://www.mycompiler.io/static/js/embedv2.js#id:HHKJh9a></script>
16</div><p>One important thing to note is that C does not enforce any array bounds checks, and accessing elements outside the maximum index will lead to “undefined behaviour”. So, in the above example, trying to get or set a value like <code>a[5]</code>Â can cause your program to crash or behave abnormally.</p><h2>Alternative ways to initialize arrays<span class="hx:absolute hx:-mt-20" id=alternative-ways-to-initialize-arrays></span> 17<a href=#alternative-ways-to-initialize-arrays class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>Previously, we’ve seen how to declare an array and set its elements. However, if you know the elements of the array, then there is an easier way to declare the array. For example, you want to declare an integer array with the values 10, 20, 30, 40, you can use the “initializer list” syntax:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>4</span><span class=p>]</span> <span class=o>=</span> <span class=p>{</span><span class=mi>10</span><span class=p>,</span> <span class=mi>20</span><span class=p>,</span> <span class=mi>30</span><span class=p>,</span> <span class=mi>40</span><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>This statement will automatically create an array of size 4, and initialize <code>a[0]</code> to 10, <code>a[1]</code> to 20 and so on. To confirm that this works, you can print the variables, just like we did in our previous program.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
17<script src=https://www.mycompiler.io/static/js/embedv2.js#id:AdUNevW></script>
17</div><p>You can also make an array that is bigger than the initializer list, like so:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>6</span><span class=p>]</span> <span class=o>=</span> <span class=p>{</span><span class=mi>10</span><span class=p>,</span> <span class=mi>20</span><span class=p>,</span> <span class=mi>30</span><span class=p>,</span> <span class=mi>40</span><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>In this case, the rest of the elements are initialized with zero. In our above example, elements from <code>a[0]</code> to <code>a[3]</code> will be initialized, whereas <code>a[4]</code> and <code>a[5]</code> will be set to zero. Again, you can easily verify this by writing a program:</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
17<script src=https://www.mycompiler.io/static/js/embedv2.js#id:BLreNEn></script>
17</div><p>You can also skip the array size when using initializer lists, like in the example below:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[]</span> <span class=o>=</span> <span class=p>{</span><span class=mi>10</span><span class=p>,</span> <span class=mi>20</span><span class=p>,</span> <span class=mi>30</span><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>The C compiler automatically guesses the size of the array from the size of the initializer list. So, the above example is the same as writing <code>int a[3] = {10, 20, 30};</code></p><p>However, you cannot skip both the size and the initializer list, and write <code>int a[];</code>. Arrays in C are of a fixed size, and so their size must be known when you create the array. Therefore, if you skip both of them, C cannot create the array, and this will lead to a compile-time error.</p><h2>Looping/iterating over an array in C<span class="hx:absolute hx:-mt-20" id=loopingiterating-over-an-array-in-c></span> 18<a href=#loopingiterating-over-an-array-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>Previously, we have learnt how you can use the individual elements of an array. Since the array indexes are integers starting from 0 to (array size - 1), you can use loops to visit all the elements of the array. This process of visiting all the elements of an array is also called “traversing an array”.</p><p>Here is a very simple example. It initializes an array and prints each array element and the index:</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
18<script src=https://www.mycompiler.io/static/js/embedv2.js#id:DQJEYcc></script>
18</div><p>In the <code>for</code> loop, the value of <code>i</code> starts with 0 (because we set <code>i = 0</code>) and ends at 3 (because the loop continues till <code>i < 4</code>). Inside the for loop, we print the value of <code>a[i]</code>. So, in the first iteration, we print the value of <code>a[0]</code>. In the second iteration, we print <code>a[1]</code>. In this way, we print all the elements of the array.</p><h2>Array program examples in C<span class="hx:absolute hx:-mt-20" id=array-program-examples-in-c></span> 19<a href=#array-program-examples-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>Now that we know the basics of an array, we will look at some basic programs that use arrays in C.</p><h3>Reading user-entered numbers into an array<span class="hx:absolute hx:-mt-20" id=reading-user-entered-numbers-into-an-array></span> 20<a href=#reading-user-entered-numbers-into-an-array class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>Let us begin with a simple program that reads five numbers into an array, and then prints them out. Here is the source code for the program, and you can download it <a href=https://www.mycompiler.io/view/09tME0B target=_blank rel=noopener>here</a>. (In this coding playground, you can change the input numbers via the “Input” tab.)</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
20<script src=https://www.mycompiler.io/static/js/embedv2.js#id:09tME0B></script>
20</div><p>We begin by initializing an array of five elements and, a variable <code>i</code> which we’ll use for the <code>for</code> loop.</p><p>In the first loop, we read numbers from the user and set it in the <code>a[i]</code> element. The elements of an array simply behave like regular variables. If you had an <code>int</code> variable named <code>p</code> and wanted to set the value of <code>p</code> from user input, you’d use <code>scanf("%d", &p)</code>. Similarly, here, to set the value of <code>a[i]</code> from user input, we used <code>scanf("%d", &a[i])</code>.</p><p>The second loop simply prints the variables one by one using <code>printf()</code>.</p><ins class="adsbygoogle bw-incontent" style=display:block data-ad-client=ca-pub-3394850521000379 data-ad-slot=2863733613></ins>
20<script>(adsbygoogle=window.adsbygoogle||[]).push({})</script>
20<h3>Linear search in an array<span class="hx:absolute hx:-mt-20" id=linear-search-in-an-array></span> 21<a href=#linear-search-in-an-array class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>Sometimes, you may need to search for an element in an array. For example, given an array <code>{10, 20, 30, 40}</code>, you may want to know if 30 is present in the array.</p><p>Linear search is a simple technique to search for an element. We iterate over every element of the array to check if it matches with the number we’re looking for.</p><p>Here is the source code for the program, and you can download it <a href=https://www.mycompiler.io/view/IwDQ9tc target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
21<script src=https://www.mycompiler.io/static/js/embedv2.js#id:IwDQ9tc></script>
21</div><p>In this program, we have declared an array <code>a</code>, the loop variable <code>i</code>, the element to search for <code>search</code>. We have also declared another variable <code>pos</code>, which keeps track of the array index where we found the element we were searching. We’ve initialized <code>pos</code> to -1; the reason for doing so will become clear later on.</p><p>Next, we read the elements of the array as well as the number to search for.</p><p>In the final loop, we’ve implemented the linear search logic. We iterate through each element of the array, and check if <code>a[i]</code> is equal to the value of <code>search</code>. If it is equal, we set the <code>pos</code> variable to the current index <code>i</code>, since we found the element at index <code>i</code>. Then, we break out of the loop, since we’ve found our element and we don’t need to process any further.</p><p>Then, we compare the value of <code>pos</code>. The minimum value of an array index can be 0. So, if we find the value of <code>pos</code> to be -1, this means there was no match and we’ll print a message like “30 was not found”. Otherwise, we’ll print a message such as “30 was found at position 2”.</p><h2>Two-dimensional (2D) arrays in C<span class="hx:absolute hx:-mt-20" id=two-dimensional-2d-arrays-in-c></span> 22<a href=#two-dimensional-2d-arrays-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>So far, we’ve looked at arrays where the element stores a simple data type like <code>int</code>. These arrays are sometimes called one-dimensional (1D) arrays.</p><p>Just as <code>int</code> or <code>float</code> are data types, an array is also a data type. Therefore, you can build an array who’s individual elements are 1D arrays. These kinds of arrays are called two-dimensional (2D) arrays.</p><p>To declare a 2D array, you need:</p><ul><li>the basic data type</li><li>the variable name</li><li>the size of the 1D arrays</li><li>the number of 1D arrays, which combined together make up the 2D array.</li></ul><p>Here is how you can declare a 2D array:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>][</span><span class=mi>4</span><span class=p>];</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>This statement allocates a contiguous block of memory. For our example, two 1D arrays combine together to form a 2D array, as you can see in the diagram below. Each 1D array can hold four integers. Also, just like 1D arrays, all the elements of the array are initialized to zero.</p><p><img src=/wp-content/uploads/2019/11/2d-array.png alt="Visualizing a two-dimensional array" data-zoomable loading=lazy></p><p>Now, let us see how we can access the elements of a 2D array. Since a 2D array consists of multiple 1D arrays, you need two indexes. This is because, you need to specify the index of the 1D array, and the specific element inside that 1D array.</p><p>So, if you want to access the first 1D array’s second element, you should use <code>a[0][1]</code>. Similarly, to access the second 1D array’s third element, you should use <code>a[1][2]</code> and so on.</p><p>A very common approach is to visualize 2D arrays like a table, where the first index is the row number and the second index is the column number. So, you can represent the above array as:</p><p><img src=/wp-content/uploads/2019/11/grid2.png alt="Visualizing a 2-D array as a table." data-zoomable loading=lazy></p><p>This allows us to talk about 2D arrays in a much easier way. To use the element at the first row, third column, you can use <code>a[0][2]</code>; to use the element at the second row, second column, use <code>a[1][1]</code> and so on.</p><h2>Initializing, using and looping over 2D arrays<span class="hx:absolute hx:-mt-20" id=initializing-using-and-looping-over-2d-arrays></span> 23<a href=#initializing-using-and-looping-over-2d-arrays class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>Apart from using two indexes, using 2D arrays is the same as using 1D arrays. A basic example of using the arrays is as follows:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>][</span><span class=mi>2</span><span class=p>];</span> 24</span></span><span class=line><span class=cl> 25</span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>][</span><span class=mi>0</span><span class=p>]</span> <span class=o>=</span> <span class=mi>10</span><span class=p>;</span> 26</span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>][</span><span class=mi>1</span><span class=p>]</span> <span class=o>=</span> <span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>][</span><span class=mi>0</span><span class=p>]</span> <span class=o>*</span> <span class=mi>10</span><span class=p>;</span>
26 <span class=c1>// a[0][1] will be set to 100 27</span></span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>][</span><span class=mi>0</span><span class=p>]</span> <span class=o>=</span> <span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>][</span><span class=mi>1</span><span class=p>]</span> <span class=o>/</span> <span class=mi>5</span><span class=p>;</span> <span class=c1>// a[1][0] will be set to 20 28</span></span></span><span class=line><span class=cl><span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>][</span><span class=mi>1</span><span class=p>]</span> <span class=o>=</span> <span class=n>a</span><span class=p>[</span><span class=mi>0</span><span class=p>][</span><span class=mi>1</span><span class=p>]</span> <span class=o>+</span> <span class=n>a</span><span class=p>[</span><span class=mi>1</span><span class=p>][</span><span class=mi>0</span><span class=p>];</span> <span class=c1>// a[1][1] will be set to 120 29</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>You can find the full program below, or download it <a href=https://www.mycompiler.io/view/04C5Tjr target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
29<script src=https://www.mycompiler.io/static/js/embedv2.js#id:04C5Tjr></script>
29</div><p>If you know the elements beforehand, you can also use the initializer list syntax that we discussed previously. For example, if you want to make the following array:</p><p><img src=/wp-content/uploads/2019/11/grid4.png alt data-zoomable loading=lazy>You can write it as follows:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>][</span><span class=mi>2</span><span class=p>]</span> <span class=o>=</span> <span class=p>{</span><span class=mi>20</span><span class=p>,</span> <span class=mi>10</span><span class=p>,</span> <span class=mi>40</span><span class=p>,</span> <span class=mi>60</span><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>For easier understanding, you can also group the elements of the initializer list by using braces. For example, to initialize a 2D array with two rows and three columns, you can write:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>2</span><span class=p>][</span><span class=mi>3</span><span class=p>]</span> <span class=o>=</span> <span class=p>{{</span><span class=mi>10</span><span class=p>,</span> <span class=mi>20</span><span class=p>,</span> <span class=mi>30</span><span class=p>},</span> <span class=p>{</span><span class=mi>40</span><span class=p>,</span> <span class=mi>50</span><span class=p>,</span> <span class=mi>60</span><span class=p>}};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>You can also loop over, print, or ask for input using <code>scanf()</code> as you would with 1D arrays. Here is an example of initializing an array, and then using <code>for</code> loops to print the elements. You can download the code <a href=https://www.mycompiler.io/edit/AM8ZbgC target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
29<script src=https://www.mycompiler.io/static/js/embedv2.js#id:AM8ZbgC></script>
29</div><p>In the above program, we have used nested <code>for</code> loops to print the elements of the array. This is because a 2D array has two indexes, which means that the first and second index has to be changed individually. Since the <code>printf()</code> call uses <code>a[i][j]</code>, so, the outer loop with variable <code>i</code> changes the first index, i.e. the row number. The inner loop with variable <code>j</code> changes the column number.</p><p>To understand this better, let us step through the loops. The outer loop starts with <code>i = 0</code>, and we get to the inner loop. The inner loop starts with <code>j = 0</code> and ends at <code>j = 2</code> (since <code>j < 3</code>), so we print <code>a[0][0]</code>, <code>a[0][1]</code> and <code>a[0][2]</code>. After this, the inner loop value increases to 1, and similarly, we print <code>a[1][0]</code>, <code>a[1][1]</code> and <code>a[1][2]</code>.</p><p>In this way, we print all the elements of the 2D array.</p><ins class="adsbygoogle bw-incontent" style=display:block data-ad-client=ca-pub-3394850521000379 data-ad-slot=1388614818></ins>
29<script>(adsbygoogle=window.adsbygoogle||[]).push({})</script>
29<h2>2D array program examples in C<span class="hx:absolute hx:-mt-20" id=2d-array-program-examples-in-c></span> 30<a href=#2d-array-program-examples-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>In this section, we’re going to look at some basic programs involving 2D arrays in C. Since 2D arrays can be visualized in the form of a table or matrix, all of our examples will revolve around the concept of using them for matrix operations.</p><h3>Reading user-entered numbers into a 2D array<span class="hx:absolute hx:-mt-20" id=reading-user-entered-numbers-into-a-2d-array></span> 31<a href=#reading-user-entered-numbers-into-a-2d-array class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>Before we look at more complex examples, let us first look at how to read numbers from the user into an array. Here’s the source code of the program, and you can download it <a href=https://www.mycompiler.io/view/4mjPLez target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
31<script src=https://www.mycompiler.io/static/js/embedv2.js#id:4mjPLez></script>
31</div><p>In the program, we begin by initializing an array of 10 rows and 10 columns, along with loop variables as well as variables that store the number of rows and columns requested by the user. Next, we ask the user to enter the number of rows and columns, and then we check if the number of rows and columns will fit into the 2D array. If it doesn’t fit, we print an error and exit the program.</p><p>Then, in the first nested <code>for</code> loop, we ask the user to enter the elements with <code>scanf()</code>. When printing the message “Enter row X, column Y”, we have used <code>i + 1</code> and <code>j + 1</code> ; this is because row/column numbers start from 1, but array indexes start from 0.</p><p>In the second nested <code>for</code> loop, we simply print the elements as we’ve seen in our previous example.</p><h3>Finding the transpose of a matrix<span class="hx:absolute hx:-mt-20" id=finding-the-transpose-of-a-matrix></span> 32<a href=#finding-the-transpose-of-a-matrix class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>When you “transpose” a matrix, you take every element at the ith row and jth column of the matrix, and put it in the jth row and ith column. The formula for transpose is as follows:</p><span class=katex-display><span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msup><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>01</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>02</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>10</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>11</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>12</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow><mi>T</mi></msup><mo>=</mo><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>10</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>01</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>11</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>02</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>12</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow></mrow><annotation encoding="application/x-tex">\begin{bmatrix} 33a_{00} & a_{01} & a_{02} \\ 34a_{10} & a_{11} & a_{12} \\ 35\end{bmatrix}^T = \begin{bmatrix} 36a_{00} & a_{10} \\ 37a_{01} & a_{11} \\ 38a_{02} & a_{12} \\ 39\end{bmatrix}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:2.6313em;vertical-align:-.95em></span><span class=minner><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span><span class=msupsub><span class=vlist-t><span class=vlist-r><span class=vlist style=height:1.6812em><span style=top:-3.9029em;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style=margin-right:.13889em>T</span></span></span></span></span></span></span></span><span class=mspace style=margin-right:.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:.2778em></span></span><span class=base><span class=strut style=height:3.6em;vertical-align:-1.55em></span><span class=minner><span class=mopen><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:2.05em><span style=top:-4.05em><span class=pstrut style=height:5.6em></span><span style=width:.667em;height:3.6em><svg width=".667em" height="3.6em" viewBox="0 0 667 3600"><path d="M403 1759V84H666V0H319V1759v0 1759h347v-84H403zm0 0V0H319V1759v0 1759h84z"/></svg></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:1.55em><span></span></span></span></span></span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:2.05em><span style=top:-4.21em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-3.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-1.81em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:1.55em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:2.05em><span style=top:-4.21em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-3.01em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-1.81em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:1.55em><span></span></span></span></span></span></span></span><span class=mclose><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:2.05em><span style=top:-4.05em><span class=pstrut style=height:5.6em></span><span style=width:.667em;height:3.6em><svg width=".667em" height="3.6em" viewBox="0 0 667 3600"><path d="M347 1759V0H0V84H263V1759v0 1759H0v84H347zm0 0V0H263V1759v0 1759h84z"/></svg></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:1.55em><span></span></span></span></span></span></span></span></span></span></span></span>
39<p>As you can see in this example, the size of a matrix also changes when you “transpose” it. A matrix of size <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mo>Ã</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">m \times n</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.6667em;vertical-align:-.0833em></span><span class="mord mathnormal">m</span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:.4306em></span><span class="mord mathnormal">n</span></span></span></span> changes to a matrix of size <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>Ã</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">n \times m</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.6667em;vertical-align:-.0833em></span><span class="mord mathnormal">n</span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:.4306em></span><span class="mord mathnormal">m</span></span></span></span>.</p><p>We can translate the above program into code quite easily. Suppose, the input array is <code>a</code> and the result array is <code>b</code>. Elements in <code>a[i][j]</code> will go into a <code>b[j][i]</code>. Here’s the source code for the program, and you can download it <a href=https://www.mycompiler.io/view/Cq9XaQC target=_blank rel=noopener>here</a>:</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
39<script src=https://www.mycompiler.io/static/js/embedv2.js#id:Cq9XaQC></script>
39</div><p>Just like our previous example, we declare the arrays are <code>a</code> and <code>b</code> respectively and have 10 rows and 10 columns to hold the input and result matrix. Next, we ask the user to enter the rows and columns. If it exceeds the size of the array, we print an error and exit.</p><p>Then, we ask the user to enter the numbers one by one and print the input matrix. After that, we iterate through the matrix using a nested <code>for</code> loop, and implement the logic where <code>a[i][j]</code> goes to <code>b[j][i]</code>.</p><p>As we discussed, the matrix size changes when you transpose it. In all the other loops, we were comparing <code>i < rows</code> and <code>j < cols</code>, but now since the size has changed, we should use <code>i < cols</code> and <code>j < rows</code>. So, in the last loop, we use this idea and print all the elements of the matrix.</p><h3>Adding two matrices<span class="hx:absolute hx:-mt-20" id=adding-two-matrices></span> 40<a href=#adding-two-matrices class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>In order to add two matrices, both must have the same number of rows and columns. The process of adding two matrices involves taking numbers from the same position of the matrix, adding them, and putting the result in the same position. The formula for matrix addition is as follows:</p><span class=katex-display><span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>01</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>02</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>10</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>11</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>12</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow><mo>+</mo><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>01</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>02</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>10</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>11</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>12</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow><mo>=</mo><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>00</mn></msub><mo>+</mo><msub><mi>b</mi><mn>00</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>01</mn></msub><mo>+</mo><msub><mi>b</mi><mn>01</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>02</mn></msub><mo>+</mo><msub><mi>b</mi><mn>02</mn></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>10</mn></msub><mo>+</mo><msub><mi>b</mi><mn>10</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>11</mn></msub><mo>+</mo><msub><mi>b</mi><mn>11</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>12</mn></msub><mo>+</mo><msub><mi>b</mi><mn>12</mn></msub></mrow></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow></mrow><annotation encoding="application/x-tex">\begin{bmatrix} 41a_{00} & a_{01} & a_{02} \\ 42a_{10} & a_{11} & a_{12} \\ 43\end{bmatrix} + \begin{bmatrix} 44b_{00} & b_{01} & b_{02} \\ 45b_{10} & b_{11} & b_{12} \\ 46\end{bmatrix} = \begin{bmatrix} 47a_{00} + b_{00} & a_{01} + b_{01} & a_{02} + b_{02} \\ 48a_{10} + b_{10} & a_{11} + b_{11} & a_{12} + b_{12} \\ 49\end{bmatrix}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span><span class=mspace style=margin-right:.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:.2778em></span></span><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=m
49ord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">02</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">12</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span></span></span></span></span>
49<p>So, if we have the two input matrices <code>a</code> and <code>b</code> and the result matrix <code>c</code>, then you can use <code>c[i][j] = a[i][j] + b[i][j]</code> in a loop to add the matrices. Here’s the source code for the program, and you can download it <a href=https://www.mycompiler.io/view/Fq1a1uN target=_blank rel=noopener>here</a>:</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
49<script src=https://www.mycompiler.io/static/js/embedv2.js#id:Fq1a1uN></script>
49</div><p>In this program, we have two input matrices <code>a</code> and <code>b</code> and one result matrix <code>c</code>, and each of them are of 10x10 size. First, we ask the user for the size of the matrices, and if they’re bigger than the arrays, we print an error and exit.</p><p>Otherwise, we ask the user to enter the values in the input matrix. Then, using a nested <code>for</code> loop, we implement the process of matrix addition, <code>c[i][j] = a[i][j] + b[i][j]</code>. Finally, we print all the three matrices.</p><h3>Multiplying two matrices<span class="hx:absolute hx:-mt-20" id=multiplying-two-matrices></span> 50<a href=#multiplying-two-matrices class=subheading-anchor aria-label="Permalink for this section"></a></h3><p>Let us assume we have two matrices. One is of size <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mo>Ã</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">m \times n</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.6667em;vertical-align:-.0833em></span><span class="mord mathnormal">m</span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:.4306em></span><span class="mord mathnormal">n</span></span></span></span> and the other one is <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mo>Ã</mo><mi>q</mi></mrow><annotation encoding="application/x-tex">p \times q</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.7778em;vertical-align:-.1944em></span><span class="mord mathnormal">p</span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:.625em;vertical-align:-.1944em></span><span class="mord mathnormal" style=margin-right:.03588em>q</span></span></span></span>. These matrices can be only multiplied if <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>p</mi></mrow><annotation encoding="application/x-tex">n = p</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.4306em></span><span class="mord mathnormal">n</span><span class=mspace style=margin-right:.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:.2778em></span></span><span class=base><span class=strut style=height:.625em;vertical-align:-.1944em></span><span class="mord mathnormal">p</span></span></span></span>. After multiplying, the result matrix will have a size of <span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mo>Ã</mo><mi>q</mi></mrow><annotation encoding="application/x-tex">m \times q</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:.6667em;vertical-align:-.0833em></span><span class="mord mathnormal">m</span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:.625em;vertical-align:-.1944em></span><span class="mord mathnormal" style=margin-right:.03588em>q</span></span></span></span>.</p><p>Now, to calculate the result in the ith row and jth column of the result matrix, take all the elements of the ith row of the first matrix, and multiply it with the corresponding value in the jth column. Then, take all these products and fill the sum in the result matrix. To see what we mean, let us take an example of how you can multiply two 2x2 matrices:</p><span class=katex-display><span class=katex><span class=katex-mathml><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>01</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>10</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>a</mi><mn>11</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow><mo>Ã</mo><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>00</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>01</mn></msub></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>10</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><msub><mi>b</mi><mn>11</mn></msub></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow><mo>=</mo><mrow><mo fence="true">[</mo><mtable rowspacing="0.16em" columnalign="center center" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>00</mn></msub><msub><mi>b</mi><mn>00</mn></msub><mo>+</mo><msub><mi>a</mi><mn>01</mn></msub><msub><mi>b</mi><mn>10</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>00</mn></msub><msub><mi>b</mi><mn>01</mn></msub><mo>+</mo><msub><mi>a</mi><mn>01</mn></msub><msub><mi>b</mi><mn>11</mn></msub></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>10</mn></msub><msub><mi>b</mi><mn>00</mn></msub><mo>+</mo><msub><mi>a</mi><mn>11</mn></msub><msub><mi>b</mi><mn>10</mn></msub></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><msub><mi>a</mi><mn>10</mn></msub><msub><mi>b</mi><mn>01</mn></msub><mo>+</mo><msub><mi>a</mi><mn>11</mn></msub><msub><mi>b</mi><mn>11</mn></msub></mrow></mstyle></mtd></mtr></mtable><mo fence="true">]</mo></mrow></mrow><annotation encoding="application/x-tex">\begin{bmatrix} 51a_{00} & a_{01} \\ 52a_{10} & a_{11} \\ 53\end{bmatrix} \times \begin{bmatrix} 54b_{00} & b_{01} \\
55b_{10} & b_{11} \\ 56\end{bmatrix} = \begin{bmatrix} 57a_{00} b_{00} + a_{01} b_{10} & a_{00} b_{01} + a_{01} b_{11} \\ 58a_{10} b_{00} + a_{11} b_{10} & a_{10} b_{01} + a_{11} b_{11} \\ 59\end{bmatrix}</annotation></semantics></math></span><span class=katex-html aria-hidden=true><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>Ã</span><span class=mspace style=margin-right:.2222em></span></span><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span><span class=mspace style=margin-right:.2778em></span><span class=mrel>=</span><span class=mspace style=margin-right:.2778em></span></span><span class=base><span class=strut style=height:2.4em;vertical-align:-.95em></span><span class=minner><span class="mopen delimcenter" style=top:0><span class="delimsizing size3">[</span></span><span class=mord><span class=mtable><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span><span class=arraycolsep style=width:.5em></span><span class=arraycolsep style=width:.5em></span><span class=col-align-c><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:1.45em><span style=top:-3.61em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">00</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span><span style=top:-2.41em><span class=pstrut style=height:3em></span><span class=mord><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">10</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:
592.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">01</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mspace style=margin-right:.2222em></span><span class=mbin>+</span><span class=mspace style=margin-right:.2222em></span><span class=mord><span class="mord mathnormal">a</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span><span class=mord><span class="mord mathnormal">b</span><span class=msupsub><span class="vlist-t vlist-t2"><span class=vlist-r><span class=vlist style=height:.3011em><span style=top:-2.55em;margin-left:0;margin-right:.05em><span class=pstrut style=height:2.7em></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">11</span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.15em><span></span></span></span></span></span></span></span></span></span><span class=vlist-s>â</span></span><span class=vlist-r><span class=vlist style=height:.95em><span></span></span></span></span></span></span></span><span class="mclose delimcenter" style=top:0><span class="delimsizing size3">]</span></span></span></span></span></span></span><p>The program that implements this logic is below. You can download it <a href=https://www.mycompiler.io/view/8Nxz0oa target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
59<script src=https://www.mycompiler.io/static/js/embedv2.js#id:8Nxz0oa></script>
59</div><p>In the program, we first declare 10x10 input and result matrices, along with some loop variables. Just like our previous programs, we ask the user for the sizes of the two matrices, and check if they are bigger than the 10x10 size. In addition, we check if the number of columns in the first matrix equals the number of rows in the second matrix. If any of these conditions fail, we print an error and exit the program.</p><p>Then, we ask the user to enter the numbers for both matrices. Once we’ve read all the numbers, we calculate the product with a nested loop. The two outer loops with variables <code>i</code> and <code>j</code> are used to move across the different elements of the result matrix. Initially, we set <code>c[i][j] = 0</code>. Now, we need to pick all the elements from the ith row of the first matrix and jth column from the second matrix. So, we use another loop with the variable <code>k</code> that helps us with this and multiplies and adds all the elements.</p><p>Finally, we display the input and result matrices.</p><ins class="adsbygoogle bw-incontent" style=display:block data-ad-client=ca-pub-3394850521000379 data-ad-slot=5302859384></ins>
59<script>(adsbygoogle=window.adsbygoogle||[]).push({})</script>
59<h2>Multidimensional arrays in C<span class="hx:absolute hx:-mt-20" id=multidimensional-arrays-in-c></span> 60<a href=#multidimensional-arrays-in-c class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>In the previous sections, we have seen 1D and 2D arrays. Any array with more than one dimension is a multidimensional array, and the concept can be extended to any number of dimensions, with the basics staying the same.</p><p>For example, a three dimensional (3D) array is a combination of multiple 2D arrays, and you can initialize a three dimensional array by using something like:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>3</span><span class=p>][</span><span class=mi>2</span><span class=p>][</span><span class=mi>2</span><span class=p>];</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>This statement creates a 3D array which is a combination of three 2x2 2D arrays. As with all arrays in C, memory allocation is contiguous and all the elements is initialized to zero. You can think of this array as follows:</p><p><img src=/wp-content/uploads/2019/11/3d1.png alt data-zoomable loading=lazy></p><p>To use any element of the array, you need to use three indexes. For example, writing <code>a[0][1][1]</code> will access the first 2D array’s second row and second column.</p><p>You can also use initializer lists with 3D arrays. Here is an example:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>int</span> <span class=n>a</span><span class=p>[</span><span class=mi>3</span><span class=p>][</span><span class=mi>2</span><span class=p>][</span><span class=mi>2</span><span class=p>]</span> <span class=o>=</span> <span class=p>{</span> 61</span></span><span class=line><span class=cl> <span class=p>{</span> 62</span></span><span class=line><span class=cl> <span class=p>{</span><span class=mi>10</span><span class=p>,</span> <span class=mi>20</span><span class=p>},</span> 63</span></span><span class=line><span class=cl> <span class=p>{</span><span class=mi>30</span><span class=p>,</span> <span class=mi>40</span><span class=p>},</span> 64</span></span><span class=line><span class=cl> <span class=p>},</span> 65</span></span><span class=line><span class=cl> <span class=p>{</span> 66</span></span><span class=line><span class=cl> <span class=p>{</span><span class=mi>1</span><span class=p>,</span> <span class=mi>2</span><span class=p>},</span> 67</span></span><span class=line><span class=cl> <span class=p>{</span><span class=mi>4</span><span class=p>,</span> <span class=mi>5</span><span class=p>}</span> 68</span></span><span class=line><span class=cl> <span class=p>},</span> 69</span></span><span class=line><span class=cl> <span class=p>{</span> 70</span></span><span class=line><span class=cl> <span class=p>{</span><span class=mi>3</span><span class=p>,</span> <span class=mi>5</span><span class=p>},</span> 71</span></span><span class=line><span class=cl>
71 <span class=p>{</span><span class=mi>7</span><span class=p>,</span> <span class=mi>11</span><span class=p>}</span> 72</span></span><span class=line><span class=cl> <span class=p>}</span> 73</span></span><span class=line><span class=cl><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>In the above example, the first 2D array’s first row is initialized to 10, 20, and the second row is initialized to 30, 40. Similarly, the second 2D array’s first row is initialized with 1, 2; the second row is initialized to 4, 5, and so on.</p><p>To iterate over a 3D array, you need to nest three <code>for</code> loops. The outer loop to change the outermost index. The loop inside it changes the middle index, and the innermost loop changes the last index. To see what we mean, take a look at the example below which initializes the array and prints all its elements. You can download it <a href=https://www.mycompiler.io/view/Ge9Q1qr target=_blank rel=noopener>here</a>.</p><div class=mycompiler-embed-wrap style="margin:1.25rem 0">
73<script src=https://www.mycompiler.io/static/js/embedv2.js#id:Ge9Q1qr></script>
73</div><p>Similarly, for a four dimensional array, you need to nest four <code>for</code> loops, and access it with four indexes, like <code>a[0][1][4][2]</code>.</p><h2>Strings in C: Arrays of Characters<span class="hx:absolute hx:-mt-20" id=strings-in-c-arrays-of-characters></span> 74<a href=#strings-in-c-arrays-of-characters class=subheading-anchor aria-label="Permalink for this section"></a></h2><p>In C, strings are simply an array of characters followed by the “null terminating character”. The null terminating character is a character with the integer value zero, and it is used to represent the end of a string.</p><p>Let us see how you can define a string. If you want to create a string with the content “world”, you can use the “initializer string” syntax. It looks like this:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>char</span> <span class=n>s</span><span class=p>[]</span> <span class=o>=</span> <span class=s>"world"</span><span class=p>;</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>The array <code>s</code> is six characters in length, because, apart from the characters “w”, “o”, “r”, “l”, “d”, there is also a null terminating character in the array.</p><p>You can also use the “initializer list” syntax to declare strings. However, in this case, the C compiler won’t put in the null terminating character for you, so you have to manually put it in. In C, <code>'\0'</code> is used to represent this character, so you would have to use:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>char</span> <span class=n>string</span><span class=p>[]</span> <span class=o>=</span> <span class=p>{</span><span class=sc>'w'</span><span class=p>,</span> <span class=sc>'o'</span><span class=p>,</span> <span class=sc>'r'</span><span class=p>,</span> <span class=sc>'l'</span><span class=p>,</span> <span class=sc>'d'</span><span class=p>,</span> <span class=sc>'\0'</span><span class=p>};</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>With the initializer list/string syntax, you can also set the array size manually if you wish to, but this isn’t necessary:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>char</span> <span class=n>string</span><span class=p>[</span><span class=mi>6</span><span class=p>]</span> <span class=o>=</span> <span class=p>{</span><span class=sc>'w'</span><span class=p>,</span> <span class=sc>'o'</span><span class=p>,</span> <span class=sc>'r'</span><span class=p>,</span> <span class=sc>'l'</span><span class=p>,</span> <span class=sc>'d'</span><span class=p>,</span> <span class=sc>'\0'</span><span class=p>};</span> 75</span></span><span class=line><span class=cl><span class=kt>char</span> <span class=n>string</span><span class=p>[</span><span class=mi>6</span><span class=p>]</span> <span class=o>=</span> <span class=s>"world"</span><span class=p>;</span> <span class=c1>// same as above 76</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:p
76ointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>However, there is one special case. If you write something like this:</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>char</span> <span class=n>string</span><span class=p>[</span><span class=mi>5</span><span class=p>]</span> <span class=o>=</span> <span class=s>"world"</span><span class=p>;</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>The C compiler will declare an array of five characters. However, since there is no space to fit the null terminating character, it will not add it to the string. However, for this example, you can’t put a size less than 5, and C compilers will throw an error.</p><div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"><div><div class=highlight><pre tabindex=0 class=chroma><code class=language-c data-lang=c><span class=line><span class=cl><span class=kt>char</span> <span class=n>string</span><span class=p>[</span><span class=mi>4</span><span class=p>]</span> <span class=o>=</span> <span class=s>"world"</span><span class=p>;</span> <span class=c1>// wrong! 77</span></span></span></code></pre></div></div><div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"><button class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50" title="Copy code" aria-label="Copy code" data-copied-label=Copied!><div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"></div><div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"></div></button></div></div><p>C provides multiple string manipulation functions in the header file <code>string.h</code>. However, this is outside the scope of arrays, so we will not discuss it here.</p></div><ins class="adsbygoogle bw-incontent" style=display:block data-ad-client=ca-pub-3394850521000379 data-ad-slot=2616276443></ins>
77<script>(adsbygoogle=window.adsbygoogle||[]).push({})</script>
77<div class=hx:mt-16></div><div class="hx:mb-8 hx:flex hx:items-center hx:border-t hx:pt-8 hx:border-gray-200 hx:dark:border-neutral-800 hx:contrast-more:border-neutral-400 hx:dark:contrast-more:border-neutral-400 hx:print:hidden"><a href=/how-to-add-remove-and-modify-users-in-linux/ title="How to Add, Remove and Modify Users in Linux" class="hx:flex hx:max-w-[50%] hx:items-center hx:gap-1 hx:py-4 hx:text-base hx:font-medium hx:text-gray-600 hx:transition-colors [word-break:break-word] hx:hover:text-primary-600 hx:dark:text-gray-300 hx:md:text-lg hx:ltr:pr-4 hx:rtl:pl-4"><svg class="hx:inline hx:h-5 hx:shrink-0 hx:ltr:rotate-180" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"/></svg>How to Add, Remove and Modify Users in Linux</a></div></main></article><aside class="bw-ad-sidebar hx:order-last hx:hidden hx:xl:block hx:shrink-0 hx:px-4 hx:print:hidden"><div class="hx:sticky hx:top-16 hx:pt-6 hx:overflow-y-auto hx:max-h-[calc(100vh-var(--navbar-height)-env(safe-area-inset-bottom))]"><ins class=adsbygoogle style=display:block;width:160px;height:600px data-ad-client=ca-pub-3394850521000379 data-ad-slot=3534606440></ins>
77<script>(adsbygoogle=window.adsbygoogle||[]).push({})</script>
77</div></aside></div><footer class="hextra-footer hx:bg-gray-100 hx:pb-[env(safe-area-inset-bottom)] hx:dark:bg-neutral-900 hx:print:bg-transparent"><div class="hextra-custom-footer hextra-max-footer-width hx:mx-auto hx:pl-[max(env(safe-area-inset-left),1.5rem)] hx:pr-[max(env(safe-area-inset-right),1.5rem)] hx:text-gray-600 hx:dark:text-gray-400"><div class="hx:flex hx:flex-wrap hx:items-center hx:gap-x-2 hx:gap-y-1 hx:py-4 hx:text-sm"><a class="hx:text-gray-600 hx:hover:text-gray-900" href=/tags/>Tags</a> 78<span class=hx:opacity-50 aria-hidden=true>|</span><a class="hx:text-gray-600 hx:hover:text-gray-900" href=/legal/#privacy>Privacy Policy</a> 79<span class=hx:opacity-50 aria-hidden=true>|</span><a class="hx:text-gray-600 hx:hover:text-gray-900" href=/legal/#tos>Terms of Service</a> 80<span class=hx:opacity-50 aria-hidden=true>|</span><span>Copyright © 2026 booleanworld.com. All rights reserved.</span></div></div></footer>
80<script defer src=/js/main.min.87785fd96557e898756188ba90bc3e2b22faddb10ae258b4d77bdb1c45dfef9e.js integrity="sha256-h3hf2WVX6Jh1YYi6kLw+KyL63bEK4li013vbHEXf754="></script>
80<script defer src=/js/flexsearch.433e941a8a573ebb9931fc16fc75266ab6b93f569ac2fb4f3dc66882e0416f4c.js integrity="sha256-Qz6UGopXPruZMfwW/HUmara5P1aawvtPPcZoguBBb0w=" crossorigin=anonymous></script>
80<script defer src=/en.search.min.423a47566acadc2f8b92bbb4ca95334500545268040247c4966cf47d6d0eb1b3.js integrity="sha256-QjpHVmrK3C+Lkru0ypUzRQBUUmgEAkfElmz0fW0OsbM="></script>
80<script defer src=/js/medium-zoom.min.6603320c02180d81b16dca4971f52763035ebc6ff18bd38729a47ff062be8d67.js integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin=anonymous></script>
80<script>document.addEventListener("DOMContentLoaded",()=>{if(!window.mediumZoom)return;const e=()=>document.documentElement.classList.contains("dark")?"rgba(17, 17, 17, 0.98)":"rgba(255, 255, 255, 0.98)",n=window.mediumZoom("[data-zoomable]",{background:e()}),t=document.createElement("style");t.textContent=` 81 .medium-zoom-overlay { 82 z-index: 1000; 83 } 84 .medium-zoom-image--opened { 85 z-index: 1001; 86 } 87 `,document.head.appendChild(t),new MutationObserver(()=>{n.update({background:e()})}).observe(document.documentElement,{attributes:!0,attributeFilter:["class"]})})</script>
87</body></html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.