entry level homework help: Passing Arrays into functions.

This is a discussion on entry level homework help: Passing Arrays into functions. within the C++ Programming forums, part of the General Programming Boards category; I'm taking a entry level C++ programming course and we just started on arrays. Our assignment is to take a ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    37

    entry level homework help: Passing Arrays into functions.

    I'm taking a entry level C++ programming course and we just started on arrays. Our assignment is to take a 20 digit number and add it along with another 20 digit number. We create the 20 digit number by creating arrays. My arrays work out but I'm stuck as to how to pass them into another function to display them. I get an "error C2664: 'DisplayResults' : cannot convert parameter 1 from 'int [20]' to 'int'" error. This is what I have so far, sorry if its rough.

    Code:
    #include <iostream>
    using namespace std;
    
    void GetNumbers();
    void DisplayResults(int, int, const int);
    
    const int SIZE = 20;
    
    void main () 
    {
      char FirstNumber = 0;
      char SecondNumber = 0;
      char Number = 0;
      char Sum = 0;
    
      char Continue;
    
      do
      {
        cout << "Please enter first number ==> ";
        GetNumbers();
       
        cout << "Continue 'Y' or 'N' ==> ";
        cin >> Continue;
      }
      
      while (Continue == 'Y');
    }
    
    
    void GetNumbers()
    {
      char Number;
      int I, K = 0;
    	
      int FirstArray[SIZE];
      int SecondArray[SIZE];
      int TempArray[SIZE];
      
      cin >> Number;
        
      for (I = 0; I < SIZE; ++I)
      {  
        FirstArray[I] = 0;
        SecondArray[I] = 0;
        TempArray[I] = 0;
      }
        
      I = 0;
      
      while (Number != '\n' && I < SIZE)
      {
          TempArray[I] = Number - '0';
          ++I;
          cin.get(Number);
      }
    
      for (K = SIZE - 1, --I; I >= 0; --I, --K)
        FirstArray[K] = TempArray[I];
    
      cout << "Please enter second number ==> ";
      cin >> Number;
      
      I = 0;
      
      while (Number != '\n' && I < SIZE)
      {
        TempArray[I] = Number - '0';
        ++I;
        cin.get(Number);
      }
    
      for (K = SIZE - 1, --I; I >= 0; --I, --K)
        SecondArray[K] = TempArray[I];
    
      DisplayResults(FirstArray, SecondArray, SIZE);
    
    }
    
    void DisplayResults(int FirstArray[], int SecondArray[], const int SIZE)
    {
      int I;
      
      for (I = 0; I < SIZE; I++)
        cout << FirstArray[I] << endl;
    
      for (I = 0; I < SIZE; I++)
        cout << SecondArray[I] << endl;
    
      cout << "====================" << endl;
    }
    I get the error trying to pass my 20 digit array into my DisplayResults function. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    void DisplayResults(int, int, const int);
    //...
    void DisplayResults(int FirstArray[], int SecondArray[], const int SIZE)
    Try changing your prototype to match how you actually implement it, as the compiler still "thinks" the function only accepts 3 separate integer parameters.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Awesome, thanks man. Never noticed that through all of my notes and all of my examples : / Thanks again.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Code:
    for (I = 0; I < SIZE; ++I)
      {  
        FirstArray[i] = 0;
        SecondArray[i] = 0;
        TempArray[i] = 0;
      }
    Code:
    while (Number != '\n' && I < SIZE)
      {
          TempArray[i] = Number - '0';
          ++I;
          cin.get(Number);
      }
    How does this even compile these should all be uppercase since 'i' is undefined?
    Last edited by ~Kyo~; 04-20-2010 at 11:59 PM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Good point on the "i", I don't know that changed when I copy and pasted it. They're all capital. I am currently having a problem in my program when I add the two arrays. It displays the correct answer, and the ones carry over correctly, but I get an error message saying, "Run-Time check failure #2- Stack around variable 'SumArray' was corrupted" This is the current code

    Code:
    #include <iostream>
    using namespace std;
    
    void GetNumbers();
    void AddNumbers(int[], int[],int[], const int);
    void DisplayResults(int[], int[], int[], const int);
    
    const int SIZE = 20;
    
    void main () 
    {
      char FirstNumber = 0;
      char SecondNumber = 0;
      char Number = 0;
      char Sum = 0;
    
      char Continue;
    
      do
      {
        cout << "Please enter first number ==> ";
        GetNumbers();
       
        cout << "\nContinue 'Y' or 'N' ==> ";
        cin >> Continue;
      }
      
      while (Continue == 'Y');
    }
    
    
    void GetNumbers()
    {
      char Number;
      int I, K = 0;
    	
      int FirstArray[SIZE];
      int SecondArray[SIZE];
      int SumArray[SIZE];
      int TempArray[SIZE];
      
      cin >> Number;
        
      for (I = 0; I < SIZE; ++I)
      {  
        FirstArray[I] = 0;
        SecondArray[I] = 0;
        TempArray[I] = 0;
      }
        
      I = 0;
      
      while (Number != '\n' && I < SIZE)
      {
        TempArray[I] = Number - '0';
        ++I;
        cin.get(Number);
      }
    
      for (K = SIZE - 1, --I; I >= 0; --I, --K)
      FirstArray[K] = TempArray[I];
    
      cout << "Please enter second number ==> ";
      cin >> Number;
      
      I = 0;
      
      while (Number != '\n' && I < SIZE)
      {
        TempArray[I] = Number - '0';
        ++I;
        cin.get(Number);
      }
    
      for (K = SIZE - 1, --I; I >= 0; --I, --K)
      SecondArray[K] = TempArray[I];
    
      AddNumbers(FirstArray, SecondArray, SumArray, SIZE);
    }
    
    
    
    void AddNumbers(int FirstArray[], int SecondArray[], int SumArray[], const int SIZE)
    {
       for (int I = 0; I <= SIZE; ++I)
      {  
         if (FirstArray [I] + SecondArray [I] > 9)
    	{  
    	  SumArray[I] = (FirstArray [I] + SecondArray [I]) % 10;
    	  SumArray[I - 1] += 1;
    	}
       
       else 
       SumArray[I] = FirstArray [I] + SecondArray [I];
      }
      
      DisplayResults(FirstArray, SecondArray, SumArray, SIZE); 
    
    }
    
    
    
    void DisplayResults(int FirstArray[], int SecondArray[], int SumArray[], const int SIZE)
    {
      int I;
      
      for (I = 0; I < SIZE; I++)
      cout << FirstArray[I];
      
      cout << "\n";
    
      for (I = 0; I < SIZE; I++)
      cout << SecondArray[I];
    
      cout << "\n====================" << endl;
      
      for (I = 0; I < SIZE; I++)
      cout << SumArray[I];
    }
    Like I said, the program runs, but when the answer is outputted I get the run-time error. Any ideas? When I run the debugger it tells me that the Array is corrupted after it outputs it from DisplayResults.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because the loop in your add function goes one too far, thus accessing and overwriting other bits of memory.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Tabstop you're the man. Thanks again. One last question, whats the best way to pull my arrays out of the functions so I don't have to have them run inside one another? How do I get the Arrays out so I can run the functions from main?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Declare the arrays inside main (and C++ has never had "void main", you need to change that to "int main"). Pass them to the Get function, pass them to the Add function, pass them to the Display function.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    But once I return to the main from GetNumbers their values disappear. Thats why I ended up never breaking from the functions because I am unsure on how to return/reference an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 05:46 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21