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

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 hav...

Monetizing Your YouTube Channel in Ethiopia: A Quick Guide

Do Ethiopian YouTubers Earn Money from Their Channels? Ethiopia is not officially included in the list of countries where YouTube channels can be monetized. As a result, many Ethiopian YouTubers feel compelled to misrepresent their location by claiming to be based in another qualifying region. Unfortunately, currently, it seems that they have no alternative options for monetization. Despite these challenges, Ethiopian YouTubers still have the opportunity to earn money from their channels through YouTube's monetization program. This program, known as the YouTube Partner Program, allows content creators to monetize their videos by displaying ads. In return, they receive a portion of the revenue generated from those ads. To be eligible for monetization, Ethiopian YouTubers, like creators from other countries, need to meet certain requirements set by YouTube. These requirements include having at least 1,000 subscribers and 4,000 watch hours in the past 12 months, adhering to YouTube...