Announcement

Collapse
No announcement yet.

Need help with C++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with C++

    Well I'm totally not getting this, anyone care to help. I'm trying to show an array passing to a function and I also need to include conts with array parameter. I'm missing the function prototype also.

    Code:
    #include <iostream.h>
    
       void changeElement(int a[], int n) {
          for (int i = 0; i < n; i++) {
             a[i] *= 2;
          }
       }
    
       int main() {
          int list[] = { 0, 1, 2, 3 };
          changeElements(list, 4);
          for (int i = 0; i < 4, i++) {
             cout << list[i] << endl;
          }
          return 0;
       }
    
       0
       2
       4
       6
    Also if you feel like giving me hand with C++ programming, drop me a PM, since I'm totally lost at the moment with my classes.
    Why is it called tourist season, if we can't shoot at them?

  • #2
    I made 3 changes to your code:

    1) You called it changeElement and changeElements depending on the line. Maybe just a typo here on the forums?
    2a) You don't want/need the .h when you include iostream. Having .h is the C way, and you want the C++ way.
    2b) After you drop the .h - You must include the std namespace. Without that, cout and endl are not defined. The alternative is to say std::cout and std::endl specifically.

    3) I gave you a function prototype, as an example.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void changeElements(int a[], int n);
    
    int main() {
    	int list[] = { 0, 1, 2, 3 };
    	changeElements(list, 4);
    	for (int i = 0; i < 4; i++) {
    		cout << list[i] << endl;
    	}
    	return 0;
    }
    
    void changeElements(int a[], int n) {
    	   for (int i = 0; i < n; i++){
    		   a[i] *= 2;
    	   }
    }
    P.S. Remember to use g++, and not gcc for this.
    Last edited by Wombat; 7 January 2007, 13:19.
    Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

    Comment

    Working...
    X