Thread: arrays and functions

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    arrays and functions

    I'm working on a function that prints all the array elements that are between 2 values in the array. I keep getting these errors in my function call and I dont know why since I declared them in the prototype. I'm a bit shaky when it comes to deal with arrays in functions. The for loop I have going to print the elements in between the 2 values looks very dry and of course is not done. Do i have to pass in an array as a parameter because I'm working with an array? ugg, this is so frustrating. Thank you for your time.

    Code:
       #include <iostream>
    using namespace std;
    void print (int x,int y );//prototype
    int main()
    {
     
     const int size=10;
    int a[size]={1,2,3,4,5,6,7,8,9,10};
    
     cout<<"The elements printed are"<<print(x,y);//call
    
      return 0;
     
    }
    
     void print(int x, int y) //print out all  elements between 2 given parameters
     {
    
    	 for (int i=x; i<y; i++)  //not done
    		 cout<<i;
    
     }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Sorry, forgot to include the errors.

    Code:
     error C2065: 'x' : undeclared identifier
     error C2065: 'y' : undeclared identifier

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try

    cout<<"The elements printed are"<<print(0,10);//call
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Then i get this error:

    Code:
    binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
    Plus the fact that the function has to work for any 2 given parameters.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >binary '<<' : no operator defined which takes a right-hand operand of type 'void'
    print returns a void type, so it can't be printed. You'll want to split up the calls:
    Code:
    cout<<"The elements printed are ";
    print(0, 10);
    >Plus the fact that the function has to work for any 2 given parameters.
    It will, but you need to get those values somehow first. The easiest way is to hardcode them, but I imagine you'll want to prompt for user input.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Ok that problem with the errors has been solved, thanks prelude.

    The program runs fine but there is one logic error. When I enter 2 numbers, it always prints out the range of numbers in between the 2 in the array but also the first number entered. I tried changing the condition in the for loop but I get tons of errors as a result. If anybody can assist me with excluding the first parameter in the range without causing tons of errors, I would be ever so grateful.

    Code:
         #include <iostream>
    using namespace std;
    void print (int x,int y );//prototype
    int main()
    {
    	 
     int x;
     int y;
     const int size=10;
    int a[size]={1,2,3,4,5,6,7,8,9,10};
    
     cout<<"Enter 2 values: ";
     cin>>x>>y;
     cout<<"The elements printed between"<<x<<"and"<<y<<"are";
    	 print(x,y);//call
    
      return 0;
     
    }
    
     void print(int x, int y) //print out all  elements between 2 given parameters
     {
    
    	 for (int i=x; i<y; i++) //can't exclude x from being printed
    		 cout<<i;
    
     }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I think you're confused about what you're doing and what you want to do. What you want to do is use x and y as indices for printing out the elements of the array. What you're doing is echoing all numbers between x and y. This is what you want:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void print(int array[], int x, int y);
    
    int
    main()
    {
      int array[] = {1,2,3,4,5,6,7,8,9,10};
      int x, y;
    
      cout<<"Enter two numbers: ";
      cin>> x >> y;
      cout<<"The elements in [x,y) are: ";
      print(array, x, y);
      cout<<endl;
    }
    
    void
    print(
      int array[],
      int x,
      int y
      )
    {
      cout<< array[x];
      while (++x != y) {
        cout<<", "<< array[x];
      }
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    That's pretty good code, Prelude but when I run it, the Y parameter gets printed out also. All i want is this. If the user enters the numbers, 3 and 9. I want my code to print out, 4, 5, 6, 7, 8. Thats it. With my original code I kept printing out the 3.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but when I run it, the Y parameter gets printed out also.
    No, it doesn't. In C++, array indices begin with 0. So the 9th element is 10, which is not printed when you give the input 3 9. So my code will give you the correct answer if you want every element from array[x] to array[y - 1] including array[y - 1] printed.

    >I want my code to print out, 4, 5, 6, 7, 8. Thats it.
    Then you subtract 1 from the condition:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void print(int array[], int x, int y);
    
    int
    main()
    {
      int array[] = {1,2,3,4,5,6,7,8,9,10};
      int x, y;
    
      cout<<"Enter two numbers: ";
      cin>> x >> y;
      cout<<"The elements in [x,y) are: ";
      print(array, x, y);
      cout<<endl;
    }
    
    void
    print(
      int array[],
      int x,
      int y
      )
    {
      cout<< array[x];
      while (++x != y - 1) {
        cout<<", "<< array[x];
      }
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    You're a genius Prelude! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM