473,233 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

Initializing Int Array With a Non-Zero Value

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.

Truly Yours, Simon Dexter

Jul 23 '05 #1
13 26875
<si******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com
Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?
Thank You Very Much.

Truly Yours, Simon Dexter


Int is not int. C++ is case sensitive.

The only way to do this at the point of declaration is to repeat the number
as many times as you need it, e.g.,

int array[5] = {9,9,9,9,9};

Alternatively, you can do it in a loop after the declaration.

Vectors allow you to avoid repeating the number:

std::vector<int> array(5, 9);
--
John Carson

Jul 23 '05 #2
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:
Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero number?
int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong

-- OR --

int intArr[n1]; // declaration
intArr[0] = x1; // initialization

It all depends on how far you want to go. You can give it a completer
list in the first example if you want. Or, you can use a for loop
that runs through the array index until it has filled the last
element, using the second statement inside the loop, substituting your
index variable for the zero and applying whatever number you
application feels is appropriate for the array.


Thank You Very Much.


You're welcome.

Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."
Jul 23 '05 #3
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:if********************************@4ax.com
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:
Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?


int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong


Your code has the effect of initializing the first element of the array to
x1 and the rest to zero. You also can't use pointer values without a cast.
--
John Carson

Jul 23 '05 #4
>> how to initialize an int array with a non-zero number?
John Carson .....
first element of the array to x1 and the rest to zero

if we write, int a[20]; it leaves the whole array uninintialized. It
doesn't initalize array with 0's at all. after all it is c++, not java
or vb.
if we write int a[20]={ 1,2};
then it will initialize only first two elements, and leave all others
uninitialized.
Still if you want to initialize an array with 0's, you have to write it
expilicitly.
int a[3]={0,0,0}; or use std::vector, std::vector<int> a(size,0);
The answer is whether u want to initialize array with zero or non-zero
value , you have to explicitly initialize them.

Jul 23 '05 #5
> Does anyone know how to initialize an int array with a non-zero number?

did anyone mention memset?

like, int * pi = new int (123);
memset(pi, <some byte here>, 123*sizeof(int));

pity you'd have pretty limited initial values set to choose from.

Jul 23 '05 #6
On Wed, 13 Jul 2005 11:44:15 +0400, upashu2 <up*****@rediffmail.com> wrote:
how to initialize an int array with a non-zero number?
John Carson .....
first element of the array to x1 and the rest to zero

if we write, int a[20]; it leaves the whole array uninintialized. It
doesn't initalize array with 0's at all. after all it is c++, not java
or vb.
if we write int a[20]={ 1,2};
then it will initialize only first two elements, and leave all others
uninitialized.


This is wrong.

[dcl.init.aggr] 8.5.1 Aggregates
....
7 If there are fewer initializers in the list than there are members in
the aggregate, then each member not explicitly initialized shall be
value-initialized (8.5).
[Example:
struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an
expression of the form int(), that is, 0. ]

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #7
On Wed, 13 Jul 2005 08:46:19 +0400, <si******@yahoo.com> wrote:
Does anyone know how to initialize an int array with a non-zero number?


Just iterate over the array and initialize its members with any values you
like.

The standard library provides some basic function templates for filling
arrays as well as ranges:

fill/fill_n
generate

Example:

int a[10];
fill_n(a, sizeof(a) / sizeof(*a), 1); // fill with ones

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #8
<ma************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
Does anyone know how to initialize an int array with a non-zero
number?
did anyone mention memset?

like, int * pi = new int (123);


That should be

int *pi = new int[123];
memset(pi, <some byte here>, 123*sizeof(int));
This does byte by byte initialisation, which is viable for the very small
fraction of integers which have the same number in each byte.
pity you'd have pretty limited initial values set to choose from.


Small and obsure. You cannot do this:

memset(pi, 5, 123*sizeof(int));

and initialise all integers to 5. You initialise all integers to whatever
number consists of a 5 in each byte.

--
John Carson

Jul 23 '05 #9
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc****************@netspace.net.au> did courageously avow:
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:if********************************@4ax.com
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:
Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?


int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong


Your code has the effect of initializing the first element of the array to
x1 and the rest to zero. You also can't use pointer values without a cast.


Where is this pointer you speak of?
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop. Why are you centering on
one item and not the whole post?
Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."
Jul 23 '05 #10
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:72********************************@4ax.com
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc****************@netspace.net.au> did courageously avow:
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:if********************************@4ax.com
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong
Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.


Where is this pointer you speak of?


You say: "x1 can be any legitimate value; number, char, pointer to another
array, etc". Thus the "pointer to another array" is the pointer that I speak
of. x1 cannot be a pointer without using a cast.
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop. Why are you centering on
one item and not the whole post?


The OP asked how to initialize an array with a non-zero number. I took this
to mean initialize the *whole* array with a non-zero number and so I took
your answer to be a claim that

int intArr[n1] = {x1};

initialized each element in the array to x1. The fact that you also gave
alternative methods of initialization was not inconsistent with this
interpretation.

If my interpretation was incorrect, I apologize.

--
John Carson

Jul 23 '05 #11


upashu2 wrote:
how to initialize an int array with a non-zero number?
John Carson .....
first element of the array to x1 and the rest to zero
if we write, int a[20]; it leaves the whole array uninintialized. It
doesn't initalize array with 0's at all. after all it is c++, not java
or vb.
That's true, but not what John presented. He initialized the first
value. The rest of the array will be default initialized.
if we write int a[20]={ 1,2};
then it will initialize only first two elements, and leave all others
uninitialized.
You are incorrect. The rest of the array will be filled with 0.
Still if you want to initialize an array with 0's, you have to write it
expilicitly.


Wrong.


Brian

Jul 23 '05 #12
[some further thoughts]

"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:72********************************@4ax.com
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc****************@netspace.net.au> did courageously avow:
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:if********************************@4ax.com
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong
Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.


Where is this pointer you speak of?
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop.


You didn't say at *any* point in your post that

int intArr[n1] = {x1};

would set all elements after the first to zero, so it was surely worth
pointing it out so the OP would know what to expect.
Why are you centering on one item and not the whole post?


I was not writing a review. It is perfectly legitimate to focus on the one
part of a post that is in need of clarification.

--
John Carson

Jul 23 '05 #13
On Thu, 14 Jul 2005 03:25:54 +1000, "John Carson"
<jc****************@netspace.net.au> did courageously avow:
[some further thoughts]

"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:72********************************@4ax.com
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc****************@netspace.net.au> did courageously avow:
"Rick N. Backer" <ke********@NsOhSaPw.cAaM> wrote in message
news:if********************************@4ax.com
On 12 Jul 2005 21:46:19 -0700, si******@yahoo.com did courageously
avow:

> Hi, Everyone!
>
> Does anyone know how to initialize an int array with a non-zero
> number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong

Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.
Where is this pointer you speak of?
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop.


You didn't say at *any* point in your post that

int intArr[n1] = {x1};

would set all elements after the first to zero, so it was surely worth
pointing it out so the OP would know what to expect.


I have no problem and readily concede that point and, truthfully,
hadn't considered that aspect. I may have boiled the question down to
much in trying to simplify it and my response.
Why are you centering on one item and not the whole post?


I was not writing a review. It is perfectly legitimate to focus on the one
part of a post that is in need of clarification.


Oh how I hate first encounters :-). I am used to people only
applying to one point and will concede this also and stand now
unoffended. I am just used to people interjecting in line and
returning the original document intact so context of the discussion
can be maintained. In this instance I ass-u-me'd, incorrectly, that
you hadn't paid attention to the rest of the post. My apologies.
Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
2
by: Andrew Ward | last post by:
The following program compiles and runs fine on my compiler (vc7.1): #include <memory> using namespace std; class X {}; auto_ptr<X> foo() {
3
by: Bill Pursell | last post by:
I've found myself wanting to do this: int *x = {1,2,3,4,5}; Obviously, I can't do that. I can certainly non-portably hack it like int *x = (int *)"\x01\x00\x00\x00\x02\x00...", but that's...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
6
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.