Thread: help with arrays

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Post help with arrays

    hey everybody. im new here and i just started using c++. i based this code off of the code in the function tutorial. basically, it was meant to ask for how many numbers you would like to multiply, then ask you what the numbers were and multiply them. i thought i would store the information for the multiple numbers in an array. i dont understand the subject all that well so any help would be appreciated. i keep getting incorrect answers when i run the code. here it is.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int multiply(int array[10], int a, int b, int number);
    int main()
    {	
    	int c = 1, b = 1, a = 1, number, array[10];
    
    	cout << "How many numbers would you like to multiply? ";
    	cin >> number;
    	cout << "Please input " << number << " numbers to be multiplied: ";
    	
    	// store information to array
    	while (c <= number)
    	{
    		cin >> array[b];
    		c ++;
    		b ++;
    	}
    
    	cout << "The product of the " << number << " numbers is: " << multiply(array, a, b, number)
    		<< endl;
    	
    	return 0;
    }
    int multiply(int array[10], int a, int b, int number)
    {
    	b = 1;
    
    	//retrieve information from array
    	while (b <= number)
    	{
    		a = a + array[b];
    		b ++;
    	}
    	return a;
    }
    thanks for any help.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You're not multiplying those numbers together; you're summing them. Use more descriptive identifers. For loops would be better than while loops here.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    2
    ok, i made the changes. thanks for the help / advice.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Smile

    You're welcome.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM