how would i write a function that will search an array and display an item from the array?
Depending on what you were searching for, pass the array through a loop and compare the indexes. But what language?
c++, am taking the inroductory course...
That also depends on what array it is...
Care to show what your attempts have produced thus far?
I don't know the syntax for c++, but in general it would be a for loop in which you set a condition for the element that you are searching, for example for (int i = 0, i < array.length(), i++) { if (array[i] satisfies the condition) display array[i]; } Hope to have understood the question PS: sorry if my English is not very good
If it is characters or strings you would use array.lengthIO as well, or you can use sizeof(array).
I don't think that using sizeof directly will work in general. It will for character arrays, but not for integer arrays. Sizeof will return the number of bytes that are used by the variable. For a char array, this will usually be correct. But when using the sizeof on an integer array, the value will be four times bigger, at least on most platforms, than it should be. This is because an int usually takes up four bytes. You could try 'sizeof(array)/sizeof(int)' for an integer array. Or use a separate variable to remember the size of the array. Alternatively, you can use the vector class, but I guess that'll be left for future exercises. Anyway, the structure of your program looks good. Now try to fill in some specifics, like the array length, how to check the condition and how to print the array values.
Join our real-time social learning platform and learn together with your friends!