Skip to main content

Arrays in C++

Arrays in C++

C++ data types fall into different categories. One of these categories is the structured data type. This chapter and the next few chapters focus on structured data types.

Recall that a data type is called simple if variables of that type can store only one value at a time. Ex: char, short, int, long, float and bool

In contrast, in a structured data type, each data item is a collection of other data items. Ex: array, struct, union, class. Simple data types are building blocks of structured data types.

Pointer data type used to store memory address of computer.

let us consider the following problem. We want to write a C++ program that reads five numbers, finds their sum, average, and prints the numbers in reverse order.

int num1, num2, num3, num4, num5;

int sum=0, ave=0;

cout<<"Enter number1";          cin>>num1;     

cout<<"Enter number2";          cin>>num2;

cout<<"Enter number3";          cin>>num3;

cout<<"Enter number4";          cin>>num4;

cout<<"Enter number5";          cin>>num5;

sum =num1+num2+num3+num4+num5;

ave=sum/5;

cout<<"Sum ="<<sum<<"& Average ="<<ave;

cout<<"The number in reverse order ";

cout<<num5<<" "<<num4<<" "<<num3<<" "<<num2<<" "<<num1;

However, if you need to read and process 100 (or more) number, you would have to declare 100 variables and write many cin, cout statements. Thus, for large amounts of data, this type of program is not desirable.

So what?

An array is a variable that can store multiple values of the same type.
Examples: Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate variables, we can simply create an array:
        double grade[27]; //grade is an array that can hold a max of 27 elements
In C++, the size and type of arrays cannot be changed after its declaration.
Array available in the form of:  
  1. One Dimensional  
  2. Multidimensional 
One-dimensional array is an array in which the components are arranged in a list form.
Multi-dimensional array is an array in which the components are arranged in a table form.
The amount of storage required to hold an array is directly related to its type and size.
For a single-dimension array, the total size in bytes is computed as :
      total bytes = sizeof(base type) x size of array
For example,
          int age[16];
declares an array of 16 integers, named age. When the compiler sees this declaration, it sets aside enough memory to hold all 16 elements. Because each integer requires 4 bytes, this declaration sets aside 64 contiguous bytes of memory.
Note that: array element is stored in contiguous memory locations in index order.





Comments

Popular posts from this blog

Cryptocurrency: Types, Challenges and Applications

Cryptocurrency: Types, Challenges and Applications Cryptocurrency is a digital or virtual form of currency that utilizes cryptography to secure financial transactions, control the creation of additional units, and verify the transfer of assets. It is decentralized and operates on a technology called blockchain, which is a distributed ledger that records all transactions across multiple computers. Crypto is gaining popularity worldwide due to its potential to revolutionize traditional financial systems. In this response, I will discuss the types of cryptocurrencies, the challenges they face, and their applications. It is used to ensure the confidentiality, integrity, and authenticity of data. Cryptography techniques involve various algorithms and protocols that are designed to make data transmission secure and resistant to unauthorized access or modification. Some common cryptographic methods include encryption, decryption, hashing, and digital signatures. Cryptography plays a crucial r

Introduction to Microprocessor and Assembly Language

 Introduction to Microprocessor and Assembly Language Introduction: Microprocessors and assembly language are fundamental components of modern computing systems. Understanding the basics of microprocessors and assembly language is essential for anyone interested in computer architecture, low-level programming, or embedded systems. In this blog post, we will delve into the world of microprocessors, explore the concept of assembly language, and discuss their significance in the field of computer science. What is a Microprocessor? A microprocessor is a central processing unit (CPU) that serves as the brain of a computer. It is a small chip that performs arithmetic, logical, control, and input/output operations. Microprocessors are responsible for executing instructions and manipulating data within a computer system. They are present in a wide range of devices, from personal computers and smartphones to embedded systems and IoT devices. Evolution of Microprocessors: Microprocessors have co