Sequential Search array for loop condition
If length of array in sequential search is not given and there is a lot of out of order indexing,
- how many times we should execute the for loop?
- what should be the condition of for loop?
I counted indexing again and again.
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < 5; i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
For loop Condition for unknown length array in sequential search
c++ arrays
add a comment |
If length of array in sequential search is not given and there is a lot of out of order indexing,
- how many times we should execute the for loop?
- what should be the condition of for loop?
I counted indexing again and again.
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < 5; i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
For loop Condition for unknown length array in sequential search
c++ arrays
1
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
5
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)sizeof array / sizeof array[0]
.
– Some programmer dude
Jan 2 at 4:51
main
must have a return type and it must beint
.
– user10605163
Jan 2 at 4:51
add a comment |
If length of array in sequential search is not given and there is a lot of out of order indexing,
- how many times we should execute the for loop?
- what should be the condition of for loop?
I counted indexing again and again.
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < 5; i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
For loop Condition for unknown length array in sequential search
c++ arrays
If length of array in sequential search is not given and there is a lot of out of order indexing,
- how many times we should execute the for loop?
- what should be the condition of for loop?
I counted indexing again and again.
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < 5; i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
For loop Condition for unknown length array in sequential search
c++ arrays
c++ arrays
edited Jan 2 at 6:27
William Miller
1,405217
1,405217
asked Jan 2 at 4:47
Sami UllahSami Ullah
11
11
1
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
5
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)sizeof array / sizeof array[0]
.
– Some programmer dude
Jan 2 at 4:51
main
must have a return type and it must beint
.
– user10605163
Jan 2 at 4:51
add a comment |
1
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
5
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)sizeof array / sizeof array[0]
.
– Some programmer dude
Jan 2 at 4:51
main
must have a return type and it must beint
.
– user10605163
Jan 2 at 4:51
1
1
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
5
5
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)
sizeof array / sizeof array[0]
.– Some programmer dude
Jan 2 at 4:51
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)
sizeof array / sizeof array[0]
.– Some programmer dude
Jan 2 at 4:51
main
must have a return type and it must be int
.– user10605163
Jan 2 at 4:51
main
must have a return type and it must be int
.– user10605163
Jan 2 at 4:51
add a comment |
1 Answer
1
active
oldest
votes
Using sizeof(array)/sizeof(array[0])
will give you the number of elements in the array,
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
Note that using namespace std;
is considered bad practice`
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f54001318%2fsequential-search-array-for-loop-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using sizeof(array)/sizeof(array[0])
will give you the number of elements in the array,
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
Note that using namespace std;
is considered bad practice`
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
add a comment |
Using sizeof(array)/sizeof(array[0])
will give you the number of elements in the array,
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
Note that using namespace std;
is considered bad practice`
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
add a comment |
Using sizeof(array)/sizeof(array[0])
will give you the number of elements in the array,
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
Note that using namespace std;
is considered bad practice`
Using sizeof(array)/sizeof(array[0])
will give you the number of elements in the array,
#include <iostream>
using namespace std;
int main() {
int array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
Note that using namespace std;
is considered bad practice`
edited Jan 2 at 5:44
answered Jan 2 at 5:12
William MillerWilliam Miller
1,405217
1,405217
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
add a comment |
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
"using namespace std; is considered bad practice" That's still an opinion.
– Sid S
Jan 2 at 5:43
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
@SidS you're correct. However it is not my opinion - I am simply relaying it
– William Miller
Jan 2 at 5:45
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.
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%2f54001318%2fsequential-search-array-for-loop-condition%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
1
for (i = 0; i < sizeof array / sizeof *array; i++)
– Sid S
Jan 2 at 4:50
5
Possible duplicate of How do I find the length of an array?
– user10605163
Jan 2 at 4:50
Arrays never have an unknown length, all arrays will have their size fixed at compilation time. There is a (relatively) easy "trick" to get the size of an array (a proper array, not one that has decayed to a pointer): Dividing the size of the whole array with the size of a single element. Like (in your case)
sizeof array / sizeof array[0]
.– Some programmer dude
Jan 2 at 4:51
main
must have a return type and it must beint
.– user10605163
Jan 2 at 4:51