Thread: array program

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

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

    Fear the mullet!!!!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    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];

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > 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
    My best code is written with the delete key.

  4. #4
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    thanx

  5. #5
    Unregistered
    Guest

    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?

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    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.

  7. #7
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM