Table of Contents
Declaring an array
Declare an array by specifying the size
The elements of the array and how to access them?
How to initialize an array in C programming?
Declaring an array by initializing elements
How to insert and print array elements ?
Example: C array
Example
Home Backend Development C++ Arrays in C/C++?

Arrays in C/C++?

Sep 20, 2023 pm 08:25 PM
programming array c language

Arrays in C/C++?

An array is a sequential collection of elements of the same type. Arrays are used to store collections of data, but it is often more useful to think of arrays as collections of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ... and number99, you can declare an array variable (such as numbers) and use numbers[0], numbers[1] and ..., numbers[99] to represent each variable. Specific elements in the array are accessed through indexing.

All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Declaring an array

Declaring an array requires specifying the type of elements and the number of required elements. An array is as follows -

type arrayName [ arraySize ];
Copy after login

Declare an array by specifying the size

This is called a one-dimensional array. arraySize must be an integer constant greater than zero, and the type can be any valid C data type. For example, to declare an array of 10 elements named balance and of type double, use the following statement -

double balance[10];
Copy after login

The elements of the array and how to access them?

A single piece of data in an array is an element of the array. You can use indexing to access elements of an array.

Suppose you declare an array tag as above. The first element is mark[0], the second element is mark[1], and so on. The array starts at index 0.

How to initialize an array in C programming?

Declare an array by specifying size and initializing elements

int mark[5] = {19, 10, 8, 17, 9};
Copy after login

Declaring an array by initializing elements

int mark[] = {19, 10, 8, 17, 9};
Copy after login

Here,

mark[0] is equal to 19; mark[1] is equal to 10; mark[2] is equal to 8; mark[3] is equal to 17; mark[4] is equal to 9
Copy after login

How to insert and print array elements ?

int mark[5] = {19, 10, 8, 17, 9}

// change 4th element to 9
mark[3] = 9;
// take input from the user and insert in third element
cin >> mark[2];
// take input from the user and insert in (i+1)th element
cin >> mark[i];
// print first element of the array
cout << mark[0];
// print ith element of the array
cout >> mark[i-1];
Copy after login

Example: C array

C program that uses an array to store and calculate the sum of 5 numbers entered by the user

Input

Enter 5 numbers:
3
4
5
4
2
Copy after login

Output

Sum = 18
Copy after login

Example

#include <iostream>
using namespace std;

int main() {
   int numbers[5], sum = 0;
   cout << "Enter 5 numbers: ";
   for (int i = 0; i < 5; ++i) {
      cin >> numbers[i];
      sum += numbers[i];
   }
   cout << "Sum = " << sum << endl;
   return 0;
}
Copy after login

The above is the detailed content of Arrays in C/C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Designated initializer in C language Designated initializer in C language Sep 01, 2023 am 08:49 AM

In the C90 standard, we have to initialize the array in a fixed order, such as initializing the index at position 0, 1, 2, etc. Starting with the C99 standard, they introduced designated initialization functionality in C. Here we can initialize elements in random order. Initialization can be done using array indexes or structure members. This extension is not implemented in GNUC++. If we specify some indexes and enter some values, then it will look like this - intarr[6]={[3]=20,[5]=40};orintarr[6]={[3]20,[5 ]40};This is equivalent to: intarr[6]={0,0,0,20,0,40};We can also put some range of elements using the following syntax

Introduction to priority queue in C/C++ Introduction to priority queue in C/C++ Sep 13, 2023 pm 05:21 PM

A priority queue is a queue in which elements are inserted or removed based on the priority assigned to them, where priority is an integer value in the range 0-10, where 0 represents the element with the highest priority and 10 represents the element with the highest Priority element The element with the lowest priority. Implementing a priority queue follows two rules: data or elements with the highest priority will be executed before data or elements with the lowest priority. If two elements have the same priority, they will be executed in the order they were added to the list. There are various data structures available that can be used to implement priority queues such as stacks, queues, and linked lists. In this article, we will explain queue data structure. There are two ways to implement priority queue eg - maintain multiple in a single array

In C language, array post-increment and front-increment In C language, array post-increment and front-increment Aug 30, 2023 pm 04:57 PM

Question: Use C program to explain the concepts of post-increment and pre-increment of arrays. Solution Increment Operator (++) - There are two types of increment operators used to increase the value of a variable by 1 - pre-increment and post-increment. In prepended increment, the increment operator is placed before the operand, and the value is incremented first and then the operation is performed. eg:z=++a;a=a+1z=a The increment operator is placed after the operand in the post-increment operation, and the value will increase after the operation is completed. eg:z=a++;z=aa=a+1 Let us consider an example of accessing a specific element in a memory location by using pre-increment and post-increment. Declare an array of size 5 and perform compile-time initialization. Afterwards try assigning the pre-increment value to variable 'a'. a=++arr[1]

In C language, variable length parameters of macros In C language, variable length parameters of macros Aug 27, 2023 pm 10:49 PM

We know that functions can be defined using variable length parameters in C language. For this we need to use ellipses (…). Similarly, in macros, we can also use variable length parameters. Here, too, we need to include the ellipses. ‘__VA_ARGS__’ is used to handle variable length arguments. The concatenation operator ‘##’ is used to concatenate variadic parameters. In this example, the macro accepts variable-length arguments, just like the printf() or scanf() functions. In this macro, we will print the file name, line number, and error message. The first parameter is pr. It is used to confirm

In Java, how to add new elements to an array? In Java, how to add new elements to an array? Jan 03, 2024 pm 03:30 PM

Adding new elements to an array is a common operation in Java and can be accomplished using a variety of methods. This article will introduce several common methods of adding elements to an array and provide corresponding code examples. 1. A common way to use a new array is to create a new array, copy the elements of the original array to the new array, and add new elements at the end of the new array. The specific steps are as follows: Create a new array whose size is 1 larger than the original array. This is because a new element is being added. Copy the elements of the original array to the new array. Add to the end of the new array

Rearrange an array so that arr becomes arr] and only use O(1) extra space, implemented in C++ Rearrange an array so that arr becomes arr] and only use O(1) extra space, implemented in C++ Aug 28, 2023 am 11:53 AM

We get an array of positive integer type, say, arr[] of any given size, such that the element value in the array should be greater than 0 but less than the size of the array. The task is to rearrange an array only by changing arr[i] to arr[arr[i]] in the given O(1) space and print the final result. Let’s look at various input and output scenarios for this situation − Input − intarr[] = {032154} Output − Array before arrangement: 032154 Rearrange the array so that arr[i] becomes arr[arr[i]], And has O(1) extra space: 012345 Explanation − We are given an integer array of size 6, and all elements in the array have values ​​less than 6. Now we will rearrange

Robin Li mentioned the 'busy traffic' again, saying that AI will create more opportunities for human beings Robin Li mentioned the 'busy traffic' again, saying that AI will create more opportunities for human beings May 25, 2023 pm 10:05 PM

Two months ago, the painting "Car, Water, Horse, Dragon" created by Baidu Wenxinyiyan when it was tested by the first batch of users became popular on the Internet overnight. "Car", "Water", "Horse", The picture of "Dragon", four unrelated things stacked together, is indeed a bit innocent. Then almost overnight, Wen Xinyiyan completed the iteration and successfully interpreted the profound Chinese idiom "traffic and traffic" with pictures. Two months later, on May 18, when Robin Li, founder, chairman and CEO of Baidu, explained to the audience what "generative AI" was, he once again mentioned the "busy traffic", and what emerged from the joke was something more. Much confidence and calmness. Yes, in the past two months, Wen Xinyiyan's academic "scores" have increased almost linearly. "Baiduwen

Basic operations and usage of arrays in PHP Basic operations and usage of arrays in PHP Jun 28, 2023 pm 08:02 PM

Basic operations and usage of arrays in PHP 1. Overview Array is a very important data type in PHP. It can be used to store multiple values, and these values ​​can be accessed through indexes or keys. Arrays have rich operations and usage methods in PHP. This article will introduce in detail the basic operations and usage methods of arrays in PHP. 2. Create arrays In PHP, you can create arrays in two ways: countable arrays and associative arrays. Creating a Countable Array A countable array is an array that is arranged in order and indexed numerically

See all articles