How to set the last element of an array as a certain number?
up vote
2
down vote
favorite
Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?
int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
c
add a comment |
up vote
2
down vote
favorite
Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?
int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
c
3
Set your loop condition toi < 4
and manually setn[4] = 100;
outside of your loop
– Govind Parmar
Nov 19 at 21:11
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?
int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
c
Say I have an array that sets values for 5 elements. How do I make the last one to print out as 100?
int n[5];
for(i=0; i<5; i++){
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
c
c
asked Nov 19 at 21:10
Henry Gibbs
224
224
3
Set your loop condition toi < 4
and manually setn[4] = 100;
outside of your loop
– Govind Parmar
Nov 19 at 21:11
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31
add a comment |
3
Set your loop condition toi < 4
and manually setn[4] = 100;
outside of your loop
– Govind Parmar
Nov 19 at 21:11
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31
3
3
Set your loop condition to
i < 4
and manually set n[4] = 100;
outside of your loop– Govind Parmar
Nov 19 at 21:11
Set your loop condition to
i < 4
and manually set n[4] = 100;
outside of your loop– Govind Parmar
Nov 19 at 21:11
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:
int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
Note that since your for
loop no longer asks for a value for n[4]
from the user, it doesn't matter whether the line n[4] = 100;
comes before or after your loop.
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
add a comment |
up vote
1
down vote
In general, I would solve this problem like this:
#define ARRAY_SIZE 5
void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}
This will work for any size array.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:
int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
Note that since your for
loop no longer asks for a value for n[4]
from the user, it doesn't matter whether the line n[4] = 100;
comes before or after your loop.
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
add a comment |
up vote
1
down vote
accepted
You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:
int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
Note that since your for
loop no longer asks for a value for n[4]
from the user, it doesn't matter whether the line n[4] = 100;
comes before or after your loop.
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:
int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
Note that since your for
loop no longer asks for a value for n[4]
from the user, it doesn't matter whether the line n[4] = 100;
comes before or after your loop.
You can rewrite your loop to not scan for the final element in your array, and fill that out with the hard-coded value of 100 in your code:
int n[5];
n[4] = 100;
for(i = 0; i < 4; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
Note that since your for
loop no longer asks for a value for n[4]
from the user, it doesn't matter whether the line n[4] = 100;
comes before or after your loop.
answered Nov 19 at 21:22
Govind Parmar
6,69653053
6,69653053
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
add a comment |
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
1
1
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
Thank you very much.
– Henry Gibbs
Nov 19 at 21:25
add a comment |
up vote
1
down vote
In general, I would solve this problem like this:
#define ARRAY_SIZE 5
void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}
This will work for any size array.
add a comment |
up vote
1
down vote
In general, I would solve this problem like this:
#define ARRAY_SIZE 5
void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}
This will work for any size array.
add a comment |
up vote
1
down vote
up vote
1
down vote
In general, I would solve this problem like this:
#define ARRAY_SIZE 5
void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}
This will work for any size array.
In general, I would solve this problem like this:
#define ARRAY_SIZE 5
void myFunc()
{
int n[ARRAY_SIZE];
int i;
for (i=0; i<ARRAY_SIZE-1; i++)
{
printf("Please enter value %d: n",i+1);
scanf(" %d", &n[i]);
}
// now, i == ARRAY_SIZE-1
n[i] = 100;
}
This will work for any size array.
answered Nov 19 at 21:27
yano
1,424720
1,424720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382675%2fhow-to-set-the-last-element-of-an-array-as-a-certain-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Set your loop condition to
i < 4
and manually setn[4] = 100;
outside of your loop– Govind Parmar
Nov 19 at 21:11
@GovindParmar under int n[5] I'd write n[4]=100;
– Henry Gibbs
Nov 19 at 21:15
@GovindParmar Anyway you can write that as an answer so I can give your props?
– Henry Gibbs
Nov 19 at 21:20
@danglingpointer Wouldn't that be out of bounds?
– Osiris
Nov 19 at 21:25
@danglingpointer looks like a good way to get yourself shot in the foot.
– SergeyA
Nov 19 at 21:31