-
array program
I am doing my array program and i have to use a linear funct and i am all screwed up help?!
int linearSearch(const int array[], int key, int size){
for (int n=0; n<size; n++){
if(array[n]==key){
return array[n];
n++;}}
return -1;}
int main(int argc, char* argv[])
{const int size=20;
int numbers[size];
int numbs[size];
int i,j;
for(i=0; i<size; i++){
cout<<"Enter a number: "<<flush;
cin>>numbers[i];}
cout <<"Here is your array: \n";
for(i=0; i<size; i++){
cout<<numbers[i]<<" ";}
cout <<"\nHere are you nonrepeating numbers: \n";
for(i=0;i<size;i++){
j=linearSearch(numbers,i,size);
if (j!=-1){
numbers[i]=j;
cout <<numbs[i]<<" ";}}
return 0;
}
Quote:
Fear the mullet!!!!
:confused: :eek:
-
I would encourage you to write out in english what it is you are trying to do before you try to write code for the process. Here's and example.
This problem will accept user input of 20 integers greater than zero, sort them, and then print out all unique numbers in the entries.
Unique numbers in an sorted array can be displayed by printing the last item in a sequence of identical items. In an array sorted from lowest to highest adjacent numbers will either be the same or the current one less than the next one. If the current number is less than the next number then it is the last in a sequence or uniuqe (which is a sequence of 1). The last item of the array is by definition the last item in a sequence.
//for each element in the array except the last one
for(i = 0; i < size - 1; i++)
{
//of the current item is less than the next one
if(array[i] < array[i + 1])
//print it out
{cout << array[i];}
}
//print out the last one when all the others have been evaluated.
cout << array[size - 1];
-
> numbers[i]=j;
this should be numbs[i] = j;
You were trying to read from an array in which you didn't assign anything, that's why you got messed up output.
Here's the working program
Code:
#include <iostream>
using namespace std;
int linearSearch(const int array[], int key, int size){
for (int n=0; n<size; n++){
if(array[n]==key){
return array[n];
n++;
}
}
return -1;
}
int main(int argc, char* argv[]){
const int size=20;
int numbers[size];
int numbs[size];
int i,j;
for(i=0; i<size; i++){
cout<<"Enter a number: ";
cin>>numbers[i];
}
cout <<"Here is your array: \n";
for(i=0; i<size; i++){
cout<<numbers[i]<<" ";
}
cout <<"\nHere are your nonrepeating numbers:\n";
for(i=0;i<size;i++){
j=linearSearch(numbers,i,size);
if (j!=-1){
numbs[i]=j;
cout <<numbs[i]<<" ";
}
}
return 0;
}
-Prelude
-
-
a little question
I have a question on your program. You have linearsearch return a value and then you have the function continue. Is this allowed?
-
if the value of key is found in the array passed in then the value of key which equals array[i] is returned and the function stops without the second return statement ever being reached. If the value of key is not found in any element of the array passed in then the value -1 is returned and the first return statement is never seen. Either way something is returned.
Also:
Either way the line n++ in the if statement of linearsearch() is never used, so a warning should be generated, but it's not an error.
Likewise numbs[] is superfluous (unless it will be used in a later extension of the program), as the value of j could just as easily be printed out if it's value isn't -1.
Unfortunately the program only finds the first instance of a number from 0 to size--it can't find any number entered greater than size, ti can't tell whether the number found is unique (which is what I presume is meant by nonrepeating), and it can't even tell if a given number is entered in two adjacent elements of the array (if that is what is meant by nonrepeating) as it only detects the first instance of a given value of key. But what's the difference-- it does compile without error, and that's the sign of good code; right?
Note to self: DON't BE SO CYNICAL. CHILL OUT.
-
thanx for the help but uh i'm still struggling w/the part about it not being about to do #s above 20 and i cant figure outhow to fix it can anyone please help me im losing my mind the code hasnt changed because i have been putting things in and deleting it like trying to change size's number and few other things that just didn't work
help?:( :confused: :eek: