Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign a multiple numbers to an array

I have a list of number : -1.5 , – 1 , -0,5 , … , 100.
I want to assign them to an array.
E.g :

a[0] = -1.5;
a[1] = -1;
....
a[202] = 100;

Can you show me how to do it ?
Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to initialize array with several values, you can use this:

int a[4] = {1, 2, 3, 4};

But it should not be used, if you have a lot of numbers. If you need to assign list of numbers, that you stated in question, you should describe them with mathmetical expression in cycle:

for(int i = 0; i < 203; i++)
{
    a[i] = -1.5 + 0.5 * i;
}

If you want to assign array with list of values, that can’t be described with mathematical expression, you should read them from file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading