Computer Science, asked by amjadalishba946, 4 months ago

You are supposed to write a C++ program where you have to declare an array of integer having size 10. Input in array after you have taken input display the number that is appeared most in array. If user enters following data
20 3 40 50 20 3 3 5 3 121
After taking input output should be like following user can enter any data in array.
Most appeared number in array is 3 and is appeared 4 times in array.

Answers

Answered by ayushupadhyay1701
0

Arrays in C++

In simple English, array means collection. In C++ also, an array is a collection of similar types of data. eg.- an array of int will contain only integers, an array of double will contain only doubles, etc.

Why Array?

Suppose we need to store the marks of 50 students in a class and calculate the average marks. Declaring 50 separate variables will do the job but no programmer would like to do so. And here comes the array in action.

How to declare an array

datatype array_name [ array_size ];

For example, take an integer array 'n'.

int n[6];

n[ ] is used to denote an array named 'n'.

So, n[6] means that 'n' is an array of 6 integers. Here, 6 is the size of the array i.e., there are 6 elements of array 'n'.

Giving array size i.e. 6 is necessary because the compiler needs to allocate space to that many integers. The compiler determines the size of an array by calculating the number of elements of the array.

Here 'int n[6]' will allocate space to 6 integers.

We can also declare an array by another method.

int n[ ] = { 2,3,15,8,48,13 };

In this case, we are declaring and assigning values to the array at the same time. Hence, no need to specify the array size because the compiler gets it from { 2,3,15,8,48,13 }.

Following is the pictorial view of the array.

element 2 3 15 8 48 13

index 0 1 2 3 4 5

0,1,2,3,4 and 5 are the indices. It is like these are the identities of 6 different elements of the array. Index starts from 0. So, the first element has index 0. We access the elements of an array by writing array_name[index].

Index of an array starts with 0.

Here,

n[0] is 2

n[1] is 3

n[2] is 15

n[3] is 8

n[4] is 48

n[5] is 13

Initializing an array

By writing int n[ ]={ 2,4,8 }; , we are initializing the array.

But when we declare an array like int n[3]; , we need to assign the values to it separately. Because 'int n[3];' will definitely allocate the space of 3 integers in the memory but there are no integers in that.

To assign values to the array, assign a value to each of the element of the array.

n[0] = 2;

n[1] = 4;

n[2] = 8;

It is just like we are declaring some variables and then assigning the values to them.

int x,y,z;

x=2;

y=4;

z=8;

Similar questions