Sequential Search array for loop condition












0















If length of array in sequential search is not given and there is a lot of out of order indexing,




  1. how many times we should execute the for loop?

  2. 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










share|improve this question




















  • 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 be int.

    – user10605163
    Jan 2 at 4:51
















0















If length of array in sequential search is not given and there is a lot of out of order indexing,




  1. how many times we should execute the for loop?

  2. 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










share|improve this question




















  • 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 be int.

    – user10605163
    Jan 2 at 4:51














0












0








0








If length of array in sequential search is not given and there is a lot of out of order indexing,




  1. how many times we should execute the for loop?

  2. 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










share|improve this question
















If length of array in sequential search is not given and there is a lot of out of order indexing,




  1. how many times we should execute the for loop?

  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 be int.

    – user10605163
    Jan 2 at 4:51














  • 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 be int.

    – 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












1 Answer
1






active

oldest

votes


















0














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`






share|improve this answer


























  • "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











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
});


}
});














draft saved

draft discarded


















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









0














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`






share|improve this answer


























  • "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
















0














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`






share|improve this answer


























  • "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














0












0








0







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`






share|improve this answer















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`







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • "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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas